client with CoGetClassObject.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:2k
源码类别:
Windows编程
开发平台:
Visual C++
- // client.cpp
- #include <iostream.h>
- #include "Component with registrationcomponent.h" // Generated by MIDL
- // {10000002-0000-0000-0000-000000000001}
- const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
- void main()
- {
- cout << "Client: Calling CoInitialize()" << endl;
- HRESULT hr = CoInitialize(NULL);
- if(FAILED(hr))
- cout << "CoInitialize failed" << endl;
- IUnknown* pUnknown;
- ISum* pSum;
- // cout << "Client: Calling CoCreateInstance()" << endl;
- // hr = CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&pUnknown);
- // if(FAILED(hr))
- // cout << "CoCreateInstance failed" << endl;
- IClassFactory* pClassFactory;
- hr = CoGetClassObject(CLSID_InsideCOM, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory, (void**)&pClassFactory);
- if(FAILED(hr))
- cout << "CoGetClassObject failed" << endl;
- pClassFactory->CreateInstance(NULL, IID_IUnknown, (void**)&pUnknown);
- pClassFactory->Release();
- cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
- hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
- if(FAILED(hr))
- cout << "IID_ISum not supported" << endl;
- hr = pUnknown->Release();
- cout << "Client: Calling pUnknown->Release() reference count = " << hr << endl;
- int sum;
- hr = pSum->Sum(2, 3, &sum);
- if(SUCCEEDED(hr))
- cout << "Client: Calling Sum(2, 3) = " << sum << endl;
- hr = pSum->Release();
- cout << "Client: Calling pSum->Release() reference count = " << hr << endl;
- cout << "Client: Calling CoUninitialize()" << endl;
- CoUninitialize();
- }