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

Windows编程

开发平台:

Visual C++

  1. // client.cpp
  2. #define _WIN32_DCOM
  3. #include <olectl.h>
  4. #include <conio.h>
  5. #include <iostream.h>
  6. #include "Componentcomponent.h"
  7. class CSink : public IOutGoing
  8. {
  9. public:
  10. // IUnknown
  11. ULONG __stdcall AddRef();
  12. ULONG __stdcall Release();
  13. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  14. // IOutGoing
  15. HRESULT __stdcall GotMessage(int Message);
  16. CSink() : m_cRef(0) { }
  17. ~CSink() { }
  18. private:
  19. long m_cRef;
  20. };
  21. ULONG CSink::AddRef()
  22. {
  23. return ++m_cRef;
  24. }
  25. ULONG CSink::Release()
  26. {
  27. if(--m_cRef != 0)
  28. return m_cRef;
  29. delete this;
  30. return 0;
  31. }
  32. HRESULT CSink::QueryInterface(REFIID riid, void** ppv)
  33. {
  34. if(riid == IID_IUnknown)
  35. {
  36. *ppv = (IUnknown*)this;
  37. }
  38. else if(riid == IID_IOutGoing)
  39. {
  40. cout << "Client: CSink::QueryInterface() for IOutGoing" << endl;
  41. *ppv = (IOutGoing*)this;
  42. }
  43. else 
  44. {
  45. *ppv = NULL;
  46. return E_NOINTERFACE;
  47. }
  48. AddRef();
  49. return S_OK;
  50. }
  51. HRESULT CSink::GotMessage(int Message)
  52. {
  53. if(Message == (int)'b' || Message == (int)'B')
  54. PlaySound("BrockschmidtQuack", NULL, SND_RESOURCE|SND_ASYNC);
  55. cout << "CSink::GotMessage is " << (char)Message << endl;
  56. return S_OK;
  57. }
  58. void main()
  59. {
  60. cout << "Client: Calling CoInitialize()" << endl;
  61. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  62. IUnknown* pUnknown;
  63. cout << "Client: Calling CoCreateInstance()" << endl;
  64. CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnknown);
  65. ISum* pSum;
  66. cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
  67. HRESULT hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  68. if(FAILED(hr))
  69. cout << "QueryInterface FAILED" << endl;
  70. int sum;
  71. pSum->Sum(17, 2, &sum);
  72. cout << "pSum->Sum(17, 2) = " << sum << endl;
  73. IConnectionPointContainer* pConnectionPointContainer;
  74. hr = pUnknown->QueryInterface(IID_IConnectionPointContainer, (void**)&pConnectionPointContainer);
  75. if(SUCCEEDED(hr))
  76. {
  77. IConnectionPoint* pConnectionPoint;
  78. hr = pConnectionPointContainer->FindConnectionPoint(IID_IOutGoing, &pConnectionPoint);
  79. cout << "FindConnectionPoint returns " << hr << " pConnectionPoint = " << pConnectionPoint << endl;
  80. CSink* mySink = new CSink;
  81. DWORD dwCookie;
  82. pConnectionPoint->Advise((IUnknown*)mySink, &dwCookie);
  83. cout << "Press any key to exit" << endl;
  84. _getch();
  85. pConnectionPoint->Unadvise(dwCookie);
  86. pConnectionPoint->Release();
  87. pConnectionPointContainer->Release();
  88. }
  89. cout << "Client: Calling Release() for pSum" << endl;
  90. hr = pSum->Release();
  91. cout << "Client: Calling Release() for pUnknown" << endl;
  92. hr = pUnknown->Release();
  93. cout << "Client: Calling CoUninitialize()" << endl;
  94. CoUninitialize();
  95. }