I have created a DLL that starts up a thread which polls a usb device, when the DLL gets a message from the USB device it calls a callback function.
Using this dll in code consists of creating an instance of the class from the DLL, then calling a method of the said class to set a callback function, then starting the thread so that a callback happens every time the USB device does something, the thread works, the callback happens (it is a keypad device so it just sends keypresses in the form of a LPCSTR) but when I pass this keypress value to an internal function it causes an access violation. Where as anything else calling this function with the same values works just fine.
this is the line that causes the access violation
InvokeHelper(0x60030024, DISPATCH_METHOD, VT_VARIANT, (void*)&result, parms,
num, TheStr);
containing function:
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 set callback:
void WINAPI ThalesEPPv6::SetGoodKeyPadPressCallback(OnGoodKeyPadPressCallback func, LPVOID dialog) { OnGoodKeyPadPress = func;}
DLL start thread:
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 message Thread:
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;
}