CPPClient.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:2k
源码类别:

Windows编程

开发平台:

Visual C++

  1. #define _WIN32_DCOM
  2. #include <windows.h>
  3. #include <iostream.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include "componentcomponent.h"
  7. void main()
  8. {
  9.     HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  10.     if(FAILED(hr))
  11.         cout << "CoInitializeEx failed. " << endl;
  12. ISum* pSum = 0;
  13. //  hr = CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_LOCAL_SERVER, IID_ISum, (void**)&pSum);
  14. // if(FAILED(hr))
  15. //     cout << "CoCreateInstance FAILED" << endl;
  16. IClassFactory* pClassFactory = 0;
  17. CoGetClassObject(CLSID_InsideCOM, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory, (void**)&pClassFactory);
  18. pClassFactory->CreateInstance(NULL, IID_ISum, (void**)&pSum);
  19. pClassFactory->Release();
  20. pSum->Initialize(6, 4);
  21. IUnknown* pUnknown = 0;
  22. hr = pSum->QueryInterface(IID_IUnknown, (void**)&pUnknown);
  23. if(FAILED(hr))
  24. cout << "QueryInterface FAILED" << endl;
  25. IMoniker* pMoniker = 0;
  26. hr = CreateObjrefMoniker(pUnknown, &pMoniker);
  27. if(FAILED(hr))
  28. cout << "CreateObjrefMoniker FAILED" << endl;
  29. IBindCtx* pBindCtx = 0;
  30. hr = CreateBindCtx(0, &pBindCtx);
  31. if(FAILED(hr))
  32. cout << "CreateBindCtx FAILED" << endl;
  33. OLECHAR* pString;
  34. hr = pMoniker->GetDisplayName(pBindCtx, NULL, &pString);
  35. if(FAILED(hr))
  36. printf("GetDisplayName FAILED %0xn", hr);
  37. cout << endl << "The line below is the custom marshaled interface pointer " << endl;
  38. cout << "containing the persistent state of the object. Copy it to " << endl;
  39. cout << "the clipboard and then open and run the VBClient.vbp project." << endl;
  40. cout << "Paste the marshaled interface pointer into the text box and " << endl;
  41. cout << "click Go! You will see that the object's state is available. " << endl;
  42. cout << "You can even close the CPPClient program and the VB program will still work." << endl;
  43. // +7 to remove "objref:"
  44. wprintf(L"n%snn", pString+7);
  45. CoTaskMemFree(pString);
  46. // Need an extra IUnknown->Release to counter AddRef by CoMarshalInterface's QI for IMarshal
  47. //    during the IMoniker->GetDisplayName call.
  48. pUnknown->Release();
  49. pBindCtx->Release();
  50. pMoniker->Release();
  51. pUnknown->Release();
  52. int sum = 0;
  53. pSum->GetSum(&sum);
  54. cout << "GetSum = " << sum << endl;
  55. pSum->Release();
  56.     CoUninitialize();
  57. }