8
我想在線程中使用COM接口。從我讀過的內容來看,我必須在每個線程中調用CoInitialize/CoUninitialize
。TThread和COM - 「CoInitialize尚未被調用」,雖然CoInitialize在構造函數中調用
雖然這是工作的罰款:
procedure TThreadedJob.Execute;
begin
CoInitialize(nil);
// some COM stuff
CoUninitialize;
end;
當我移動到構造函數的調用和析構函數:
TThreadedJob = class(TThread)
...
protected
procedure Execute; override;
public
constructor Create;
destructor Destroy; override;
...
constructor TThreadedJob.Create;
begin
inherited Create(True);
CoInitialize(nil);
end;
destructor TThreadedJob.Destroy;
begin
CoUninitialize;
inherited;
end;
procedure TThreadedJob.Execute;
begin
// some COM stuff
end;
我得到EOleException:CoInitialize的沒有被調用例外,我沒有線索爲什麼。
謝謝你閃電般的快速回答。 – forsajt