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

Windows编程

开发平台:

Visual C++

  1. // vbclient.cpp
  2. #define _WIN32_DCOM
  3. #include <olectl.h>
  4. #include <conio.h>
  5. #include <iostream.h>
  6. #include <stdio.h>
  7. #include "Componentcomponent.h"
  8. // {8788DE0C-1441-11D3-8EF5-00C04F68F506}
  9. const IID IID_EventClass = {0x8788DE0C, 0x1441, 0x11D3, {0x8E, 0xF5, 0x00,
  10. 0xC0, 0x4F, 0x68, 0xF5, 0x06}};
  11. // {8788DE0B-1441-11D3-8EF5-00C04F68F506}
  12. const CLSID CLSID_InsideCOM = {0x8788DE0B, 0x1441, 0x11D3, {0x8E, 0xF5, 0x00, 
  13.     0xC0, 0x4F, 0x68, 0xF5, 0x06}};
  14. class CSink : public IUnknown
  15. {
  16. public:
  17. // IUnknown
  18. ULONG __stdcall AddRef();
  19. ULONG __stdcall Release();
  20. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  21. // IDispatch
  22. HRESULT __stdcall GetTypeInfoCount(UINT* pCountTypeInfo);
  23. HRESULT __stdcall GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo);
  24. HRESULT __stdcall GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId);
  25. HRESULT __stdcall Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
  26.     HRESULT virtual __stdcall Message(short* KeyAscii);
  27. CSink() : m_cRef(0) { }
  28. ~CSink() { }
  29. private:
  30. long m_cRef;
  31. };
  32. HRESULT CSink::Message(short* KeyAscii)
  33. {
  34. cout << "Message" << endl;
  35. return S_OK;
  36. }
  37. HRESULT CSink::GetTypeInfoCount(UINT* pCountTypeInfo)
  38. {
  39. cout << "GetTypeInfoCount" << endl;
  40. *pCountTypeInfo = 0;
  41. return S_OK;
  42. }
  43. HRESULT CSink::GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo)
  44. {
  45. cout << "GetTypeInfo" << endl;
  46. return S_OK;
  47. }
  48. HRESULT CSink::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId)
  49. {
  50. cout << "GetIDsOfNames" << endl;
  51. if(riid != IID_NULL)
  52. return DISP_E_UNKNOWNINTERFACE;
  53. return S_OK;
  54. }
  55. HRESULT CSink::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
  56. {
  57. cout << "Invoke" << endl;
  58. if(riid != IID_NULL)
  59. return DISP_E_UNKNOWNINTERFACE;
  60. return S_OK; 
  61. }
  62. ULONG CSink::AddRef()
  63. {
  64. cout << "AddRef" << endl;
  65. return ++m_cRef;
  66. }
  67. ULONG CSink::Release()
  68. {
  69. cout << "Release" << endl;
  70. if(--m_cRef != 0)
  71. return m_cRef;
  72. delete this;
  73. return 0;
  74. }
  75. HRESULT CSink::QueryInterface(REFIID riid, void** ppv)
  76. {
  77. if(riid == IID_IUnknown)
  78. {
  79. cout << "Client: CSink::QueryInterface() for IID_IUnknown" << endl;
  80. *ppv = (IUnknown*)this;
  81. }
  82. else if(riid == IID_IDispatch)
  83. {
  84. cout << "Client: CSink::QueryInterface() for IID_IDispatch" << endl;
  85. *ppv = (IDispatch*)this;
  86. }
  87. else if(riid == IID_EventClass)
  88. {
  89. cout << "Client: CSink::QueryInterface() for IID_EventClass" << endl;
  90. *ppv = (IDispatch*)this;
  91. }
  92. else 
  93. {
  94. *ppv = NULL;
  95. return E_NOINTERFACE;
  96. }
  97. AddRef();
  98. return S_OK;
  99. }
  100. /*HRESULT CSink::GotMessage(int Message)
  101. {
  102. if(Message == (int)'b' || Message == (int)'B')
  103. PlaySound("BrockschmidtQuack", NULL, SND_RESOURCE|SND_ASYNC);
  104. cout << "CSink::GotMessage is " << (char)Message << endl;
  105. return S_OK;
  106. }*/
  107. void main()
  108. {
  109. cout << "Client: Calling CoInitialize()" << endl;
  110. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  111. IUnknown* pUnknown;
  112. cout << "Client: Calling CoCreateInstance()" << endl;
  113. CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnknown);
  114. // ISum* pSum;
  115. // cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
  116. // HRESULT hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  117. // if(FAILED(hr))
  118. // cout << "QueryInterface FAILED" << endl;
  119. // int sum;
  120. // pSum->Sum(17, 2, &sum);
  121. // cout << "pSum->Sum(17, 2) = " << sum << endl;
  122. IConnectionPointContainer* pConnectionPointContainer;
  123. HRESULT hr = pUnknown->QueryInterface(IID_IConnectionPointContainer, (void**)&pConnectionPointContainer);
  124. if(SUCCEEDED(hr))
  125. {
  126. IConnectionPoint* pConnectionPoint;
  127. hr = pConnectionPointContainer->FindConnectionPoint(IID_EventClass, &pConnectionPoint);
  128. cout << "FindConnectionPoint returns " << hr << " pConnectionPoint = " << pConnectionPoint << endl;
  129. printf("FindConnectionPoint = %0xn", hr);
  130. CSink* mySink = new CSink;
  131. DWORD dwCookie;
  132. pConnectionPoint->Advise((IUnknown*)mySink, &dwCookie);
  133. cout << "Press any key to exit" << endl;
  134. _getch();
  135. pConnectionPoint->Unadvise(dwCookie);
  136. pConnectionPoint->Release();
  137. pConnectionPointContainer->Release();
  138. }
  139. // cout << "Client: Calling Release() for pSum" << endl;
  140. // hr = pSum->Release();
  141. cout << "Client: Calling Release() for pUnknown" << endl;
  142. hr = pUnknown->Release();
  143. cout << "Client: Calling CoUninitialize()" << endl;
  144. CoUninitialize();
  145. }