Let's write this example program to see how Booleans work in .NET and how to change the value with WinDbg:
using System;
namespace ChangeValueOfBoolean
{
class Program
{
static void Main()
{
var h = new BooleanHolder();
h.BoolValue = true;
Console.WriteLine("Debug now. Boolean member has the value {0}", h.BoolValue);
Console.ReadLine();
Console.WriteLine("After debugging, boolean member has the value {0}", h.BoolValue);
h.BoolValue = true;
Console.ReadLine();
}
}
class BooleanHolder
{
public bool BoolValue { get; set; }
}
}
Steps to debug:
- Compile it in debug mode
- run the application.
- attach WinDbg
- fix the symbols
.symfix;.reload
- load the .NET extension
.loadby sos clr
- find the relevant object
!dumpheap -short -type BooleanHolder
- dump the object
!do <address>
dump the raw value in memory dd <address>+<offset> L1
We'll see that true == 1
- edit the raw value
ed <address>+<offset> 0
- continue the program
g
- See the output on the console
Press Enter
It has switched to false
Complete walkthrough in WinDbg:
0:004> .symfix;.reload
Reloading current modules
..........................
0:004> .loadby sos clr
0:004> !dumpheap -short -type BooleanHolder
025330c8
0:004> !do 025330c8
Name: ChangeValueOfBoolean.BooleanHolder
MethodTable: 00144d74
EEClass: 00141804
Size: 12(0xc) bytes
File: E:\Projekte\SVN\HelloWorlds\ChangeValueOfBoolean\bin\Debug\ChangeValueOfBoolean.exe
Fields:
MT Field Offset Type VT Attr Value Name
704bf3d8 4000001 4 System.Boolean 1 instance 1 <BoolValue>k__BackingField
0:004> dd 025330c8+4 L1
025330cc 00000001
0:004> ed 025330c8+4 0
0:004> g
