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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With