MsgWaitForMultipleObjects exception migrating Internet Explorer Browser Helper Object to Google Chrome

1

Our internal application includes a Browser Helper Object (BHO) for dealing with some asynchronous functions (peripherals, etc) by calling methods of two DLLs, it works in Internet Explorer based on its ActiveX support.

We need to remove any use of ActiveX and have the application working in other browsers (i.e Google Chrome) so we built an API REST service that allow us to call methods of the DLLs and for most of the cases it works fine, however for one specific method, when the API REST Service makes the call there is an Access Violation (0xC0000005) in Windows and the service stops, after much debugging in the called DLL we found that the error was in the following piece of c++ code. This code makes "flow" the requests/answers to and from the peripherals software using MSMQ, it works fine when called by IE ActiveX but fails when called by the API Rest Service:

  while (TRUE)
  { // Wait until the main application thread asks this thread to do
    // a process
    dwRet = MsgWaitForMultipleObjects( 1                ,
                                       &hEventEndThread ,
                                       FALSE            ,
                                       INFINITE         ,
                                       QS_ALLINPUT       );
    if (dwRet == WAIT_OBJECT_0)
    { // Exit the thread if the main application sets the "end thread"
      // event. The main application will set the "start process" event
      // after setting the "end thread " event.
      break; // Terminate this thread by exiting the proc.
    }
    else
    {
      if (dwRet == WAIT_OBJECT_0 + 1)
      {
        while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
        {
          TranslateMessage(&msg);
          DispatchMessage(&msg);     /// This is the line where the Windows Access Violation occurs
        }
        continue;
      }
      else
      {
        continue;
      }
    }
  }

We found that the exception was thrown in DispatchMessage(&msg); and we suspect that this code may be tied to IE BHO functionality but are not sure about it or the way to "fix it", we have tried changing the MsgWaitForMultipleObjects flags from QS_ALLINPUT with the following results:

  • QS_INPUT: There was no exception but the intended process was not carried away
  • QS_POSTMESSAGE: There was exception
  • QS_SENDMESSAGE: There was no exception but the intended process was not carried away

We are trying different combinations but don't feel comfortable with this "guessing" approach and the google searches about the use of those flags have not been very insightful.

The question is either:

  • What should be the value of the flags for the send/arrived events of MSMQ? or
  • Should we change the MsgWaitForMultipleObjects approach for being aware of send/arrived events of MSMQ?

NOTE: I'm fully aware this is a complex case (we have being trying to solve it for days) and tried to make an abstraction that could be posted as a SO question, i'm more than willing to reformulate it tough.

EDIT: We added some log and found that the exception always happens the third time some message arrives (all of this in the same second), we also printed out the attributes of msg and found this values:

Wed Feb 05 17:34:38 2020 PeekMessage msg.message: 799
Wed Feb 05 17:34:38 2020 PeekMessage msg.wParam: 1
Wed Feb 05 17:34:38 2020 PeekMessage msg.lParam: 0
Wed Feb 05 17:34:38 2020 PeekMessage msg.hwnd: 2101766
Wed Feb 05 17:34:38 2020 PeekMessage msg.time: 113452640

Wed Feb 05 17:34:38 2020 PeekMessage msg.message: 799
Wed Feb 05 17:34:38 2020 PeekMessage msg.wParam: 1
Wed Feb 05 17:34:38 2020 PeekMessage msg.lParam: 0
Wed Feb 05 17:34:38 2020 PeekMessage msg.hwnd: 397728
Wed Feb 05 17:34:38 2020 PeekMessage msg.time: 113452640

Wed Feb 05 17:34:38 2020 PeekMessage msg.message: 49891
Wed Feb 05 17:34:38 2020 PeekMessage msg.wParam: 2304
Wed Feb 05 17:34:38 2020 PeekMessage msg.lParam: 82366828
Wed Feb 05 17:34:38 2020 PeekMessage msg.hwnd: 397728
Wed Feb 05 17:34:38 2020 PeekMessage msg.time: 113452640

Thanks,

Johann

c++
events
activex
msmq
bho
asked on Stack Overflow Feb 4, 2020 by JohannSinuhe • edited Feb 5, 2020 by JohannSinuhe

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0