简体   繁体   English

是否可以在C#中将IntPtr的值转换为另一个IntPtr而又不安全?

[英]Can I convert a value of IntPtr to another IntPtr without unsafe in C#?

Suppose I have a IntPtr which was a void** get from native code. 假设我有一个IntPtr,它是从本地代码获取的**。 When I was going to deference it to a void* equivalence IntPtr I used such code: 当我打算将其引用为void *等效IntPtr时,我使用了以下代码:

unsafe
{
    var innerPtr = (IntPtr)outerPtr.ToPointer();
}

To do this I need to enable the unsafe code. 为此,我需要启用unsafe代码。 I'm wondering if there is another way to convert this kind of pointers without unsafe? 我想知道是否存在另一种在不安全的情况下转换这种指针的方法?


Update: 更新:

Sorry for my question is invalid. 对不起,我的问题是无效的。 The usage of (IntPtr)outerPtr.ToPointer() is meaningless. (IntPtr)outerPtr.ToPointer()的用法是没有意义的。

(IntPtr)outerPtr.ToPointer() doesn't actually do anything useful, the result still contains the same address. (IntPtr)outerPtr.ToPointer()实际上并没有做任何有用的事情,结果仍然包含相同的地址。

If the equivalent of dereferencing a void** to a void* is what you want, you can use Marshal.ReadIntPtr(IntPtr address) . 如果您想要将void**取消引用为void** void*则可以使用Marshal.ReadIntPtr(IntPtr address)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM