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;
}