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?
在具有ADO数据库连接的多线程环境中,我想知道是否已调用CoInitialize。我该如何检查?
1 个解决方案
#1
9
Normally you should not do this check and just call CoInitialize
/CoUnInitialize
pair. Still you can do it like this:
通常你不应该做这个检查,只需要调用CoInitialize / CoUnInitialize对。你仍然可以这样做:
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.
如果在线程中多次调用CoInitialize,则没有问题。第一个调用应返回S_OK,所有后续调用应返回S_FALSE。所有这些调用都被认为是成功的,应该通过CoUnInitialize调用进行配对。如果您在一个线程中调用了CoInitialize n次,则只有最后一个第N个CoUnInitialize调用会关闭COM。
#1
9
Normally you should not do this check and just call CoInitialize
/CoUnInitialize
pair. Still you can do it like this:
通常你不应该做这个检查,只需要调用CoInitialize / CoUnInitialize对。你仍然可以这样做:
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.
如果在线程中多次调用CoInitialize,则没有问题。第一个调用应返回S_OK,所有后续调用应返回S_FALSE。所有这些调用都被认为是成功的,应该通过CoUnInitialize调用进行配对。如果您在一个线程中调用了CoInitialize n次,则只有最后一个第N个CoUnInitialize调用会关闭COM。