【问题标题】:How to change the value of a managed variable?如何更改托管变量的值?
【发布时间】:2015-11-26 10:32:03
【问题描述】:

我的布尔变量可以使用语法来引用

MPrime.exe Spirit.MPrimeComServerManager._isComServerReady

我尝试过使用语法

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

我不确定如何将e* 命令与托管代码一起使用。

这是!DumpObj的输出:

00007fff81a6d6e8  4000198   169   System.Boolean  1   static   0 _isComServerReady

【问题讨论】:

    标签: variables windbg managed editing


    【解决方案1】:

    让我们编写这个示例程序,看看布尔值在 .NET 中是如何工作的,以及如何使用 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; }
        }
    }
    

    调试步骤:

    1. 在调试模式下编译
    2. 运行应用程序。
    3. 附加WinDbg
    4. 修复符号.symfix;.reload
    5. 加载 .NET 扩展 .loadby sos clr
    6. 找到相关对象!dumpheap -short -type BooleanHolder
    7. 转储对象!do <address>
    8. 将原始值转储到内存中dd <address>+<offset> L1

      我们会看到true == 1

    9. 编辑原始值ed <address>+<offset> 0
    10. 继续程序g
    11. 查看控制台上的输出
    12. Enter

      已切换到false

    在 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
    

    【讨论】:

    • 不错的演练,如果您能展示如何使用 c++ / c# 进行修改会不会很棒?表达之类的?? foo::booleanxxx::bool; ed foo:boolean::xxx 而不是 ed @@masm(address) 无论如何都赞成
    • @blabb:嗯,我从来没有那样做。应该可以吗?我试试
    • cdb -g booldoll.exe 1 ?? booldoll!foo bool true 0:001> ed booldoll!foo 0 0:001> ?? booldoll!foo bool false 0:001> g 0 bool foo =true; int main (void) { char temp[10]; printf("%x\n",foo);获取_s(温度,大小(温度)); printf("%x\n",foo);获取_s(温度,大小(温度)); }
    • @blabb:问题被标记为“管理”。您的示例看起来像普通的 C++。我无法得到??运算符以使用纯 C# 程序。也许它适用于 C++/CLI...
    • 是的,它是纯 c++,我不怎么做 c#,所以我想知道你能不能做到这一点
    猜你喜欢
    • 1970-01-01
    • 2014-03-19
    • 2015-11-09
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多