COM 的全称是 Component Object Model 组件对象模型。
1、 COM基本知识
1.1 返回值HRESULT
COM要求所有的方法都会返回一个HRESULT类型的错误号。
1.2 初识 IDL
每个标准的COM组件都需要一个接口定义文件,文件的扩展名为
作者:帅后炮
链接:https://www.zhihu.com/question/53350764/answer/134594746
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
让我们看IUnknow接口的定义文件是怎样的。
- [
- local,
- object,
- uuid(00000000-0000-0000-C000-000000000046),
- pointer_default(unique)
- ]
-
- interface IUnknown
- {
- typedef [unique] IUnknown *LPUNKNOWN;
-
- cpp_quote("//")
- cpp_quote("// IID_IUnknown and all other system IIDs are provided in UUID.LIB")
- cpp_quote("// Link that library in with your proxies, clients and servers")
- cpp_quote("//")
-
- HRESULT QueryInterface(
- [in] REFIID riid,
- [out, iid_is(riid)] void **ppvObject);
- ULONG AddRef();
- ULONG Release();
- }
-
- [local]属性禁止产生网络代码。
- [object]属性是表明定义的是一个COM接口,而不是DEC风格的接口。
- [uuid]属性给接口一个GUID。
- [unique]属性表明null(空)指针为一个合法的参数值。
- [pointer_defaul]属性所有的内嵌指针指定一个默认指针属性
- typedef [unique] IUnknown *LPUNKNOWN;这是一个类型定义
- cpp_quote这个比较有趣,这是一个在idl文件写注解的方法。这些注解将保存到***.h和***_i.c文件中
- [in]表示这个参数是入参
- [out]表示这个参数是出参
- [iid_is(riid)]表示这个参数需要前一个的riid 参数。
1.3 IUnkown接口
在整个例子除了IUnkown这个东西,其他应该不会感到陌生吧!COM要求(最基本的要求)所有的接口都需要从IUnknown接口直接或间接继承,所以IUnknown接口有"万恶之源"之称。
IUnkown接口定义了三个方法。
- HRESULT QueryInterface([in] REFIID riid,[out] void **ppv);
- ULONG AddRef();
- ULONG Release();
其中
和Release()负责对象引用计数用的,而 QueryInterface()方法是用于查询所实现接口用的。每当COM组件被引用一次就应调用一次AddRef()方法。而当客户端在释放COM组件的某个接口时就需要调用Release()方法。
这里所讲的请在下面的例子仔细体会。
2、一个比较简单的COM
此例子共有四个文件组成:
文件名说明Interface.h接口类定义文件Math.h和Math.cpp实现类文件Simple.cpp 主函数文件这里用来当作COM的客户端
2.1 interface.h 文件
- #ifndef INTERFACE_H
- #define INTERFACE_H
- #include <unknwn.h>
-
- //{7C8027EA-A4ED-467c-B17E-1B51CE74AF57}
- static const GUID IID_ISimpleMath =
- { 0x7c8027ea, 0xa4ed, 0x467c, { 0xb1, 0x7e, 0x1b, 0x51, 0xce, 0x74, 0xaf, 0x57 } };
-
- //{CA3B37EA-E44A-49b8-9729-6E9222CAE84F}
- static const GUID IID_IAdvancedMath =
- { 0xca3b37ea, 0xe44a, 0x49b8, { 0x97, 0x29, 0x6e, 0x92, 0x22, 0xca, 0xe8, 0x4f } };
-
- interface ISimpleMath : public IUnknown
- {
- public:
- virtual int Add(int nOp1, int nOp2) = 0;
- virtual int Subtract(int nOp1, int nOp2) = 0;
- virtual int Multiply(int nOp1, int nOp2) = 0;
- virtual int Divide(int nOp1, int nOp2) = 0;
- };
-
- interface IAdvancedMath : public IUnknown
- {
- public:
- virtual int Factorial(int nOp1) = 0;
- virtual int Fabonacci(int nOp1) = 0;
- };
- #endif
此文件首先 #include
定义文件包括进来。
接下来定义了两个接口,GUID(Globally Unique Identifier全局唯一标识符)它能保证时间及空间上的唯一。
ISmipleMath接口里定义了四个方法,而IAdvancedMath接口里定义了二个方法。这些方法都是虚函数,而整个 ISmipleMath 与 IAdvancedMath 抽象类就作为二进制的接口。
2.2 math.h文件
- #include "interface.h"
-
- class CMath : public ISimpleMath,
- public IAdvancedMath
- {
- private:
- ULONG m_cRef;
-
- private:
- int calcFactorial(int nOp);
- int calcFabonacci(int nOp);
-
- public:
- //IUnknown Method
- STDMETHOD(QueryInterface)(REFIID riid, void **ppv);
- STDMETHOD_(ULONG, AddRef)();
- STDMETHOD_(ULONG, Release)();
-
- // ISimpleMath Method
- int Add(int nOp1, int nOp2);
- int Subtract(int nOp1, int nOp2);
- int Multiply(int nOp1, int nOp2);
- int Divide(int nOp1, int nOp2);
-
- // IAdvancedMath Method
- int Factorial(int nOp);
- int Fabonacci(int nOp);
- };
此类为实现类,他实现了ISmipleMath和IAdvancedMath两个接口类(当然也可以只实现一个接口类)。
请注意:m_cRef 是用来对象计数用的。当 m_cRef 为0组件对象应该自动删除。
2.3 math.cpp文件
- #include "interface.h"
- #include "math.h"
-
- STDMETHODIMP CMath::QueryInterface(REFIID riid, void **ppv)
- {// 这里这是实现dynamic_cast的功能,但由于dynamic_cast与编译器相关。
- if(riid == IID_ISimpleMath)
- *ppv = static_cast(this);
- else if(riid == IID_IAdvancedMath)
- *ppv = static_cast(this);
- else if(riid == IID_IUnknown)
- *ppv = static_cast(this);
- else {
- *ppv = 0;
- return E_NOINTERFACE;
- }
-
- reinterpret_cast(*ppv)->AddRef(); //这里要这样是因为引用计数是针对组件的
- return S_OK;
- }
-
- STDMETHODIMP_(ULONG) CMath::AddRef()
- {
- return ++m_cRef;
- }
-
- STDMETHODIMP_(ULONG) CMath::Release()
- {
- ULONG res = --m_cRef; // 使用临时变量把修改后的引用计数值缓存起来
- if(res == 0) // 因为在对象已经销毁后再引用这个对象的数据将是非法的
- delete this;
- return res;
- }
-
- int CMath::Add(int nOp1, int nOp2)
- {
- return nOp1+nOp2;
- }
-
- int CMath::Subtract(int nOp1, int nOp2)
- {
- return nOp1 - nOp2;
- }
-
- int CMath::Multiply(int nOp1, int nOp2)
- {
- return nOp1 * nOp2;
- }
-
- int CMath::Divide(int nOp1, int nOp2)
- {
- return nOp1 / nOp2;
- }
-
- int CMath::calcFactorial(int nOp)
- {
- if(nOp <= 1)
- return 1;
-
- return nOp * calcFactorial(nOp - 1);
- }
-
- int CMath::Factorial(int nOp)
- {
- return calcFactorial(nOp);
- }
-
- int CMath::calcFabonacci(int nOp)
- {
- if(nOp <= 1)
- return 1;
-
- return calcFabonacci(nOp - 1) + calcFabonacci(nOp - 2);
- }
-
- int CMath::Fabonacci(int nOp)
- {
- return calcFabonacci(nOp);
- }
- CMath::CMath()
- {
- m_cRef=0;
- }
此文件是CMath类定义文件。
2.4 simple.cpp文件
- #include "math.h"
- #include <iostream>
-
- using namespace std;
-
- int main(int argc, char* argv[])
- {
- ISimpleMath *pSimpleMath = NULL;//声明接口指针
- IAdvancedMath *pAdvMath = NULL;
-
- //创建对象实例,我们暂时这样创建对象实例,COM有创建对象实例的机制
- CMath *pMath = new CMath;
-
- //查询对象实现的接口ISimpleMath
- pMath->QueryInterface(IID_ISimpleMath, (void **)&pSimpleMath);
- if(pSimpleMath)
- cout << "10 + 4 = " << pSimpleMath->Add(10, 4) << endl;
-
- //查询对象实现的接口IAdvancedMath
- pSimpleMath->QueryInterface(IID_IAdvancedMath, (void **)&pAdvMath);
- if(pAdvMath)
- cout << "10 Fabonacci is " << pAdvMath->Fabonacci(10) << endl;
-
- pAdvMath->Release();
- pSimpleMath->Release();
- return 0;
- }
此文件相当于客户端的代码,首先创建一个CMath对象,再根据此对象去查询所需要的接口,如果正确得到所需接口指针,再调用接口的方法,最后再将接口的释放掉。