ry.tlb"named_guids raw_interfaces_only
// the applications main entry point
int _tmain(int argc, _TCHAR* argv[])
{
// initialize the COM runtime
CoInitialize(NULL);
// a pointer to our COM class
comlibrary::IMathPtr pDotNetCOMPtr;
// create a new instance of the Math class
HRESULT hRes =pDotNetCOMPtr.CreateInstance(comlibrary::CLSID_Math);
// check it was created okay
if (hRes == S_OK)
{
// define a local to hold the result
long res = 0L;
// call the Add function
hRes =pDotNetCOMPtr->Add(1, 2, &res);
// check Add was called okay
if (hRes == S_OK)
{
// print the result
printf("The result was: %ld", res);
}
// release the pointer to the math COM class
pDotNetCOMPtr.Release();
}
// uninitialise the COM runtime
CoUninitialize();
}
示例的运行结果如下:
The result was: 3
当我们运行最后的程序时,必须保证ComLibrary.dll 与程序在同样的目录中,否则,COM 运行时会找不到文件。如果打算让这个库被多个客户端使用,那么,我强烈建议对程序集签名,并放在全局程序集缓存(Global Assembly Cache,GAC)中,这样,所有的客户端都能找到它,就不必要在第一个目录下都复制一份。