Microsoft KB Archive/196026

= PRB: Firing Event in Second Thread Causes IPF or GPF =

Article ID: 196026

Article Last Modified on 5/22/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q196026



SYMPTOMS
In Visual Basic, an event fired by an ActiveX component from a thread other than the main thread will sometimes cause an Invalid Page Fault (IPF) or General Protection Fault (GPF). Usually it seems to work fine in the Visual Basic IDE but fails when it runs as a standalone EXE.



CAUSE
Visual Basic uses an apartment threading model. Cross-thread function calls need to be marshalled. Visual Basic does not support events fired directly from any thread other than the main thread created by a Visual Basic project without marshalling.



Method 1
Marshall the event firing code:
 * 1) The thread being spun off needs to call CoInitialize/CoUnInitialize.
 * 2) The sink interface being called back on in the thread must be marshalled over to that thread with CoMarshalInterThreadInterfaceInStream/CoGetInterfaceAndReleaseStream.

Method 2
Instead of firing an event from a secondary thread, post a message back to the main thread and fire the event there. An example of this method is given later in this article.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
The following example first creates an ATL project, then a Visual Basic 6.0 project. It then uses these to demonstrate the proper way to have a secondary thread request that the primary thread fire an event.

Steps to Create an ATL Project with Visual C++
 Create a new ATL COM AppWizard Project named MyAtlDll and keep the default settings. In the class view, right-click the project name and select New ATL Object. Select Simple Object in the displayed dialog box and click Next. Type in "MyAtl" as the C++ short name. Click the Attribute tab and select Support Connection Points. Click OK and a new ATL object is added. In the class view, right-click the IMyAtl and choose Add Method. Type "InitTask" in the Method Name text box and "[in] long number" in the parameters text box. Click OK. In the class view, right-click _IMyAtlEvents and select Add Method. Select "void" from return type drop-down list box. Type "TaskFinished" in the Method Name text box and "[in] long result" in the parameters text box. Click OK. In the class view, right-click CMyAtl and choose Add member variable. Type "long" in the variable type text box and "m_number" in the variable name text box.</li> Build the project to generate the type library needed for step 10.</li>  In the class view, expand and double-click CMyAtl -> IMyAtl -> InitTask. Edit the InitTask function to make it appear as follows: STDMETHODIMP CMyAtl::InitTask(long number) {        HANDLE hThrd; DWORD tid;

m_number = number; if((hThrd = CreateThread(                0,                 0,                 (LPTHREAD_START_ROUTINE)justDoIt,                 (void *)this,                 0,                 &tid)) == NULL) {           //error handling here }        CloseHandle(hThrd); return S_OK; }

</li>  Add the following code to the MyAtl.cpp file: DWORD WINAPI justDoIt(LPVOID lpParameter) {        CMyAtl *myAtl = (CMyAtl*)lpParameter;

long result; for (int i = 1; i <= myAtl->m_number; ++i) result = i * 2;

myAtl->Fire_TaskFinished(result);

return 0; }

</li>  Add the following code right above the line "#endif //__MYATL_H_" in MyAtl.h file: DWORD WINAPI justDoIt(LPVOID lpParameter);

</li> In the class view, right-click CMyAtl and select Implement Connection Point. Select _IMyAt-Events in the displayed dialog box. Click OK.</li> Build the ATL project and the control will be registered automatically.</li></ol>

Steps to Create the Visual Basic 6.0 Project
<ol> Create a new Standard EXE project. Form1 is created by default.</li> Choose References from the Project menu, select "MyAtlDll 1.0 Type library," and click OK.</li> Add a CommandButton to the form and keep the default name (command1).</li>  Add the following code to the code window of form1: Option Explicit Private WithEvents vbATL As MYATLDLLLib.MyAtl

Private Sub Command1_Click vbATL.InitTask 11111 End Sub

Private Sub Form_Load Set vbATL = New MYATLDLLLib.MyAtl End Sub

Private Sub vbATL_TaskFinished(ByVal result As Long) MsgBox result End Sub

</li> Press the F5 key to run the project. Click Command1 and you will get 22222 in a message box.</li> Build the project to be an EXE and run the EXE outside the IDE. You will get an error message when you click the CommandButton.

Note that it may work sometimes, but it does not work consistently. To fix the problem in this specific sample, you need to derive the CMyAtl from CWindowImpl, and add a message map. Make sure the window is hidden. Now, instead of firing an event from the secondary thread, you can post a message to the main thread, and fire the event in the message handler.</li></ol>

Steps to Resolve the Problem
<ol>  Using the ATL project created in the steps above, add the following line: #include <atlwin.h>

after the line: #define _MYATL_H

in the MyAtl.h file. </li>  Add the line: #define WM_TASK_FINISH (WM_USER + 101)

before the line: class ATL_NO_VTABLE CMyAtl :

in the MyAtl.h file. </li>  Add CWindowImpl to one of the parent classes of CMyAtl by adding the following line: public CWindowImpl<CMyAtl>,

right after the line: class ATL_NO_VTABLE CMyAtl :

in the file MyAtl.h. </li>  Add the following code to the CMyAtl definition part of the MyAtl.h file: public: DECLARE_WND_CLASS("MyAtl")

BEGIN_MSG_MAP(CMyAtl) MESSAGE_HANDLER(WM_TASK_FINISH, OnTaskFinished) END_MSG_MAP public: LRESULT OnTaskFinished(UINT uMsg, WPARAM wParam,                       LPARAM lParam, BOOL& bHandled) {           Fire_TaskFinished((long)wParam); return 0; }

HRESULT FinalConstruct {          RECT rect; rect.left=0; rect.right=100; rect.top=0; rect.bottom=100;

HWND hwnd = Create( NULL, rect, "MyAtlWindow", WS_POPUP);

if (hwnd) return S_OK; else return HRESULT_FROM_WIN32(GetLastError); }

void FinalRelease {          if (m_hWnd != NULL) DestroyWindow; }

</li> <li> Change the following line: myAtl->Fire_TaskFinished(result);

in the function DWORD WINAPI justDoIt(LPVOID lpParameter)

in the file MyAtl.cpp, to be: myAtl->PostMessage(WM_TASK_FINISH,result,0);

</li> <li>Rebuild the ATL project.</li> <li>Run the Visual Basic project from the IDE and as an EXE. Note that the event fires just fine either way.</li></ol>

<div class="references_section">