2

My boolean variable can be referenced using the syntax

MPrime.exe Spirit.MPrimeComServerManager._isComServerReady

I have tried using the syntax

?? MPrime.exe Spirit.MPrimeComServerManager._isComServerReady=1 

and I am not sure how to use e* commands with managed code.

This is the output from !DumpObj:

00007fff81a6d6e8  4000198   169   System.Boolean  1   static   0 _isComServerReady
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222

1 Answers1

3

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:

  1. Compile it in debug mode
  2. run the application.
  3. attach WinDbg
  4. fix the symbols .symfix;.reload
  5. load the .NET extension .loadby sos clr
  6. find the relevant object !dumpheap -short -type BooleanHolder
  7. dump the object !do <address>
  8. dump the raw value in memory dd <address>+<offset> L1

    We'll see that true == 1

  9. edit the raw value ed <address>+<offset> 0
  10. continue the program g
  11. See the output on the console
  12. 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

enter image description here

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • nice walkthrough would it would be great if you could show how to modify with c++ / c#? expression like ?? foo::booleanxxx::bool; ed foo:boolean::xxx instead of ed @@masm(address) anyway upvoted – blabb Nov 27 '15 at 07:03
  • @blabb: Hmm, I never did it that way. Should it be possible? I'll try – Thomas Weller Nov 27 '15 at 16:54
  • cdb -g booldoll.exe 1 <----- (a4.bac): Control-C exception - code 40010005 (first chance) 0:001> ?? booldoll!foo bool true 0:001> ed booldoll!foo 0 0:001> ?? booldoll!foo bool false 0:001> g 0 <-------------- type booldoll.cpp #include bool foo =true; int main (void) { char temp[10]; printf("%x\n",foo); gets_s(temp,sizeof(temp)); printf("%x\n",foo); gets_s(temp,sizeof(temp)); } – blabb Nov 27 '15 at 20:39
  • @blabb: the question was tagged "managed". Your example looks like plain C++. I could not get the ?? operator to work with a pure C# program. Maybe it works in C++/CLI... – Thomas Weller Nov 29 '15 at 15:21
  • yep it is plain c++ i don't do c# much so i was wondering if you could do it that is all – blabb Nov 29 '15 at 16:39