使用c ++编码的dll文件时获取CrossThreadMessagingException [英] get a CrossThreadMessagingException when using a dll file coded by c++

查看:77
本文介绍了使用c ++编码的dll文件时获取CrossThreadMessagingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我尝试使用C#导入由c ++编码的dll,它调用的函数参数是结构之一,该结构的成员之一是回调函数.我通过单步调试代码时发现了CrossThreadMessagingException,我认为可能是这个问题是由于一个线程试图设置另一个线程的数据而引起的,但是我不知道如何在用于转换的委托中使用BeginInvoke()

下面的一些代码

c ++代码和api函数:
HHNET_API KTERR_CODE WINAPI KTNET_OpenChannel(IN char * pServerIP,IN WORD wServerPort,IN char * pDeviceName,IN char * pUserName,IN char * pUserPassword,IN HHOPEN_CHANNEL_INFO * pOpenInfo,OUT HANDLE& hOpenChannel,IN HWND hNotifyWindow = NULL);

HHOPEN_CHANNEL_INFO为我说过的结构
typedef struct _HHOPEN_CHANNEL_INFO_EX
{
DWORD dwClientID;
UINT nOpenChannel:8;
UINT nSubChannel:8;
UINT res: 16;
NET_PROTOCOL_TYPE协议类型;
ChannelStreamCallback funcStreamCallback;
无效* pCallbackContext;
} HHOPEN_CHANNEL_INFO_EX;

ChannelStreamCallback是回调函数,如何使用BeginInvoke( )调用此回调函数?
Recently, I try to use C # to import a dll coded by c++, which calls a function parameter is one of structure, one of the structure's members is a callback function. I found the CrossThreadMessagingException when I debug the code by single-step, I think maybe this problem is caused the a thread try to set another thread's data.But I have no idea how to use the BeginInvoke() in a delegate which used to convert a callback function programed by c++, anybody have some ideas?

some code below

c++ code, the api function:
HHNET_API KTERR_CODE WINAPI KTNET_OpenChannel(IN  char *pServerIP,IN  WORD wServerPort,IN  char *pDeviceName,IN  char *pUserName,IN  char *pUserPassword,IN  HHOPEN_CHANNEL_INFO *pOpenInfo,OUT  HANDLE &hOpenChannel,IN HWND hNotifyWindow = NULL);

the HHOPEN_CHANNEL_INFO is the structure what I said upon
typedef struct _HHOPEN_CHANNEL_INFO_EX
{
 DWORD     dwClientID;
 UINT     nOpenChannel:8;
 UINT     nSubChannel:8;
 UINT     res:16; 
 NET_PROTOCOL_TYPE  protocolType;
 ChannelStreamCallback funcStreamCallback;
 void     *pCallbackContext;
}HHOPEN_CHANNEL_INFO_EX;

ChannelStreamCallback  is the callback function, how can I use the BeginInvoke() to call this callback function?

推荐答案

我不明白吗?当您调用此函数时,您会收到一个CrossThread异常?或更高版本?您如何将其编组为托管代码?
I don't understand ? When you call this function you get an CrossThread exception ?? or later on ?How did you Marshaled it to managed code ?


这篇关于使用c ++编码的dll文件时获取CrossThreadMessagingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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