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

后端 未结 1 1163
孤街浪徒
孤街浪徒 2021-02-02 13:47

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?

1条回答
  •  情话喂你
    2021-02-02 14:22

    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.

    0 讨论(0)
提交回复
热议问题