client.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:2k
- #define _WIN32_DCOM
- #include <iostream.h>
- #include <stdio.h>
- #include "componentcomponent.h" // Generated by MIDL
- const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
- void main()
- {
- IUnknown* pUnknown;
- ISum* pSum;
- HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
- if(FAILED(hr))
- cout << "CoInitializeEx failed. " << endl;
- hr = CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER,
- IID_IUnknown, (void**)&pUnknown);
- if(FAILED(hr))
- cout << "CoCreateInstance failed. " << endl;
- hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
- if(FAILED(hr))
- cout << "IID_ISum not supported. " << endl;
- pUnknown->Release();
- int sum;
- hr = pSum->Sum(2, 3, &sum);
- if(SUCCEEDED(hr))
- cout << "Client: Calling Sum(2, 3) = " << sum << endl;
- IMoniker* pObjrefMoniker = 0;
- CreateObjrefMoniker(pUnknown, &pObjrefMoniker);
- IBindCtx* pBindContext = 0;
- CreateBindCtx(0, &pBindContext);
- OLECHAR* pString;
- pObjrefMoniker->GetDisplayName(pBindContext, NULL, &pString);
- wprintf(L"nDisplay name of objref moniker for pUnknown is n%snn", pString);
- cout << "Binding to object named by the objref moniker display name" << endl;
- ISum* pSum2 = 0;
- BIND_OPTS bopts = { 0, 0, 0, 0 };
- hr = CoGetObject(pString, &bopts, IID_ISum, (void**)&pSum2);
- CoTaskMemFree(pString);
- hr = pSum2->Sum(5, 4, &sum);
- pSum2->Release();
-
- pSum->Release();
- pObjrefMoniker->Release();
- CoUninitialize();
- }