什么是COM(组件对象模型)? [英] What is COM (Component Object Model) in a nutshell?

查看:168
本文介绍了什么是COM(组件对象模型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎COM对象是由OS控制的通用对象。对象遵循严格的接口,并允许您查询对象以确定信息。这是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:


  • QueryInterace 允许调用代码获得已知接口的实现。在COM中,接口由GUID(也称为接口标识符,IID)引用。如果一个对象实现了几个接口,这就是客户端代码如何获取对这些接口的引用。

  • AddRef() Release() 实现COM对象的内存管理机制。顾名思义,最常见的模型是引用计数机制,其中一个实例在最后一个客户端释放其引用后被销毁。

  • QueryInterace 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_ROOT\CLSID :每个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_ROOT\CLSID: 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资源管理器(文件管理器)基本上是一个空shell。它定义了一组用于导航和显示树层次结构的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.

这篇关于什么是COM(组件对象模型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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