名詞解釋:
coclass: 是component object class的縮寫,其中包含一個(gè)或者多個(gè)interface, coclass實(shí)現(xiàn)了這些接口;
COM object: 是coclass在內(nèi)存中的實(shí)例
COM server: 是一個(gè)二進(jìn)制文件(DLL 或者 Exe),其中包含一個(gè)或者多個(gè)coclass
Registration(注冊(cè)): 創(chuàng)建注冊(cè)表項(xiàng),告訴Windows到哪里尋找COM server的過程
Guid: 每個(gè)interface或者coclass都有一個(gè)Guid, 還會(huì)看到uuid, 跟Guid是一回事
class ID, CLSID: 用來命名一個(gè)coclass;
interface ID, IID: 用來命名一個(gè)interface;
HRESULT: 一個(gè)整型數(shù)值,用來返回成功或者錯(cuò)誤的代碼
COM Library: 是操作系統(tǒng)的一部分, 當(dāng)做與COM相關(guān)的事情的時(shí)候,與之交互
COM對(duì)象和標(biāo)準(zhǔn)Win32控件的區(qū)別
在使用標(biāo)準(zhǔn)win32控件的時(shí)候,首先要獲得這個(gè)控件的句柄(handle, HWND),然后用sendmessage給它發(fā)送一個(gè)消息來操控它;同樣,當(dāng)控件要通知你什么消息或者給你傳遞一些數(shù)據(jù)時(shí),它也要給你傳遞消息;
對(duì)于COM對(duì)象則不需要把消息傳來傳去.COM對(duì)象會(huì)給你一些特定的函數(shù)指針,你可以調(diào)用這些函數(shù)指針來操作COM對(duì)象;
COM對(duì)象和VTable
我們從一個(gè)簡(jiǎn)單的C的struct開始,我們定義一個(gè)struct:
struct IExample
{
DWORD count;
char buffer[80];
};
再用typedef來簡(jiǎn)化一下:
typedef struct
{
DWORD count;
char buffer[80];
} IEXample;
接下來,我們就可以使用這個(gè)struct了:
IExample* example;
example = (IExample*)GlobalAlloc(GMEM_FIXED, sizeof(IEXample));
example->count = 1;
example->buffer[0] = 0;
然后我們知道,struct中是可以包含函數(shù)指針的,假設(shè)我們現(xiàn)在有一個(gè)函數(shù),這個(gè)函數(shù)有個(gè)字符指針的參數(shù),返回值是long類型:
long SetString(char * str)
{
return (0);
}
這個(gè)時(shí)候我們就可以得到類似這樣的代碼:
#include <windows.h> typedef long SetStringPtr(char *); typedef struct { SetStringPtr* SetString; DWORD count; char buffer[80]; } IExample; long SetString(char *str) { return (0); } int _tmain(int argc, _TCHAR* argv[]) { IExample* example; example = (IExample*)GlobalAlloc(GMEM_FIXED, sizeof(IExample)); example->SetString = SetString; example->buffer[0] = 0; example->count = 1; long value = example->SetString("this is a test!"); return 0; }
但是,假如我們現(xiàn)在不想把函數(shù)指針直接存放在IExample內(nèi), 我們想要有一組函數(shù)指針.我們可以定義另一個(gè)struct,它的唯一的目的就是存放我們的函數(shù)指針,我們的代碼就成了這個(gè)樣子:
#include <windows.h> typedef long SetStringPtr(char *); typedef long GetStringPtr(char*, long); typedef struct { SetStringPtr* SetString; GetStringPtr* GetString; } IExampleVtbl; typedef struct { IExampleVtbl* lpVtbl; DWORD count; char buffer[80]; } IExample; long SetString(char *str) { return (0); } long GetString(char* str, long len) { return 0; } static IExampleVtbl IExample_Vtbl = {SetString, GetString}; int _tmain(int argc, _TCHAR* argv[]) { IExample* example; example = (IExample*)GlobalAlloc(GMEM_FIXED, sizeof(IExample)); example->lpVtbl = &IExample_Vtbl; example->buffer[0] = 0; example->count = 1; long value = example->lpVtbl->SetString("this is a test!"); return 0; }
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
