Com API CoCreateInstance函数。是客户应用程序在创建组件实例时使用的。
//create an instance and return the IMath interface
IMath * pMath;
HRESULT hr = CocreateInstance(CLSid_Math,
NULL, clscTX_INPROC, IID_IMath, (void **)&pMath);
//Use IMath
long lResult = pMath->Multiply(44,33);
Com创建了组件实例并返回一个指向Vtable结果的指针的指针,该Vtable结构由你的抽象类IMath描述出来。
访问组件接口的另一项技术之标准接口。
大多数组件有多个接口,系统需要一种机制使客户可以访问某一特定的接口。以下是简单的程序代码:
IMath *pMath;
hr = pUnk->QueryInterface(IID_IMath, (void**)&pMath);
pUnk->Release();
if(FAILED(hr))
{
cout <<"QueryInterace() for IMath failed"<<endl;
CoUninitialeze();
return -1;
}