可以查看CoInitialize是否已被调用? [英] Can I check to see if CoInitialize has been called or not?

查看:413
本文介绍了可以查看CoInitialize是否已被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在具有ADO数据库连接的多线程环境中,我想知道CoInitialize是否已被调用。

解决方案

通常您不应该执行此检查,只需调用 CoInitialize / CoUnInitialize pair。仍然可以这样做:

  function IsCoInitialized:Boolean; 
var
HR:HResult;

begin
HR:= CoInitialize(nil);
结果:=(HR和$ 80000000 = 0)和(HR S_OK);
如果(HR和$ 80000000 = 0)则CoUnInitialize;
结束






如果您调用 CoInitialize 在线程中多次。第一个电话应该返回 S_OK ,所有后续的电话都应该返回 S_FALSE 。所有这些呼叫都被认为是成功的,应该通过 CoUnInitialize 调用配对。如果您在一个线程中调用了 CoInitialize n次,则只有最后一个第n个 CoUnInitialize 调用关闭COM。


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.

这篇关于可以查看CoInitialize是否已被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆