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

Windows编程

开发平台:

Visual C++

  1. #define _WIN32_DCOM
  2. #include <iostream.h>
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <ocidl.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.     IUnknown* pUnknown;
  13.     hr = CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, 
  14. IID_IUnknown, (void**)&pUnknown);
  15.     if(FAILED(hr))
  16. cout << "CoCreateInstance failed. " << endl;
  17.     IPersistStreamInit* pPersistStreamInit;
  18.     hr = pUnknown->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit);
  19.     if(FAILED(hr))
  20.         cout << "IID_IPersistStreamInit not supported. " << endl;
  21. IStream* pStream = 0;
  22. hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
  23.     if(FAILED(hr))
  24.         cout << "CreateStreamOnHGlobal failed. " << endl;
  25. hr = pPersistStreamInit->InitNew();
  26.     if(FAILED(hr))
  27. printf("IPersistStreamInit::InitNew failed %0xn", hr);
  28. ISum* pSum = 0;
  29. hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  30. int retval = 0;
  31. hr = pSum->Sum(5, 3, &retval);
  32. cout << "5 + 3 = " << retval << endl;
  33. hr = pPersistStreamInit->Save(pStream, FALSE);
  34. if(FAILED(hr))
  35. printf("IPersistStreamInit::Save failed %0xn", hr);
  36.     pPersistStreamInit->Release();
  37.     pUnknown->Release();
  38. pSum->Release();
  39. // Create a clone of the object
  40.     LARGE_INTEGER zeroa = { 0, 0 };
  41.     pStream->Seek(zeroa, STREAM_SEEK_SET, NULL);
  42.     hr = CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, 
  43. IID_IUnknown, (void**)&pUnknown);
  44.     if(FAILED(hr))
  45. cout << "CoCreateInstance failed. " << endl;
  46.     hr = pUnknown->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit);
  47.     if(FAILED(hr))
  48.         cout << "IID_IPersistStreamInit not supported. " << endl;
  49. hr = pPersistStreamInit->Load(pStream);
  50.     if(FAILED(hr))
  51.        printf("IPersistStreamInit::Load failed %0xn", hr);
  52. hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  53. hr = pSum->SumPersist(&retval);
  54. cout << "SumPersist returns " << retval << endl;
  55. pSum->Release();
  56. pStream->Release();
  57.     pPersistStreamInit->Release();
  58.     pUnknown->Release();
  59.     CoUninitialize();
  60. }