Vidh Phadke
Vidh Phadke

Reputation: 21

How to change the value of a managed variable?

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

Upvotes: 2

Views: 558

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59208

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

Upvotes: 3

Related Questions