Unable to create COM object in thread lazarus / 34-Windows ---------------------------------------------------------------------- Eclipticon · 2012-09-02 18:08 Hi, to access a COM object not only in my program's main thread but also from a sub thread, I would like to create a new instance of it. Unfortunately, creation of this new instance fails with a CO_E_OBJNOTCONNECTED error (see comments in code below). Can anyone hint me on what I am doing wrong? (Sorry, I can not give too many details about this COM object or code, as this is subject to a non-disclosure agreement!) Thanks, Paul Code: [Select]unit threadtest2main; {$mode objfpc}{$H+} interface uses   Classes, Windows, SysUtils, FileUtil, Forms, Controls, Graphics,   Instrument_TLB, Dialogs, StdCtrls, comobj, ActiveX; type   TTestThread = class(TThread)   private     SubThreadInstrument: Instrument;   protected     procedure Execute; override;   end; type   TForm1 = class(TForm)     Button1: TButton;     Label1: TLabel;     procedure Button1Click(Sender: TObject);     procedure FormCreate(Sender: TObject);     procedure FormDestroy(Sender: TObject);   private     MainThreadInstrument: Instrument;   end; var   Form1: TForm1;   ResultText: string; implementation {$R *.lfm} procedure TTestThread.Execute; var   Success: HRESULT; begin   Success := CoInitializeEx(nil, COINIT_MULTITHREADED); // Tried with COINIT_APARTMENTTHREADED, too   if (Success <> S_OK) or (Success <> S_FALSE) then     Exception.Create('Not s_ok or s_false');   SubThreadInstrument := ComInstrument.Create;  // Calls CreateComObject();   // Code raises an EOleSysError here: CoCreateInstance() returns CO_E_OBJNOTCONNECTED   if SubThreadInstrument.SomeStatus then     ResultText := ResultText + 'T'   else     ResultText := ResultText + 't';   FreeAndNil(SubThreadInstrument);   CoUninitialize; end; procedure TForm1.FormCreate(Sender: TObject); begin   MainThreadInstrument := ComInstrument.Create; // Calls CreateComObject();   // Creating a second instance of Instrument in the main thread works well end; procedure TForm1.Button1Click(Sender: TObject); var   ATestThread: TTestThread;   Success: HRESULT; begin   Success := CoInitializeEx(nil, COINIT_MULTITHREADED);   if (Success <> S_OK) or (Success <> S_FALSE) then     Exception.Create('Not s_ok or s_false');   ATestThread := TTestThread.Create(False);   if MainThreadInstrument.SomeStatus then     ResultText := ResultText + 'B'   else     ResultText := ResultText + 'b';   ATestThread.WaitFor;   if Assigned(ATestThread.FatalException) then     raise Exception.Create(Exception(ATestThread.FatalException).Message);   FreeAndNil(ATestThread);   Label1.Caption := ResultText; end; procedure TForm1.FormDestroy(Sender: TObject); begin   FreeAndNil(MainThreadInstrument); end; end. ---------------------------------------------------------------------- ludob · 2012-09-03 09:22 Comobj does already a CoInitialize in its initialization section which implies STA.  Try calling CoUninitialize in FormCreate followed by CoInitializeEx(nil,COINIT_APARTMENTTHREADED) ---------------------------------------------------------------------- Eclipticon · 2012-09-03 19:18 Hi ludob, thanks for the tip, unfortunately it did not do the job. I am wondering though, isn't COINIT_APARTMENTTHREADED setting it to STA? And shouldn't my CoInitializeEx return something different from S_OK and S_FALSE if I specify a contradictory threading model? Thanks! ---------------------------------------------------------------------- Eclipticon · 2012-09-03 19:38 Not added in proof: that should be twice Code: [Select]if (Success <> S_OK) AND (Success <> S_FALSE) then of course ...