我们建立了一个ATL工程MathServer,声明了ISimpleMath接口和实现了SimpleMath对象,
现在我们要在我们的MathClient的工程中访问这个COM对象。
方法一:
首先要在工程中导入MathServer工程生成的dll或tlb文件。
#import "../Lib/MathLib.dll" no_namespace
#import "../Lib/MathLib.tlb" no_namespace
然后我们就可以通过以下的方式创建COM对象了,注意一定要使用__uuidof()函数获取
类和接口的ID.
ISimpleMath* pMath;
HRESULT hr = CoCreateInstance( __uuidof(SimpleMath),
NULL,
CLSCTX_INPROC,
__uuidof(ISimpleMath),
(void**) &pMath );
if ( FAILED( hr ))
{
cout.setf( ios::hex, ios::basefield );
cout << "Failed to create server instance. HR = " << hr << endl;
return -1;
}
ong result;
pMath->Multiply( 100, 8, &result );
cout << "100 * 8 is " << result << endl;
方法二:
直接在文件中包含MathServer工程生成的两个文件,如下所示
#include "../Lib/MathLib.h"
#include "../Lib/MathLib_i.c"
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/hopestar2/archive/2009/02/10/3874735.aspx