troubleshooting Question

[COM &multithread] why is there a exception happened?

Avatar of LuRen
LuRen asked on
C++Microsoft DevelopmentEditors IDEs
24 Comments1 Solution1115 ViewsLast Modified:
I have created a multithread COM with ATL (VC++ 6.0/Sp5).  I use the ATL wizard. Here is some detail of the COM:
 
    ================================
 
   "New ATL Object..." -> "Objects" -> "Simple Object"  -> after input the name of the COM, i set the attributes of the com below:
           Threading Model : Apartment  (Default)
           Interface       : Dual (Default)
           Aggregation     : Yes (Default)

     Select "Support ISupportErrorInfo"
     Select "Support Connection Points"
     don't select "Free Threaded Marshaler"

   ===========================================

   OK, here is a simple COM created. Now i added methods and event of the COM. just for testing, I added two method:    

StartUp(), Shutdown(), and added three event: OnFirstEvt() , OnSecondEvt(), OnThirdEvt().

   I defined four message : MSG_FIRST, MSG_SECOND, MSG_THIRD, MSG_QUIT

   In StartUp(), I created two thread : DaemonThread, TestThread.

   DaemonThread had a message queue to receive the four message (MSG_FIRST, MSG_SECOND, MSG_THIRD, MSG_QUIT). If it

receive MSG_FIRST, it call fire_OnFirstEvt() and the client (maybe UI) can receive the event.  MSG_SECOND and MSG_THIRD are in the same way. When it receive MSG_QUIT, the thread is finished. here is some codes of it :

     ==============================================

                ...

            while (1) {
                  WaitMessage();
                  while( GetMessage( &msg, NULL, MSG_FIRST, MSG_QUIT ) ) {
                        switch(msg.message) {
                        case MSG_FIRST:
                              lpCOM->Fire_OnFirstEvt();
                              break;

                        case MSG_SECOND:
                              lpCOM->Fire_OnSecondEvt();
                              break;

                        case MSG_THIRD:
                              lpCOM->Fire_OnThirdEvt();
                              break;

                        case MSG_QUIT:
                              return 0;

                        default:
                              break;
                        }
                  }

                ...

     ==============================================

   TestThread is very simple. It send message to DaemonThread. here is some codes of it :
 

     ==============================================

                ...

            status = PostThreadMessage( lpDaemonThreadID, MSG_FIRST, (WPARAM)NULL, (LPARAM)NULL );

            if (status == FALSE) {
                        // ...
                  return -1;
            } else
                        // ...

            Sleep(3000);

            status = PostThreadMessage( lpDaemonThreadID, MSG_SECOND, (WPARAM)NULL, (LPARAM)NULL );

            if (status == FALSE) {
                        // ...
                  return -1;
            } else
                        // ...

            Sleep(3000);

            status = PostThreadMessage( lpDaemonThreadID, MSG_THIRD, (WPARAM)NULL, (LPARAM)NULL );

            if (status == FALSE) {
                        // ...
                  return -1;
            } else
                        // ...

            Sleep(3000);

                ...

     ==============================================

    It is all right Compiling and linking.   Now i use this COM in VB. When i debug it in VB IDE, it is all right : all threads of COM work well, all events are fired well . Then i made a exe file. but when i ran the exe file, a exception was happened and exe were stopped. i don't know what's the matter?

is there anybody who has similiar experience?

any ideas or suggestions will be very appreciated.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 24 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 24 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros