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 };
1-2引入 IUnknown 和ATL已經定義的其它接口描述文件。import 類似與 C 語言中的 #include3-12一個接口的完整描述4object 表示本塊描述的是一個接口。IDL文件是借用了PRC遠程數據交換格式的說明方法5uuid(......) 接口的 IID,這個值是 ATL 自動生成的,可以手工修改或用 guidgen.exe 產生(注3)6在某些軟件或工具中,能看到這個提示7定義接口函數中參數所使用指針的默認屬性(注4)9接口叫 IMathe 派生自 IUnknown,于是 IMathe 接口的頭三個函數一定就是QueryInterface,AddRef和Release10-12接口函數列表13-30類型庫的完整描述(類型庫的概念以后再說),下面所說明的行,是需要先了解的18#import 時候的默認命名空間23組件的 CLSID,CoCreateInstance()的第一個參數就是它27-29接口列表28[default]表示誰提供了IUnknown接口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);
};
[ // 所謂手工輸入,其實也是有技巧的:把上面的接口描述(IMathe)復制、粘貼下來,然后再改更方便哈
object, uuid(072EA6CB-7D08-4E7E-B2B7-B2FB0B875595),
// 手工或用工具產生的 IID
helpstring("IStr Interface"),
pointer_default(unique)
]
interface IStr : IUnknown
{
// 目前還沒有任何接口函數 }; [ 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; // 別忘了呦,這里還有一個那
};
};
class ATL_NO_VTABLE CMathe :
public CComObjectRootEx <CComSingleThreadModel>,
public CComCoClass <CMathe, &CLSID_Mathe>,
public IMathe, // 別忘了,這里加一個逗號
public IStr // 增加一個基類
{
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()
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
{ // 下面這個Add()函數,只有IDL中的聲明,而不用增加任何程序代碼,因為這個函數早在 IMathe 中就已經實現了
[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、打開頭文件,增加派生關系和接口入口表class ATL_NO_VTABLE CMathe :
public CComObjectRootEx <CComSingleThreadModel>,
public CComCoClass <CMathe, &CLSID_Mathe>,
public IMathe,
public IStr, // 這里增加一個逗號
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()
HRESULT Mul([in] long n1, [in] long n2, [out,retval] long *pnVal);
新聞熱點
疑難解答