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

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 << "Component: CInsideCOM::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. IConnectionPointContainer* pConnectionPointContainer;
  66. HRESULT hr = pUnknown->QueryInterface(IID_IConnectionPointContainer, (void**)&pConnectionPointContainer);
  67. if(FAILED(hr))
  68. cout << "Not a connectable object, going to crash now" << endl;
  69. IEnumConnectionPoints* pEnumConnectionPoints;
  70. IEnumConnectionPoints* pEnumConnectionPoints2;
  71. hr = pConnectionPointContainer->EnumConnectionPoints(&pEnumConnectionPoints);
  72. ULONG num_fetched;
  73. IConnectionPoint* pConnectionPoint;
  74. hr = pEnumConnectionPoints->Clone(&pEnumConnectionPoints2);
  75. hr = pEnumConnectionPoints2->Next(1, &pConnectionPoint, &num_fetched);
  76. cout << "Next hr = " << hr << " num_fetched = " << num_fetched << endl;
  77. pEnumConnectionPoints->Release();
  78. pEnumConnectionPoints2->Release();
  79. IID my_iid;
  80. pConnectionPoint->GetConnectionInterface(&my_iid);
  81. char buffer[39];
  82. OLECHAR ppsz[39];
  83. StringFromGUID2(my_iid, ppsz, 39);
  84. WideCharToMultiByte(CP_ACP, 0, ppsz, 39, buffer, 39, NULL, NULL);
  85. cout << "IID: " << buffer << endl;
  86. IConnectionPointContainer* CPC;
  87. pConnectionPoint->GetConnectionPointContainer(&CPC);
  88. if(CPC == pConnectionPointContainer)
  89. cout << " THEY EQUAL " << endl;
  90. CPC->Release();
  91. // IConnectionPoint* pConnectionPoint;
  92. // hr = pConnectionPointContainer->FindConnectionPoint(IID_IOutGoing, &pConnectionPoint);
  93. // cout << "FindConnectionPoint returns " << hr << " pConnectionPoint = " << pConnectionPoint << endl;
  94. CSink* mySink = new CSink;
  95. DWORD dwCookie;
  96. pConnectionPoint->Advise((IUnknown*)mySink, &dwCookie);
  97. cout << "CLIENT CALLED ADVISE" << hr << endl;
  98. IEnumConnections* pEnumConnections;
  99. pConnectionPoint->EnumConnections(&pEnumConnections);
  100. CONNECTDATA my_cd;
  101. ULONG dd;
  102. pEnumConnections->Next(1, &my_cd, &dd);
  103. cout << "MY COOKIE = " << my_cd.dwCookie << endl;
  104. cout << "MY COOKIE2 = " << dwCookie << endl;
  105. pEnumConnections->Release();
  106. cout << "Press any key to exit" << endl;
  107. _getch();
  108. pConnectionPoint->Unadvise(dwCookie);
  109. pConnectionPoint->Release();
  110. pConnectionPointContainer->Release();
  111. cout << "Client: Calling Release() for pUnknown" << endl;
  112. hr = pUnknown->Release();
  113. cout << "Client: Calling CoUninitialize()" << endl;
  114. CoUninitialize();
  115. }