简而言之,什么是 COM(组件对象模型)?

2022-01-14 00:00:00 com c++

似乎 COM 对象是由操作系统管理的通用对象.对象遵循严格的接口,允许您查询对象以确定信息.这就是 COM 对象吗?

It seems COM objects are general use objects which are governed by the OS. The objects follow a strict interface and allow you to query the objects to determine information. Is this what COM objects are?

推荐答案

COM 是一种机制允许重用对象(或者更确切地说是组件),独立于实现该组件的程序员和使用它的程序员使用的语言,并且独立于该组件是在客户端程序中还是在机器上的其他地方实现的(或网络).

COM is a mechanism that allows the re-use of objects (or rather components), independently of the languages used by the programmer who implemented the component and the programmer who uses it, and independently of whether the component was implemented in the client's program or elsewhere on the machine (or network).

广义上讲,每个 COM 组件都提供一个或多个接口的实现.这些接口是使用 接口定义语言 (IDL) 以与语言无关的方式定义的.例如,COM 中的一个基本接口 IUnknown 是这样定义的:

Broadly speaking, each COM component provides an implementation of one or more interfaces. Those interfaces are defined in a language-neutral manner using the Interface Definition Language (IDL). As an example, one of the fundamental interfaces in COM, IUnknown, is defined like this:

interface IUnknown
{
   virtual HRESULT QueryInterface(REFIID riid, void **ppvObject) = 0;
   virtual ULONG AddRef(void) = 0;
   virtual ULONG Release(void) = 0;
};

这个小接口是 COM 的基础,因为每个 COM 组件必须实现它.它定义了 COM 机制的两个重要方面:

This little interface is fundamental in COM, because each COM component must implement it. It defines two important aspects of the COM machinery:

  • QueryInterface 允许调用代码来获取已知接口的实现.在 COM 中,接口由 GUID(也称为接口标识符,IID)引用.如果一个对象实现了多个接口,这就是客户端代码获取对每个接口的引用的方式.如果您愿意,它可以充当一种类型转换运算符.
  • AddRef()Release() 实现了COM 对象的内存管理机制.顾名思义,最常见的模型是引用计数机制,即在最后一个客户端释放对实例的引用后销毁实例.
  • QueryInterface allows calling code to get an implementation for a known interface. In COM, interfaces are referenced by GUIDs (also known as Interface Identifiers, IID). If an object implements several interfaces, that's how client code gets a reference to each of those interfaces. It acts as a sort of casting operator, if you will.
  • AddRef() and Release() implement the memory management mechanism for COM objects. As their name suggests, the most common model is the reference counting mechanism, where an instance is destroyed after the last client has released its reference to it.

所有 COM 组件在安装时都会在系统中注册.如果程序员想要使用某个组件,他需要:

All COM components are registered with the system upon installation. If a programmer wants to use a certain component, he needs to:

  • 确保组件安装在可到达的位置.大多数时候它在运行应用程序的系统上,但 COM+ 也允许组件存在于远程计算机上.
  • 了解给定组件的 GUID.使用此 GUID,客户端可以请求系统实例化组件(在 C 中,执行此操作的函数称为 CoCreateInstance()).您可以在 HKEY_CLASSES_ROOTCLSID 下查看注册表:其中的每个 GUID(很可能)都有一个 COM 组件或接口的标识符,该键下方的条目告诉系统应该如何实例化它.
  • Make sure the component is installed at a reachable location. Most of the time it is on the system of the running application, but COM+ also allows components to exist on remote computers.
  • Know the GUID of the given component. With this GUID, the client can then ask the system to instantiate the component (in C, the function to do this is called CoCreateInstance()). You can look in the registry under HKEY_CLASSES_ROOTCLSID: each GUID in there is (most probably) an identifier for a COM component or interface, and the entries below that key tell the system how it should be instanciated.

COM 机制极其复杂.例如,在 C 中实现或使用 COM 组件需要大量的工作,但是像 Visual Basic 这样的高级语言在简化 COM 组件的实现和使用方面做了很多工作.然而,好处是非常真实的.它使得使用例如 Visual Basic 编写应用程序成为可能,但仍然可以将 C 或 C++ 中的性能关键算法实现为 COM 对象,这些对象可以直接从 VB 代码中使用.系统负责编组方法调用参数,根据需要通过线程、进程和网络连接传递它们,以便客户端代码具有使用普通对象的印象.

The COM machinery is extremely complex. For example, implementing or using COM components in C requires a horrendous amount of work, but higher-level languages like Visual Basic have done a lot to ease the implementation and use of COM components. The benefits are however very real. It makes it possible to write an application in, say, Visual Basic, but to still implement the performance-critical algorithms in C or C++ as COM objects, which can be used directly from the VB code. The system takes care of marshalling method-call arguments, passing them through threads, processes and network connections as needed so that the client code has the impression of using a normal object.

Windows 的许多基本部分都基于 COM.例如,Windows 资源管理器(文件管理器)基本上是一个空壳.它定义了一组用于导航和显示树层次结构的 COM 接口,所有实际显示我的电脑"、驱动器、文件夹和文件的代码都是一组实现这些接口的 COM 组件.

Many fundamental parts of Windows are based on COM. Windows Explorer (the file manager), for instance, is basically an empty shell. It defines a bunch of COM Interfaces for navigating and displaying tree hierarchies, and all the code that actually displays "My Computer", the drives, the folders and the files is as a set of COM components that implement those interfaces.

随着 .NET 的出现,COM 逐渐过时.

With the advent of .NET, COM is slowly becoming obsolete.

相关文章