将void *转换为托管对象 [英] converting void* to managed object

查看:83
本文介绍了将void *转换为托管对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此先感谢您的帮助.
我有以下代码将非托管void *转换为托管对象.

Thanks in advance for your help.
I have the following code to convert the unmanaged void* to managed object.

void ListenerCallback(int msg, int p1, int p2, void* p3, void* cookie) 
{
    mController^ mlc;
    // unwrap the native object...
    if(cookie){
    GCHandle h = GCHandle::FromIntPtr(IntPtr(reinterpret_cast<void*>(cookie))); 
	try 
	{ 
	  // and cast the object back out 
	  mlc = safe_cast<mcontroller^>(h.Target); 
	} 
	finally 
	{ 
	   h.Free();
	} 
   }
}



我在 GCHandle :: FromIntPtr处引发了致命错误.



I throws a Fatal error at GCHandle::FromIntPtr.

The runtime has encountered a fatal error. The address of the error was at 0x71de2e63, on thread 0x1474. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.



请指导我解决这个问题.
谢谢.



Please guide me on solving this.
Thanks.

推荐答案

该消息完全正确:您正在做极端糟糕的事情.共享对象甚至句柄都是很危险的-即使有时也可以.

您应该传输原始句柄或数据,并对其进行本地复制,然后再进行处理.
The message is completly right: you are doing extremly bad stuff. Sharing objects or even handles is dangerous - even it works sometimes.

You should transfer the raw handle or data AND make a local copy of it and than do your stuff.


这篇关于将void *转换为托管对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆