0

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;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Perry Ward
  • 15
  • 4
  • code added any more bits?? – Perry Ward Aug 26 '14 at 08:55
  • Note: TheStr is not a BSTR. Can't you debug the invoked method? – manuell Aug 26 '14 at 08:59
  • Also: are you building for UNICODE? – manuell Aug 26 '14 at 09:00
  • this isn't my program so Im not sure what this fuction is doing exactly all I know is that when it is called with the same values from a different device it works fine, as to debug the invoke method, I get an access violation on that line then it goes into assembly I'm not sure what other debugging you mean? Not sure about unicode either – Perry Ward Aug 26 '14 at 09:05
  • You have to pass a real `BSTR` to `InvokeHelper`. What is the `typedef` for `LPCTSTR` in your environment? Does it rely on `char` or `wchar_t`? – manuell Aug 26 '14 at 09:14
  • I'm not sure and I'm not sure how to find out I'm using VC6... – Perry Ward Aug 26 '14 at 09:24
  • Add `BSTR bstrTest=::SysAllocString(L"Hello");` and pass `bstrTest` in place of `TheStr`. Still crashing? – manuell Aug 26 '14 at 09:28
  • That prints "hello" okay to the textbox when called internally, but when called from the dll thread it still causes an access violation, I have the stack trace if that would be helpful: – Perry Ward Aug 26 '14 at 09:41
  • Stack trace may help – manuell Aug 26 '14 at 09:46
  • `72949197() `72a0a1b4() `5f587fe0() `5f512e9e() `5f436e7f() `C_BAXScreens::WriteToTextBox(short 47, const char * 0x0164ac7c) line 373 + 36 bytes' 'CBASelfServiceApplicationDlg::HandleKeyEntryDS(CString {"1"}) line 15945' 'CBASelfServiceApplicationDlg::HandleKeyPress(CString {"1"}, int 0) line 14816 `CBASelfServiceApplicationDlg::OnGoodKeyPadPress_Thales(const char * 0x1012102c `string') line 18182 `CBASelfServiceApplicationDlg::OnGoodKeyPadPress_Thales_V6(const char * 0x1012102c `string') line 18143 – Perry Ward Aug 26 '14 at 09:47
  • Please explain the threading architecture and the callback mechanism. Is the IDispatch call occurring in the thread hosting the COM object? – manuell Aug 26 '14 at 10:07
  • there is no IDispatch call, not sure what you mean by the thread hosting the COM object either... – Perry Ward Aug 26 '14 at 10:37
  • By `IDispatch call` I mean the `InvokeHelper` call, because that's what it does. C_BAXScreens is a COM object, right? – manuell Aug 26 '14 at 10:42
  • C_BAXScreens is an activeX controller, The dialog which the process is usually called from has some kind of thread in it but the codes a bit messy :( – Perry Ward Aug 26 '14 at 10:59
  • The crash is maybe related to calling the ActiveX from another thread. The `InvokeHelper` call take place in your own special thread? – manuell Aug 26 '14 at 12:23

0 Answers0