Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I check to see if CoInitialize has been called or not?

In a multi-threaded environment with ADO database connections, I would like to know if CoInitialize has been called or not. How would I go about checking this?

like image 680
Jerry Dodge Avatar asked Jan 26 '13 23:01

Jerry Dodge


1 Answers

Normally you should not do this check and just call CoInitialize/CoUnInitialize pair. Still you can do it like this:

function IsCoInitialized: Boolean;
var
  HR: HResult;

begin
  HR:= CoInitialize(nil);
  Result:= (HR and $80000000 = 0) and (HR <> S_OK);
  if (HR and $80000000 = 0) then CoUnInitialize;
end;

There is no problem if you call CoInitialize more than once in a thread. The first call should return S_OK, all subsequent calls should return S_FALSE. All these calls are considered successful and should be paired by CoUnInitialize calls. If you called CoInitialize n times in a thread, only the last n-th CoUnInitialize call closes COM.

like image 151
kludg Avatar answered Sep 22 '22 08:09

kludg