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

Windows编程

开发平台:

Visual C++

  1. #define _WIN32_DCOM
  2. #include <iostream.h>
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <ocidl.h>
  6. {3AF0F8E7-ED16-11D2-BB52-006097B5EAFC}
  7. const CLSID CLSID_PersistentVB = {0x3AF0F8E7, 0xED16, 0x11D2, {0xBB, 0x52, 0x00, 
  8.   0x60, 0x97, 0xB5, 0xEA, 0xFC}};
  9. class CPropertyBag : public IPropertyBag
  10. {
  11. public:
  12. // IUnknown
  13. ULONG __stdcall AddRef();
  14. ULONG __stdcall Release();
  15. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  16. // IPropertyBag
  17.     HRESULT __stdcall Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog* pErrorLog);
  18.     HRESULT __stdcall Write(LPCOLESTR pszPropName, VARIANT* pVar);
  19. CPropertyBag() : m_cRef(1) { }
  20. private:
  21. ULONG m_cRef;
  22. };
  23. HRESULT CPropertyBag::Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog* pErrorLog)
  24. {
  25. cout << "READ" << endl;
  26. return S_OK;
  27. }
  28. HRESULT CPropertyBag::Write(LPCOLESTR pszPropName, VARIANT* pVar)
  29. {
  30. wprintf(L"PropertyName = %s Value = %sn", pszPropName, pVar->bstrVal);
  31. return S_OK;
  32. }
  33. ULONG CPropertyBag::AddRef()
  34. {
  35. cout << "AddRef" << endl;
  36. return ++m_cRef;
  37. }
  38. ULONG CPropertyBag::Release()
  39. {
  40. cout << "Release" << endl;
  41. if(--m_cRef != 0)
  42. return m_cRef;
  43. delete this;
  44. return 0;
  45. }
  46. HRESULT CPropertyBag::QueryInterface(REFIID riid, void** ppv)
  47. {
  48. cout << "QueryInterface" << endl;
  49. if(riid == IID_IUnknown)
  50. *ppv = (IUnknown*)this;
  51. else if(riid == IID_IPropertyBag)
  52. *ppv = (IPropertyBag*)this;
  53. else 
  54. {
  55. *ppv = NULL;
  56. return E_NOINTERFACE;
  57. }
  58. AddRef();
  59. return S_OK;
  60. }
  61. void main()
  62. {
  63.     HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  64.     if(FAILED(hr))
  65.         cout << "CoInitializeEx failed. " << endl;
  66.     IUnknown* pUnknown;
  67.    hr = CoCreateInstance(CLSID_PersistentVB, NULL, CLSCTX_INPROC_SERVER, 
  68.       IID_IUnknown, (void**)&pUnknown);
  69.     if(FAILED(hr))
  70.       cout << "CoCreateInstance failed. " << endl;
  71.     IPersistStreamInit* pPersistStreamInit;
  72.     hr = pUnknown->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit);
  73.     if(FAILED(hr))
  74.         cout << "IID_IPersistStreamInit not supported. " << endl;
  75.     IPersistPropertyBag* pPersistPropertyBag;
  76.     hr = pUnknown->QueryInterface(IID_IPersistPropertyBag, (void**)&pPersistPropertyBag);
  77.     if(FAILED(hr))
  78.         cout << "IID_IPersistPropertyBag not supported. " << endl;
  79. hr = pPersistPropertyBag->InitNew();
  80.    if(FAILED(hr))
  81.      printf("IPersistPropertyBag::InitNew failed %0xn", hr);
  82. IPropertyBag* pPropertyBag = new CPropertyBag();
  83. hr = pPersistPropertyBag->Save(pPropertyBag, TRUE, TRUE);
  84.    if(FAILED(hr))
  85.      printf("IPersistPropertyBag::Save failed %0xn", hr);
  86. IStream* pStream = 0;
  87. hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
  88.     if(FAILED(hr))
  89.         cout << "CreateStreamOnHGlobal failed. " << endl;
  90. cout << "About to InitNew" << endl;
  91. hr = pPersistStreamInit->InitNew();
  92.    if(FAILED(hr))
  93.      printf("IPersistStreamInit::InitNew failed %0xn", hr);
  94. cout << "After InitNew" << endl;
  95. /*
  96. hr = pPersistStreamInit->Save(pStream, FALSE);
  97.    if(FAILED(hr))
  98.      printf("IPersistStreamInit::Save failed %0xn", hr);
  99. */
  100. IPersistStream* pPersistStream;
  101. pPersistStreamInit->QueryInterface(IID_IPersistStream, (void**)&pPersistStream);
  102.    hr = OleSaveToStream(pPersistStream, pStream);
  103.     if(FAILED(hr))
  104.       cout << "OleSaveToStream failed. " << endl;
  105.    LARGE_INTEGER zeroa = { 0, 0 };
  106.    pStream->Seek(zeroa, STREAM_SEEK_SET, NULL);
  107.    IUnknown* pObject;
  108.    hr = OleLoadFromStream(pStream, IID_IUnknown, (void**)&pObject);
  109.     if(FAILED(hr))
  110.       cout << "OleLoadFromStream failed. " << endl;
  111.    LARGE_INTEGER zero = { 0, 0 };
  112.    pStream->Seek(zero, STREAM_SEEK_SET, NULL);
  113.     IUnknown* pUnknown2;
  114.    hr = CoCreateInstance(CLSID_PersistentVB, NULL, CLSCTX_INPROC_SERVER, 
  115.       IID_IUnknown, (void**)&pUnknown2);
  116.     if(FAILED(hr))
  117.       cout << "CoCreateInstance failed. " << endl;
  118.     IPersistStreamInit* pPersistStreamInit2;
  119.     hr = pUnknown2->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit2);
  120.     if(FAILED(hr))
  121.         cout << "IID_IPersistStreamInit not supported. " << endl;
  122. hr = pPersistStreamInit2->Load(pStream);
  123.     if(FAILED(hr))
  124.        printf("IPersistStreamInit::Load failed %0xn", hr);
  125. //   zero.LowPart = 20;
  126.   // pStream->Seek(zero, STREAM_SEEK_SET, NULL);
  127. void* data = GlobalAlloc(0, 500);
  128. ULONG count = 0;
  129. hr = pStream->Read(data, 50, &count);
  130.   if(FAILED(hr))
  131.     printf("IStream::Read failed %0xn", hr);
  132. wprintf(L"count = %d, data = %sn", count, data);
  133.     pPersistStreamInit->Release();
  134. pStream->Release();
  135.     pUnknown->Release();
  136.     CoUninitialize();
  137. }