如何识别已经调用了多少个CoInitialize? [英] How to identify how many levels of CoInitialize have been called?

查看:98
本文介绍了如何识别已经调用了多少个CoInitialize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对一个杂乱的项目进行调试,而这个项目是在以前的开发人员不知道他们在做什么的情况下进行的,主要问题是尝试对应用程序进行多线程处理失败。我现在正在清理混乱,并试图找出问题出在哪里。问题之一是为了使用ADO组件,对 CoInitialize 的调用不一致。

I'm doing some debugging on a messy project I picked up where the previous developer didn't know what they were doing, and the main issue is failed attempts to multi-thread the application. I'm now cleaning up the mess and trying to figure out where things are going wrong. One of the issues is inconsistent calls to CoInitialize in order to use ADO components.

续我的上一个问题,我如何识别

例如,请考虑以下代码:

For example, take this code into consideration:

CoInitialize(nil);
try
  CoInitialize(nil);
  try
    //2 levels have been called, how to programatically check this?
  finally
    CoUninitialize;
  end;
finally
  CoUninitialize;
end;


推荐答案

您可以这样做:

function CoInitializeCount: Integer;
var
  HR: HResult;
  I: Integer;

begin
  Result:= 0;
  repeat
    HR:= CoInitialize(nil);
    if (HR and $80000000 <> 0) then begin
      Result:= -1;
      Exit;
    end;
    CoUnInitialize;
    if (HR <> S_OK) then begin
      CoUnInitialize;
      Inc(Result);
    end
    else Break;
  until False;
  for I:= 0 to Result - 1 do
    CoInitialize(nil);
end;






警告!上面的函数关闭了COM,它不能在COM应用程序中使用,只能在调试时回答特定的问题。


Warning! Since the above function closes COM it cannot be used in COM applications, only to answer the particular question while debugging.

这篇关于如何识别已经调用了多少个CoInitialize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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