void main35() { CA* pa = new CA(); IX *px = pa; px->fun(); IY* py = pa; py->fun(); }
void test(const CLSID & clsid) { DWORD clsctx ; clsctx = CLSCTX_INPROC_SERVER ; trace("Attempt to create in-proc component.") ; IX* pIX = NULL ; HRESULT hr = CoCreateInstance(clsid, NULL, clsctx, IID_IX, (void**)&pIX) ; if (SUCCEEDED(hr)) { trace("Successfully created component.") ; trace("Use interface IX.") ; wchar_t* wszIn = L"This is the test." ; BSTR bstrIn ; bstrIn = ::SysAllocString(wszIn) ; pIX->FxStringIn(bstrIn) ; ::SysFreeString(bstrIn) ; BSTR bstrOut ; //@dual pIX->FxStringOut(&bstrOut ) ; // Display returned string. ostrstream sout ; sout << "FxStringOut returned a string: " << bstrOut << ends; trace(sout.str()) ; ::SysFreeString(bstrOut ) ; trace("Release IX.") ; pIX->Release() ; } else { trace("Could not create component.", hr); } }
// // Client1 // int main() { HRESULT hr ; // Get the name of the component to use. char name[40] ; cout << "Enter the filename of a component to use [Cmpnt?.dll]: " ; cin >> name ; cout << endl ; // Create component by calling the CreateInstance function in the DLL. trace("Get an IUnknown pointer.") ; IUnknown* pIUnknown = CallCreateInstance(name) ; if (pIUnknown == NULL) { trace("CallCreateInstance Failed.") ; return 1 ; } trace("Get interface IX.") ; IX* pIX ; hr = pIUnknown->QueryInterface(IID_IX, (void**)&pIX) ; if (SUCCEEDED(hr)) { trace("Succeeded getting IX.") ; pIX->Fx() ; // Use interface IX. pIX->Release() ; } else { trace("Could not get interface IX.") ; } trace("Release IUnknown interface.") ; pIUnknown->Release() ; return 0 ; }
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。 HMODULE hWnd = LoadLibraryW(L"IUnknowd.dll"); if (hWnd == NULL) { return 1; } IUDllCreateObject test = (IUDllCreateObject)GetProcAddress(hWnd, "DllCreateObject"); if (NULL == test) { return 2; } IUnknow* pI = NULL; test((void**)&pI); IX* pX = NULL; HRESULT hr = pI->QueryInterface(2, (void**)&pX); if (SUCCEEDED(hr)) { pX->PrintX(); pX->Release(); } pI->Release(); return 0; }
int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr; CA *pA = new CA(); //引用计数1 //从组件查询IUnknown接口 IUnknown *pIUnknown = NULL; hr = pA->QueryInterface(IID_IUnknown, (void**)&pIUnknown); //引用计数2 if (SUCCEEDED(hr)) //对HRESULT返回值的判断,一般采用SUCCEEDED { pA->Release(); //pA不再使用,引用计数1 pA = NULL; //访止再不小心使用m_pA //从IUnknown查询IX接口 IX *pIX = NULL; hr = pIUnknown->QueryInterface(IID_IX, (void**)&pIX); //引用计数2 if (SUCCEEDED(hr)) { //调用IX接口的方法 pIX->Fx1(); pIX->Fx2(); } //从IUnknown查询IY接口 IY *pIY = NULL; hr = pIUnknown->QueryInterface(IID_IY, (void**)&pIY); //引用计数3 if (SUCCEEDED(hr)) { //调用IY接口的方法 pIY->Fy1(); pIY->Fy1(); } if ((void*)pIX != (void*)pIY) { cout << "pIX != pIY" <<endl; } if ((void*)pIUnknown != (void*)pIY) { cout << "pIUnknown != pIY" <<endl; } pIY->Release(); //pIY不再使用,引用计数2 pIY = NULL; if ((void*)pIUnknown == (void*)pIX) { cout << "pIUnknown == pIX" <<endl; } //从IX查询IY IY *pIY2 = NULL; hr = pIX->QueryInterface(IID_IY, (void**)&pIY2); //引用计数,引用计数3 pIX->Release(); //pIX不再使用,引用计数2 pIX = NULL; if (SUCCEEDED(hr)) { pIY2->Fy1(); pIY2->Fy2(); } pIY2->Release(); //pIY不再使用,引用计数1 pIY2 = NULL; } //目前引用计数为1,因为pIUnknown还在使用。 IX *pIX2 = NULL; hr = pIUnknown->QueryInterface(IID_IX, (void**)&pIX2); //引用计数为2 if (SUCCEEDED(hr)) { IX *pIX3 = NULL; pIX3 = pIX2; //执行了赋值 pIX3->AddRef(); //由于上句执行了赋值,所以引用计数需要自增,引用计数为3 pIX3->Fx1(); pIX3->Fx2(); pIX3->Release(); //pIX3不再使用,引用计数为2 pIX3 = NULL; } pIX2->Release(); //pIX2不再使用,引用计数为1 pIX2 = NULL; pIUnknown->Release(); //pIUnknown不再使用,引用计数为0,Release函数里执行了delete this,销毁组件的内存资源 pIUnknown = NULL; //释放组件? no! //delete pA; //不再需要写delete代码 return 0; }
int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr; //创建组件 CA *pA = new CA(); //从组件查询IUnknown接口 IUnknown *pIUnknown = NULL; hr = pA->QueryInterface(IID_IUnknown, (void**)&pIUnknown); if (SUCCEEDED(hr)) //对HRESULT返回值的判断,一般采用SUCCEEDED { //从IUnknown查询IX接口 IX *pIX = NULL; hr = pIUnknown->QueryInterface(IID_IX, (void**)&pIX); if (SUCCEEDED(hr)) { //调用IX接口的方法 pIX->Fx1(); pIX->Fx2(); } //从IUnknown查询IY接口 IY *pIY = NULL; hr = pIUnknown->QueryInterface(IID_IY, (void**)&pIY); if (SUCCEEDED(hr)) { //调用IY接口的方法 pIY->Fy1(); pIY->Fy1(); } if ((void*)pIX != (void*)pIY) { cout << "pIX != pIY" <<endl; } if ((void*)pIUnknown != (void*)pIY) { cout << "pIUnknown != pIY" <<endl; } if ((void*)pIUnknown == (void*)pIX) { cout << "pIUnknown == pIX" <<endl; } //从IX查询IY IY *pIY2 = NULL; hr = pIX->QueryInterface(IID_IY, (void**)&pIY2); if (SUCCEEDED(hr)) { pIY2->Fy1(); pIY2->Fy2(); } //从IX也可以查询到IUnknown //从IY也可以查询到IX //从IY也可以查询到IUnknown //从CA也可以查询到IX //从CA也可以查询到IY //总结: //只要是CA所继承的接口,从CA的组件里都可以查询到; //只要是CA所继承的接口,从CA所继承的其它接口里都可以查询到。 } //释放组件 delete pA; return 0; }