gpt4 book ai didi

delphi - 是否有一些已知的单元在使用 COM 库时自动初始化它?

转载 作者:行者123 更新时间:2023-12-02 15:32:16 26 4
gpt4 key购买 nike

我做了一个自定义控件,需要使用API​​ DoDragDrop功能,他们说我必须打电话 OleInitialize在调用该函数之前。

我在 MyControl.Create 中这样做方法,但我得到 S_FALSE结果,这意味着COM库已经初始化。但谁来初始化它呢?

我之前没有在我的应用程序中初始化它。我在想,也许有一些delphi单元在他的initialization中初始化它部分...第二个问题是我应该做什么,我应该继续初始化 MyControl.Create 中的 COM 库吗?方法还是我根本不应该初始化(因为它已经初始化了)?

最佳答案

正如我的 answer 中所述类似的问题:

In the RTL/VCL source, COM is initialized in the following ways:

  1. By a call to OleInitialize made from Forms.TApplication.Create. So this call will be made for all VCL forms applications, but not, for example, for service applications.
  2. By a call to CoInitialize or CoInitializeEx in ComObj.InitComObj. This is registered as an InitProc in the initialization section of the ComObj unit. In turn, the call to Application.Initialize in your project .dpr file's code will invoke ComObj.InitComObj.
  3. In many and various other locations around the RTL/VCL. Including, but not limited to, Datasnap, ComServ, Soap, System.Win.Sensors, Winapi.DirectShow9. Some of these areas of code are more recent than Delphi 7.

Now, of these various COM initializations, the ones that count are 1 and 2. In any standard VCL forms application, both of these will run at startup in the main thread. Item 1 runs first and so gets to initialize COM first. That's the initialization that counts. Item 2 runs after and returns S_FALSE meaning that COM was already initialized.

所有 VCL 应用程序都包含 Forms 单元,因此您可以确定 COM 是在主 VCL 线程中初始化的。对于其他线程,如果需要,您需要初始化 COM。

<小时/>

您绝不能在控制中初始化 COM,或更一般地说,初始化您无法控制的线程。如果这样做,那么您的初始化可能会与线程所有者指定的单元类型发生冲突。

关于delphi - 是否有一些已知的单元在使用 COM 库时自动初始化它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31341795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com