我创建了一个DLL,它启动了一个轮询usb设备的线程,当DLL从USB设备收到一条消息,它调用了一个回调函数。
在代码中使用这个dll包括从DLL创建类的实例,然后调用所述类的方法来设置回调函数,然后启动线程,以便每次USB设备执行某些操作时都会发生回调,线程工作,回调发生(它是一个键盘设备,所以它只是以LPCSTR的形式发送按键)但是当我将此按键值传递给内部函数时,它会导致访问冲突。其他任何调用具有相同值的函数的工作正常。
这是导致访问冲突的行
InvokeHelper(0x60030024, DISPATCH_METHOD, VT_VARIANT, (void*)&result, parms,
num, TheStr);
包含功能:
VARIANT C_BAXScreens::WriteToTextBox(short num, LPCTSTR TheStr)
{
VARIANT result;
static BYTE parms[] =
VTS_I2 VTS_BSTR;
InvokeHelper(0x60030024, DISPATCH_METHOD, VT_VARIANT, (void*)&result, parms,
num, TheStr);
return result;
}
DLL设置回调:
void WINAPI ThalesEPPv6::SetGoodKeyPadPressCallback(OnGoodKeyPadPressCallback func, LPVOID dialog) { OnGoodKeyPadPress = func;}
DLL启动线程:
void ThalesEPPv6::startMessageThread()
{
OnCheckResponseOK("Message Thread Started.");
stopThread = false;
if(!connected)reconnect(this);
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)_beginthreadex( NULL, 0, messageThread,this, 0, &threadID );
}
DLL消息线程:
unsigned int _stdcall ThalesEPPv6::messageThread(void* links)
{
ThalesEPPv6* link = (ThalesEPPv6*)links;
while(1)
{
link->send_Message(POLLIN,ENQ,MESSAGE_NOCOMMS);
link->send_Message(POLLIN,MSG,MESSAGE_NOCOMMS);
link->send_Message(POLLIN,EOT,MESSAGE_NOCOMMS);
//MESSAGE REPLY
link->send_Message(POLLIN,ACK,MESSAGE);
link->send_Message(POLLIN,ACK,MESSAGE_NOCOMMS);
Sleep(10);
if(stopThread)break;
}
// close USB handle
usb_close(usb_handle);
return 0;
}