[中英对照] COM的简明C教程(一)

来源:互联网 发布:java编码格式转换 编辑:程序博客网 时间:2024/05/18 18:43

[中英对照] COM的简明C教程(一)

 

 

Jeff Glatt(著),TOMGGX(译)

 

[ 摘要 ]

    现在有许多示范如何使用、创建COM、OLE、ActiveX组件的例程。但这些典型例子都是使用微软基础类(MFC),.NET,C#,WTL或至少使用了ATL的,由于此类架构使用 “预制” 和 “封装” 机制来给你提供一些样板代码,很不幸地,这些架构往往对程序员隐藏了所有的底层细节,以至你绝对不会真正的学到究竟是如何使用COM组件的每个细节。如果你试图使用纯C程序(不使用 MFC,WTL,.NET,ATL,C#,甚至不使用任何C++代码),那么,关于如何在纯C中处理COM对象的例子和资料是较为缺乏的。本文是试图尝试在纯C中使用COM而不使用其他编程架构的系列文章的第一部分。

 

[ ABSTRACT ]

    There are numerous examples that demonstrate how to use/create COM/OLE/ActiveX components. But these examples typically use Microsoft Foundation Classes (MFC), .NET, C#, WTL, or at least ATL, because those frameworks have pre-fabricated "wrappers" to give you some boilerplate code. Unfortunately, these frameworks tend to hide all of the low level details from a programmer, so you never really do learn how to use COM components per se. Rather, you learn how to use a particular framework riding on top of COM.

If you're trying to use plain C, without MFC, WTL, .NET, ATL, C#, or even any C++ code at all, then there is a dearth of examples and information on how to deal with COM objects. This is the first in a series of articles that will examine how to utilize COM in plain C, without any frameworks.

 

www.codeproject.com/KB/COM/com_in_c1.aspx

 

 

[英文文章标题] COM IN PLAIN C
[中文文章标题] COM的简明C教程

[中文文章标题] COM的简明C教程

 

Contents(内容)

  • A COM object and its VTable (COM 对象及其虚函数表)
  • A GUID (全局统一标识符)
  • QueryInterface(), AddRef(), and Release() (接口查询函数,引用计数器自增,自减)
  • An IClassFactory object (IclassFactory 对象)
  • Packaging into a DLL (封装到DLL中去)
  • Our C++/C include file (我们需要在C++/C中包括的文件)
  • The Definition (DEF) file (预定义文件)
  • Install the DLL, and register the object (安装DLL,并注册对象)
  • An example C program (一个C例程)
  • An example C++ program (一个C++例程)
  • Modifying the code (更改代码)
  • What's next? (我们的下一步)
  •  

    Introduction(介绍)

     

    现在有许多示范如何使用、创建COM、OLE、ActiveX组件的例程。但这些典型例子都是使用微软基础类(MFC),.NET,C#,WTL或至少使用了ATL的,由于此类架构使用 “预制” 和 “封装” 机制来给你提供一些样板代码,很不幸地,这些架构往往对程序员隐藏了所有的底层细节,以至你绝对不会真正的学到究竟是如何使用COM组件的每个细节。如果你试图使用纯C程序(不使用 MFC,WTL,.NET,ATL,C#,甚至不使用任何C++代码),那么,关于如何在纯C中处理COM对象的例子和资料是较为缺乏的。本文是试图尝试在纯C中使用COM而不使用其他编程架构的系列文章的第一部分。

    There are numerous examples that demonstrate how to use/create COM/OLE/ActiveX components. But these examples typically use Microsoft Foundation Classes (MFC), .NET, C#, WTL, or at least ATL, because those frameworks have pre-fabricated "wrappers" to give you some boilerplate code. Unfortunately, these frameworks tend to hide all of the low level details from a programmer, so you never really do learn how to use COM components per se. Rather, you learn how to use a particular framework riding on top of COM.

    If you're trying to use plain C, without MFC, WTL, .NET, ATL, C#, or even any C++ code at all, then there is a dearth of examples and information on how to deal with COM objects. This is the first in a series of articles that will examine how to utilize COM in plain C, without any frameworks.

    在如Static, Edit, Listbox, Combobox等等标准Win32控件中,为了操作此类控件,你获得一个句柄以便控制它们(比如一个HWND)并通过该句柄发送消息(通过 SendMessage函数).同样,当控件需要通知你或给你提供一些数据时,它便通过发送消息给你提供反馈(比如把消息放入你自己的消息队列中,你通过调用GetMessage函数获得反馈的消息)

    With standard Win32 controls such as a Static, Edit, Listbox, Combobox, etc., you obtain a handle to the control (i.e., an HWND) and pass messages (via SendMessage) to it in order to manipulate it. Also, the control passes messages back to you (i.e., by putting them in your own message queue, and you fetch them with GetMessage) when it wants to inform you of something or give you some data.

    在OLE/COM对象中,过程却不是这样的.你不能来回地发送消息.COM对象为了你可以调用和操作该对象,给了你一些指向特定函数的指针.比如,一个IE浏览器对象会给你一个函数指针,通过调用其指向的函数,你可以让浏览器在你的窗口中载入并显示一个网页.一个Office对象会给你另一个函数指针,同样地, 通过调用其指向的函数,你可以载入一个Office文档.如果COM对象需要通知你一些事情或给你一些数据,那么,你就要在你的程序中写一些特定的函数,并提供指向这些函数的指针(被COM对象使用),以便COM对象可以在需要的时候调用这些函数.换句话说,你需要在你的程序中创建你自己的COM对象.(以下)对于C的大部分真正讨论包括定义你自己的COM对象.为了达到这个目的,你需要知道关于COM对象的微小细节—被 “ 预制 ” 框架填入而又对你隐藏的部分,我们将在本系列探索它们.

    Not so with an OLE/COM object. You don't pass messages back and forth. Instead, the COM object gives you some pointers to certain functions that you can call to manipulate the object. For example, one of Internet Explorer's objects will give you a pointer to a function you can call to cause the browser to load and display a web page in one of your windows. One of Office's objects will give you a pointer to a function you can call to load a document. And if the COM object needs to notify you of something or pass data to you, then you will be required to write certain functions in your program, and provide (to the COM object) pointers to those functions so the object can call those functions when needed. In other words, you need to create your own COM object(s) inside your program. Most of the real hassle in C will involve defining your own COM object. To do this, you'll need to know the minute details about a COM object -- stuff that most of the pre-fabricated frameworks hide from you, but which we'll examine in this series.

    在最后,你调用COM对象中的函数操作它,它调用你的程序中的函数来通知你,给你发送数据或与你的程序通过某种方式进行交互.这个设计类似调用DLL中的函数,但仿佛DLL也可以调用你C程序中的函数 — 有点像 “ 回调 ” .但不同于DLL的是,你不可以使用LoadLibrary() 和 GetProcAddress() 来获得指向COM对象函数的指针.但是我们会发现,你可以使用操作系统提供的一个不同的函数来获得指向一个对象的指针然后使用这个对象来获得指向它的函数的指针.

    In conclusion, you call functions in the COM object to manipulate it, and it calls functions in your program to notify you of things or pass you data or interact with your program in some way. This scheme is analogous to calling functions in a DLL, but as if the DLL is also able to call functions inside your C program -- sort of like with a "callback". But unlike with a DLL, you don't use LoadLibrary() and GetProcAddress() to obtain the pointers to the COM object's functions. As we'll soon discover, you instead use a different operating system function to get a pointer to an object, and then use that object to obtain pointers to its functions.

    A COM object and its Vtable(COM对象及其虚函数表)

     

    在我们开始学习如何使用COM对象之前,我们需要先学习一下什么是COM对象.学习的最好的方法是创建一个我们自己的COM对象.但在我们做这件事情之前,让我们对C程序中结构体类型做一个(小小的)测试.作为一名C程序员,你需要相当清楚地熟悉结构体.这里有一个用于示范的结构体的范例定义,该结构体有两个成员 —— 一个DWORD(通过名为count的成员访问),和一个包括80个字符的数组(通过名为buffer的成员访问).

    Before we can learn how to use a COM object, we first need to learn what it is. And the best way to do that is to create our own COM object.But before we do that, let's examine a C struct data type. As a C programmer, you should be quite familiar with struct. Here's an example definition of a simple struct (called "IExample") that contains two members -- a DWORD (accessed via the member name "count"), and an 80 char array (accessed via the member name "buffer").

     

    struct IExample {
       DWORD   count;
       char    buffer[80];
    }; 

    原创粉丝点击