|
COM組件設(shè)計(jì)與應(yīng)用(八) 一、前言 二、接口結(jié)構(gòu) 1 import "oaidl.idl";
2 import "ocidl.idl";
3 [
4 object,
5 uuid(072EA6CA-7D08-4E7E-B2B7-B2FB0B875595),
6 helpstring("IMathe Interface"),
7 pointer_default(unique)
8 ]
9 interface IMathe : IUnknown
10 {
11 [helpstring("method Add")] HRESULT Add([in] long n1, [in] long n2, [out,retval] long *pnVal);
12 };
13 [
14 uuid(CD7672F7-C0B4-4090-A2F8-234C0062F42C),
15 version(1.0),
16 helpstring("Simple3 1.0 Type Library")
17 ]
18 library SIMPLE3Lib
19 {
20 importlib("stdole32.tlb");
21 importlib("stdole2.tlb");
22 [
23 uuid(C6F241E2-43F6-4449-A024-B7340553221E),
24 helpstring("Mathe Class")
25 ]
26 coclass Mathe
27 {
28 [default] interface IMathe;
29 };
30 };
3-3、手工修改IDL文件,黑體字部分是手工輸入的。完成后保存。 import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(072EA6CA-7D08-4E7E-B2B7-B2FB0B875595),
helpstring("IMathe Interface"),
pointer_default(unique)
]
interface IMathe : IUnknown
{
[helpstring("method Add")] HRESULT Add([in] long n1, [in] long n2, [out,retval] long *pnVal);
};
[ // 所謂手工輸入,其實(shí)也是有技巧的:把上面的接口描述(IMathe)復(fù)制、粘貼下來(lái),然后再改更方便哈
object,
uuid(072EA6CB-7D08-4E7E-B2B7-B2FB0B875595), // 手工或用工具產(chǎn)生的 IID
helpstring("IStr Interface"),
pointer_default(unique)
]
interface IStr : IUnknown
{
// 目前還沒有任何接口函數(shù)
};
[
uuid(CD7672F7-C0B4-4090-A2F8-234C0062F42C),
version(1.0),
helpstring("Simple3 1.0 Type Library")
]
library SIMPLE3Lib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
uuid(C6F241E2-43F6-4449-A024-B7340553221E),
helpstring("Mathe Class")
]
coclass Mathe
{
[default] interface IMathe;
interface IStr; // 別忘了呦,這里還有一個(gè)那
};
};
3-4、打開頭文件(Mathe.h),手工增加類的派生關(guān)系和接口入口表 ,然后保存。
class ATL_NO_VTABLE CMathe :
public CComObjectRootEx <CComSingleThreadModel>,
public CComCoClass <CMathe, &CLSID_Mathe>,
public IMathe, // 別忘了,這里加一個(gè)逗號(hào)
public IStr // 增加一個(gè)基類
{
public:
CMathe()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_MATHE)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CMathe) // 接口入口表。這里填寫的接口,才能被QueryInterface()找到
COM_INTERFACE_ENTRY(IMathe)
COM_INTERFACE_ENTRY(IStr)
END_COM_MAP()
3-5、好了,一切就緒。接下來(lái),就可以在 IStr 接口中增加函數(shù)了。示例程序中增加一個(gè)字符串連接功能的函數(shù):HRESULT Cat([in] BSTR s1, [in] BSTR s2, [out,retval] BSTR *psVal); 如果你不知道如何做,請(qǐng)重新閱讀前三回的內(nèi)容。 四、接口升級(jí) 我們這個(gè)組件已經(jīng)發(fā)行了,但忽然一天我們需要在 IMathe 接口上再增加一個(gè)函數(shù)......不行!絕對(duì)不能在 IMathe 上直接修改!怎么辦?解決方法是------再增加一個(gè)接口,我們就叫 IMathe2 吧,如果以后還要增加函數(shù),那么我們?cè)僭黾右粋€(gè)接口叫 IMathe3......子子孫孫,無(wú)窮盡也。 4-1、修改 IDL 文件,其實(shí)如果你理解了上面一小節(jié)的內(nèi)容,再增加一個(gè)接口是很簡(jiǎn)單的事情了。 import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(072EA6CA-7D08-4E7E-B2B7-B2FB0B875595),
helpstring("IMathe Interface"),
pointer_default(unique)
]
interface IMathe : IUnknown
{
[helpstring("method Add")] HRESULT Add([in] long n1, [in] long n2, [out,retval] long *pnVal);
};
[
object,
uuid(072EA6CB-7D08-4E7E-B2B7-B2FB0B875595),
helpstring("IStr Interface"),
pointer_default(unique)
]
interface IStr : IUnknown
{
[helpstring("method Cat")] HRESULT Cat([in] BSTR s1, [in] BSTR s2, [out,retval] BSTR *psVal);
};
[
object,
uuid(072EA6CC-7D08-4E7E-B2B7-B2FB0B875595),
helpstring("IMathe2 Interface"),
pointer_default(unique)
]
interface IMathe2 : IUnknown
{ // 下面這個(gè)Add()函數(shù),只有IDL中的聲明,而不用增加任何程序代碼,因?yàn)檫@個(gè)函數(shù)早在 IMathe 中就已經(jīng)實(shí)現(xiàn)了
[helpstring("method Add")] HRESULT Add([in] long n1, [in] long n2, [out,retval] long *pnVal);
};
[
uuid(CD7672F7-C0B4-4090-A2F8-234C0062F42C),
version(1.0),
helpstring("Simple3 1.0 Type Library")
]
library SIMPLE3Lib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
uuid(C6F241E2-43F6-4449-A024-B7340553221E),
helpstring("Mathe Class")
]
coclass Mathe
{
[default] interface IMathe;
interface IStr;
interface IMathe2; // 別忘了,這里還有一行呢!
};
};
4-2、打開頭文件,增加派生關(guān)系和接口入口表
class ATL_NO_VTABLE CMathe :
public CComObjectRootEx <CComSingleThreadModel>,
public CComCoClass <CMathe, &CLSID_Mathe>,
public IMathe,
public IStr, // 這里增加一個(gè)逗號(hào)
public IMathe2
{
public:
CMathe()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_MATHE)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CMathe)
COM_INTERFACE_ENTRY(IMathe)
COM_INTERFACE_ENTRY(IStr)
COM_INTERFACE_ENTRY(IMathe2)
END_COM_MAP()
4-3、示例程序中,增加了一個(gè)整數(shù)乘法函數(shù):HRESULT Mul([in] long n1, [in] long n2, [out,retval] long *pnVal); 如果你不知道如何做,那就別學(xué)了:-( 都講好幾遍了,怎么還不掌握呢?知道狗熊是怎么死的嗎?(注5) 4-4、因?yàn)槲覀兊慕M件升級(jí)了,于是也應(yīng)該修改版本號(hào)了(這不是必須的,但最好修改一下)。打開注冊(cè)表文件(.rgs) 把有關(guān)ProgID的版本 "Mathe.1" 修改為"Mathe.2"。另外如果你愿意,把IDL文件中的 version 和提示文字一并修改一下。這里就不再粘貼文件內(nèi)容了,因?yàn)楹芎?jiǎn)單,大家下載示例程序(注6)后,自己看吧。 五、小結(jié) 為祖國(guó)的軟件事業(yè)而奮斗! 下回書介紹“自動(dòng)化”--- IDispatch 接口,好玩的很!謝謝關(guān)注:-) 注1: 黑猩猩的瞬時(shí)記憶量是3,人類的瞬時(shí)記憶量是7??茖W(xué)家做過(guò)實(shí)驗(yàn),當(dāng)著面,把一塊糖扣在3個(gè)碗的其中之一,黑猩猩能立刻準(zhǔn)確找到,但如果超過(guò)3個(gè)碗,猩猩就暈了......如果給你看一串?dāng)?shù)字,然后立刻讓你說(shuō)出來(lái),一般的人只會(huì)記得其中的7個(gè)。 注2:組件一經(jīng)發(fā)表,就不要修改已有接口。這樣軟件的升級(jí)才能做到“魯棒”性。 注3:guidgen.exe 工具,在《COM 組件設(shè)計(jì)與應(yīng)用(二)》中已經(jīng)介紹。 注4:組件函數(shù)對(duì)內(nèi)存指針的處理,以后有專門的章回討論。 注5:笨死的! 注6:示例程序有兩部分,分別是vc6.0版本和vc.net 2003 版本。 |
|
|