c++ - IUnknown 中的 addref 和 release,它们实际上是做什么的?

标签 c++ c windows shell

我一直在努力思考 Windows 中的 shell 扩展。需要实现的一些函数是 addref() 和 release()。它说,它会跟踪对象引用并在不使用时释放它们。

简单解释一下,它实际跟踪的是什么?在我看来,你创建自己的对象,根据你的目的实现各种接口(interface),然后让 classfactory 将对象返回给 com 引擎运行,除非我弄错了。

我对这个概念的理解真的很慢。也是一步一步的过程,windows com 引擎加载 shell 扩展,从识别 dll 到实际执行到卸载。请做一些简单的解释。问候

最佳答案

Shell 扩展只是普通的 COM 对象。

接口(interface)(通常以大写字母 i 为前缀)基本上是一个契约。一个接口(interface)可以有一个或多个实现。

释放由对象/接口(interface)的“用户”在使用完后调用:

IWhatever*pW;
if (SUCCEEDED(CoCreateInstance(CLSID_Something, ..., IID_IWhatever, (void**) &pW)))
{
  pW->DoWhateverThisThingDoes();
  NotifyMyClients(pW);
  pW->Release(); // Tell this instance that we are done with it
}

在前面的例子中,我们调用 Release 来表明我们不再需要这个接口(interface),但我们实际上并不知道接口(interface)实例是否会立即被销毁。

如果我们想象一下 NotifyMyClients 已知的客户端/插件/扩展之一是这样实现的:

class FooClient {
IWhatever*MyWhatever;
void FooClient::OnNotifyNewWhatever(IWhatever*p) // Somehow called by NotifyMyClients
{
  p->AddRef(); // We want to use this object later even if everyone else are done with it
  if (MyWhatever) MyWhatever->Release(); // Throw away the previous instance if we had one
  MyWhatever = p;
  SetTimer(...); // Activate timer so we can interact with MyWhatever later
}
FooClient::FooClient()
{
  MyWhatever = 0; 
}
FooClient::~FooClient()
{
  if (MyWhatever) MyWhatever->Release(); 
}
};

创建对象的代码不需要知道其他代码对对象做了什么,它只负责自己与对象的交互。

基本规则是:每次在对象上调用 AddRef 时调用一次 Release。如果您创建了一个对象实例,您还必须释放它。

实现 STA 接口(interface)的伪代码可能如下所示:

#include <Whatever.h> // The skeleton/contract for IWhatever
class Whatever : public IWhatever {
ULONG refcount;
Whatever() : refcount(1) {} // Instance refcount should start at 1
HRESULT QueryInterface(...) { ... }
ULONG AddRef() { return ++refcount; }
ULONG Release()
{
  if (--refcount == 0) // Anyone still using me?
  {
    delete this; // Nope, I can destroy myself
    return 0;
  } 
  return refcount;
}
void DoWhateverThisThingDoes() { PerformMagic(this); }
};

此特定 IWhatever 实现 (CLSID_Something) 的 CLSID 必须在注册表中注册,以便 COM 可以找到它。注册包括代码所在的 .DLL 的路径。此 .DLL 必须导出 DllGetClassObject功能。

DllGetClassObject 分发其 IClassFactory 实现的一个实例,当 COM 请求一个新的 IWhatever 实例时,工厂将调用 new Whatever();

我没有介绍 QueryInterface 但它用于询问对象实例是否支持另一个接口(interface)。 ICar 可能实现了 IVehicle 但不是 IBus 也不是 ITrain 等。所有 COM 对象都支持 IUnknown 接口(interface)和所有其他接口(interface)继承自 IUnknown。

用一个答案解释 COM 是不可能的,但有很多 introduction articles online .

您可以使用/使用由 Microsoft 和第 3 方创建的 shell 对象,您可以创建自己的 documented interfaces 实现.

如果以 IContextMenu 为例。在一个系统上可以有很多实现。 Explorer 为每个已注册和适用(文件扩展名匹配注册等)IContextMenu 实现创建一个实例,当您右键单击某个内容时,每个实例都会将其菜单项添加到菜单中。再次调用添加所选菜单项的实例以执行其操作,然后释放所有实例。

MSDN 有一个最常用的扩展类型列表 here . The Complete Idiot's Guide to Writing Shell Extensions如果你想写一个,这是一个很好的起点。

关于c++ - IUnknown 中的 addref 和 release,它们实际上是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010172/

相关文章:

c++ - 如何分别为 std::istringstream 进行初始化和赋值并在以下情况下移动?

c++ - 运行时符号查找错误

c - 打印某些数组

c++ - ldap_set_option() 未设置选项 "LDAP_OPT_SSL"

c++ - 获取当前 CPU 利用率的方法在 Windows 10 上运行不可靠

c - 奇怪的LNK2001 : unresolved external symbol

c++ - 二叉搜索树的遍历

c++ - GCC -m32 标志 :/usr/bin/ld: skipping incompatible

C TCP套接字,发送文件的回显服务器,发送文件后挂断

c - 有符号字符 (C) 的位旋转?