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

怎甘沉沦 提交于 2019-12-02 20:45:59

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!