Problem with COM DLL---- Firing event in thread
-
Hi, I am using the ATL COM DLL. In this I am createng two threads. I have added new ATL object which supports connceting point, appartment thereaded model, Dual interface and agregation=yes. While creating thread and passing the interface pointer as this pointer. hStatusThread = CreateThread(NULL, o, LPTHREAD_START_ROUTINE(StatusThread), (LPVOID)this, 0, &dwThStId); ::SetThreadPriority(hStatusThread, THREAD_PRIORITY_HIGHEST); I am accessing the interface pointer in thread and firing the event.. UINT StatusThread(LPVOID param) { CMyConnectObj *ptrObj = NULL; ptrObj = (CMyConnectObj*)param; if(ptrObj == NULL) return 0; if(WaitForSingleObject(g_Mutex,INFINITE) == WAIT_OBJECT_0) { ptrObj->Fire_ErrorCode(lErrorCode); ReleaseMutex(g_Mutex); } } I am using vb6.0 client to use this dll. but while firing event at line i ptrObj->Fire_ErrorCode(lErrorCode); it's giving error as "Unhandled Exception in ConnectProg.exe (MSVBVM60.dll)0x0000005: Access Violation. But if I tried to fire event for one of the ATL object function there is no problem at all. Please help me for this with perect solution.. It's very urgent.
-
Hi, I am using the ATL COM DLL. In this I am createng two threads. I have added new ATL object which supports connceting point, appartment thereaded model, Dual interface and agregation=yes. While creating thread and passing the interface pointer as this pointer. hStatusThread = CreateThread(NULL, o, LPTHREAD_START_ROUTINE(StatusThread), (LPVOID)this, 0, &dwThStId); ::SetThreadPriority(hStatusThread, THREAD_PRIORITY_HIGHEST); I am accessing the interface pointer in thread and firing the event.. UINT StatusThread(LPVOID param) { CMyConnectObj *ptrObj = NULL; ptrObj = (CMyConnectObj*)param; if(ptrObj == NULL) return 0; if(WaitForSingleObject(g_Mutex,INFINITE) == WAIT_OBJECT_0) { ptrObj->Fire_ErrorCode(lErrorCode); ReleaseMutex(g_Mutex); } } I am using vb6.0 client to use this dll. but while firing event at line i ptrObj->Fire_ErrorCode(lErrorCode); it's giving error as "Unhandled Exception in ConnectProg.exe (MSVBVM60.dll)0x0000005: Access Violation. But if I tried to fire event for one of the ATL object function there is no problem at all. Please help me for this with perect solution.. It's very urgent.
chetanjoshi9 wrote:
But if I tried to fire event for one of the ATL object function there is no problem at all.
This is due to the fact that you have not initialized COM for your new apartment, i.e. in your worker thread. You have to call
::CoInitialize()
before using any COM functionality from your worker thread. Don't forget to place a matching call to::CoUninitialize()
before exiting your worker thread. When you've done that you will hit another problem since you're trying to call the interface to the client's event sink from another apartment than it belongs to. You have to marshal the events sink interface to the apartment of your worker thread. Have a look at Michael Lindig's solution for that here[^]. It uses the GIT (Global Interface Table) for that and you'll be able to fire your events from any apartment."It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown