显示一个对象的存储位置或地址

显示一个对象的存储位置或地址

本文介绍了.NET:显示一个对象的存储位置或地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让地址的对象?这是出于演示的目的,我知道这是一般一个坏主意,如果能很好的工作,然后为不安全 code。该项目被调整为允许不安全的code。但是我尝试均告失败。在code我至今未编译:

Is there a way to get the "address" of an object? This is for demonstration purposes, I know this is a bad idea in general and if it works at all then as unsafe code. The project is tuned to allow unsafe code. However my tries were unsuccessful. The code I have so far is not compiling:

    unsafe static String AddressOf(Object o)
    {
        void* p = &o;
        return String.Format("{0}", new IntPtr(p));
    }

错误:无法承担的地址,获取的大小,或者声明指向一个托管类型(对象)

即使存储器地址不能retrieven,那么也许内存插槽还是其他什么东西显示出物体的位置。

Even if The memory address cannot be retrieven, then maybe a memory slot or something else showing the location of that object.

背景:我的意思是做一些演示显示按值传递之间的差异或引用和转储这些对象的位置

Background: my intention is to make some demo showing the differences between passing by value or by reference and dump the location of these objects.

ADDED 2010-06-27:

我终于抛弃了从.NET程序内部做这件事的想法。似乎没有导致任何好的解决方案。即使曾它会堆满了code这么多,你可以勉强解释的结果简单的方法,并使用它的一些演示的目的。我现在想用一个好(意为CLR aware)的调试器是谏在下面的答案的方式做到这一点。

I finally abandonned the idea of doing it from within the .NET program itself. Seems not to lead to any good solution. Even if it had worked it would have cluttered the code so much that you can barely explain the result in simple ways and use it for some demo purpose. I would now like to do it with a good (meaning CLR aware) debugger in the way that is adviced in the answer below.

调试可以在里面VS在配置设置激活选项改善启用非托管的调试,遗憾的是没有在Visual Studio中的自由前preSS版可用(唯一的版本,我在家里现在)。 有没有一种方法,使启用非托管的调试手动工作?

Debugging can be improved inside VS by activating an option in the configuration settings Enable Unmanaged Debugging, unfortunately not available in the free express edition of visual studio (the only version I have at home right now). Is there a way to make Enable Unmanaged Debugging work manually?

挣扎了一下之后,我安装了调试工具为其提供的Windows 我在WinDbg。伟大的工具,我用几年前的驱动程序开发。现在,它是一个巨大的计划的一部分。我试图按照指示来获取SOS.DLL工作方式没有成功。我甚至无法找到这个DLL。即使这个解决方案似乎要求启用非托管的调试标志项目...

After struggling a bit I installed Debugging Tools for Windows which provided me the WinDbg. Great tool I used years ago for driver development. Now it's part of a huge package. I tried to follow the instructions to get the SOS.DLL way working without success. I cannot even find this DLL. Even this solution seems to require the Enable Unmanaged Debugging flag in the project...

推荐答案

为了使演示有意义的,你需要先销的对象。

To make the demo meaningful, you need to pin the object first.

GCHandle handle = GCHandle.Alloc(o, GCHandleType.Pinned);
IntPtr address = handle.AddrOfPinnedObject();

我将演示如何使用 SOS (调试工具的一部分Windows软件包)。你和放大器;你的听众将实际获得一些宝贵的技能的方式,而不是仅仅的挥手约CLR实现细节。

I would demonstrate using SOS (part of the Debugging Tools for Windows package). You & your audience will actually gain some valuable skills that way, rather than just hand-waving about CLR implementation details.

这篇关于.NET:显示一个对象的存储位置或地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:34