Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi COM objects multithreading

I've been programming for a while and regarding COM/ActiveX object, I'm facing very strange issues, that are abviously above my knowledge. Here it is. My software talks to COM objects using late binding. Since those COM object talk to hardware (such as scientific camera for instance), I have choosen to seralise all calls into a dedicated thread. this allows the main thread to interact with the user. So I'm sending messages from the main user thread (or any other thread) to the thread that is design to dealing solely with activeX.

Here how it looks



procedure MythreadActiveX.execute;
begin

  CoInitialize(nil);
  Try

  ComObject       :=CreateOLEObject(COMID);

  While not Terminated do
  Begin

  If PeekMessage(Msg,0,0,0,PM_REMOVE) then 
    Begin
     TranslateMessage(Msg); 
     DispatchMessage (Msg); 
    end;

    If (FEvent.WaitFor(TimOutMs)=wrSignaled) then   // Wait for command
    Begin

      FEvent.ResetEvent;

      Try

      Case COM_Order of
          Oder1:Begin
                 .........
                end    
          Oder2:Begin
                 .........
                end    
      end;

      FEventComplete.SetEvent;

    end;
   end; 

   CoUnInitialize;
end;

This works like a charm with most COM server, but fails with other COM DLL/Server, especially written in visual basic, where the I have noticed with process explorer that the ActiveX code is executed into the main thread despite what I did above ! The consequence result in - main thread holding up - main thread memory corruption (with large array for instance)... == my app crash

What is the cause ? is this related to ActiveX threading model ? I would like to understand and to correct my code to cope with that (In that case, the COM shall run in the main thread....)

Thanks (Since I spent time on this, i'm ready to provide more information in order to understand)

like image 757
Cyril CAVADORE Avatar asked Aug 21 '13 04:08

Cyril CAVADORE


1 Answers

Using CoInitializeEx(nil,COINIT_MULTITHREADED) is better than CoInitialize...because the COM object was dispatched into the main tread.

like image 169
Cyril CAVADORE Avatar answered Sep 28 '22 13:09

Cyril CAVADORE