CLI/C++将“this”转换为“this”指向整数的指针

发布于 2024-08-16 13:39:35 字数 340 浏览 2 评论 0原文

我正在尝试在 CLI/C++ 程序中跟踪托管对象创建/处置:

::System::Diagnostics::Trace::WriteLine(String::Format(
    "Created {0} #{1:X8}", 
    this->GetType()->Name,
    ((UInt64)this).ToString()));

失败并

显示错误 C2440: 'typecast' : 无法从 'MyType ^const ' 转换为 'unsigned __int64'

是否存在有没有办法以这种方式跟踪唯一的对象 ID? 谢谢!

I am trying to trace managed object creation/disposing in a CLI/C++ prog:

::System::Diagnostics::Trace::WriteLine(String::Format(
    "Created {0} #{1:X8}", 
    this->GetType()->Name,
    ((UInt64)this).ToString()));

Which fails with

error C2440: 'type cast' : cannot convert from 'MyType ^const ' to 'unsigned __int64'

Is there a way to keep track of the unique object IDs this way?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请止步禁区 2024-08-23 13:39:35

首先,为什么这不起作用。托管句柄类型 ^ 不是指针。它们不仅仅是地址。托管类型的实例可以并且将会被 GC在内存中移动,因此地址不稳定;因此,为什么它不允许您执行这样的转换(因为 GC 可以随时执行,并且您不知道何时执行,任何使用此类地址作为原始值的尝试本质上都是竞争条件)。

另一件经常推荐但实际上不起作用的方法是Object.GetHashCode()。一方面,它返回一个 int,显然不足以在 x64 上保持唯一。此外,文档不保证值是唯一的,而且它们实际上不在 2.0+ 中。

唯一可行的解​​决方案是为您的对象创建一个 System.Runtime.InteropServices.GCHandle 实例,然后将其转换为 IntPtr - 这保证都是唯一的,并且稳定。

First of all, why this doesn't work. Managed handle types ^ aren't pointers. They aren't just addresses. An instance of a managed type can and will be moved around in memory by GC, so addresses aren't stable; hence why it wouldn't let you do such a cast (as GC can execute at any moment, and you do not know when, any attempt to use such an address as a raw value is inherently a race condition).

Another thing that is often recommended, but doesn't actually work, is Object.GetHashCode(). For one thing, it returns an int, obviously not enough to be unique on x64. Furthermore, the documentation doesn't guarantee that values are unique, and they actually aren't in 2.0+.

The only working solution is to create a an instance of System.Runtime.InteropServices.GCHandle for your object, and then cast it to IntPtr - that is guaranteed to be both unique, and stable.

懒的傷心 2024-08-23 13:39:35

查看 GCHandle 类型:http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.aspx。看起来它会做你想做的事,尽管看起来它对于你的目的来说会有点痛苦......

Check out the GCHandle type: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.aspx. Looks like it would do what you want, though it looks it would be a bit of a pain to use for your purposes...

情栀口红 2024-08-23 13:39:35

即使您可以将 this 转换为某个整数值以进行显示,它也可能不是一个有用的唯一标识符。这是因为与 C++ 不同,在 C++/CLI 中,(托管)对象的位置(以及扩展后的值)可能会在该对象的生命周期内发生变化。 (逻辑上)相同的对象可以在程序中的不同点打印两个不同的字符串。

Even if you could cast this to some integral value for display, it probably wouldn't be a useful unique identifier. This is because unlike C++, in C++/CLI the location of a (managed) object (and by extension the value of this) can potentially change during that object's lifetime. The (logically) same object could print two different strings at different points in the program.

输什么也不输骨气 2024-08-23 13:39:35

MyType ^const 是引用类型。因此,它位于托管内存空间中,并且您无法获取指向这些类型的直接内存指针,因为它们可以随时更改。

有没有办法以这种方式跟踪唯一的对象 ID?谢谢!

您可以使用 MyType.GetHashCode();

MyType ^const is a reference type. Hence it's in the managed memory space, and you can't get direct memory pointers to these types, as they can change at any time.

Is there a way to keep track of the unique object IDs this way? Thanks!

You could use MyType.GetHashCode();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文