ctlconn.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFXCTL_CORE3_SEG
  12. #pragma code_seg(AFXCTL_CORE3_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Connection map
  21. BEGIN_CONNECTION_MAP(COleControl, CCmdTarget)
  22. CONNECTION_PART(COleControl, IID_IPropertyNotifySink, PropConnPt)
  23. END_CONNECTION_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // COleControl::GetConnectionHook - override the connection map
  26. LPCONNECTIONPOINT COleControl::GetConnectionHook(REFIID iid)
  27. {
  28. if ((m_piidEvents != NULL) && IsEqualIID(iid, *m_piidEvents))
  29. return (LPCONNECTIONPOINT)((char*)&m_xEventConnPt +
  30. offsetof(CConnectionPoint, m_xConnPt));
  31. else
  32. return NULL;
  33. }
  34. /////////////////////////////////////////////////////////////////////////////
  35. // COleControl::GetExtraConnectionPoints - override the connection map
  36. BOOL COleControl::GetExtraConnectionPoints(CPtrArray* pConnPoints)
  37. {
  38. pConnPoints->Add((char*)&m_xEventConnPt +
  39. offsetof(CConnectionPoint, m_xConnPt));
  40. return TRUE;
  41. }
  42. /////////////////////////////////////////////////////////////////////////////
  43. // COleControl::XProvideClassInfo
  44. STDMETHODIMP_(ULONG) COleControl::XProvideClassInfo::AddRef()
  45. {
  46. METHOD_PROLOGUE_EX_(COleControl, ProvideClassInfo)
  47. return (ULONG)pThis->ExternalAddRef();
  48. }
  49. STDMETHODIMP_(ULONG) COleControl::XProvideClassInfo::Release()
  50. {
  51. METHOD_PROLOGUE_EX_(COleControl, ProvideClassInfo)
  52. return (ULONG)pThis->ExternalRelease();
  53. }
  54. STDMETHODIMP COleControl::XProvideClassInfo::QueryInterface(
  55. REFIID iid, LPVOID* ppvObj)
  56. {
  57. METHOD_PROLOGUE_EX_(COleControl, ProvideClassInfo)
  58. return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  59. }
  60. STDMETHODIMP COleControl::XProvideClassInfo::GetClassInfo(
  61. LPTYPEINFO* ppTypeInfo)
  62. {
  63. METHOD_PROLOGUE_EX(COleControl, ProvideClassInfo)
  64. CLSID clsid;
  65. pThis->GetClassID(&clsid);
  66. return pThis->GetTypeInfoOfGuid(GetUserDefaultLCID(), clsid, ppTypeInfo);
  67. }
  68. STDMETHODIMP COleControl::XProvideClassInfo::GetGUID(DWORD dwGuidKind,
  69. GUID* pGUID)
  70. {
  71. METHOD_PROLOGUE_EX_(COleControl, ProvideClassInfo)
  72. if (dwGuidKind == GUIDKIND_DEFAULT_SOURCE_DISP_IID)
  73. {
  74. *pGUID = *pThis->m_piidEvents;
  75. return NOERROR;
  76. }
  77. else
  78. {
  79. *pGUID = GUID_NULL;
  80. return E_INVALIDARG;
  81. }
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // COleControl::XEventConnPt
  85. void COleControl::XEventConnPt::OnAdvise(BOOL bAdvise)
  86. {
  87. METHOD_PROLOGUE_EX(COleControl, EventConnPt)
  88. pThis->OnEventAdvise(bAdvise);
  89. }
  90. REFIID COleControl::XEventConnPt::GetIID()
  91. {
  92. METHOD_PROLOGUE_EX_(COleControl, EventConnPt)
  93. if (pThis->m_piidEvents != NULL)
  94. return *(pThis->m_piidEvents);
  95. else
  96. return GUID_NULL;
  97. }
  98. LPUNKNOWN COleControl::XEventConnPt::QuerySinkInterface(LPUNKNOWN pUnkSink)
  99. {
  100. METHOD_PROLOGUE_EX_(COleControl, EventConnPt)
  101. // First, QI for control-specific IID; failing that, QI for IDispatch
  102. LPUNKNOWN pUnkReturn = NULL;
  103. if (FAILED(pUnkSink->QueryInterface(*(pThis->m_piidEvents),
  104. reinterpret_cast<void**>(&pUnkReturn))))
  105. {
  106. pUnkSink->QueryInterface(IID_IDispatch,
  107. reinterpret_cast<void**>(&pUnkReturn));
  108. }
  109. return pUnkReturn;
  110. }
  111. /////////////////////////////////////////////////////////////////////////////
  112. // COleControl::OnEventAdvise - called by XEventConnPt::OnAdvise
  113. void COleControl::OnEventAdvise(BOOL)
  114. {
  115. // May be overridden by subclass
  116. }
  117. /////////////////////////////////////////////////////////////////////////////
  118. // AfxConnectionAdvise
  119. BOOL AFXAPI AfxConnectionAdvise(LPUNKNOWN pUnkSrc, REFIID iid,
  120. LPUNKNOWN pUnkSink, BOOL bRefCount, DWORD* pdwCookie)
  121. {
  122. ASSERT_POINTER(pUnkSrc, IUnknown);
  123. ASSERT_POINTER(pUnkSink, IUnknown);
  124. ASSERT_POINTER(pdwCookie, DWORD);
  125. BOOL bSuccess = FALSE;
  126. LPCONNECTIONPOINTCONTAINER pCPC;
  127. if (SUCCEEDED(pUnkSrc->QueryInterface(
  128. IID_IConnectionPointContainer,
  129. (LPVOID*)&pCPC)))
  130. {
  131. ASSERT_POINTER(pCPC, IConnectionPointContainer);
  132. LPCONNECTIONPOINT pCP;
  133. if (SUCCEEDED(pCPC->FindConnectionPoint(iid, &pCP)))
  134. {
  135. ASSERT_POINTER(pCP, IConnectionPoint);
  136. if (SUCCEEDED(pCP->Advise(pUnkSink, pdwCookie)))
  137. bSuccess = TRUE;
  138. pCP->Release();
  139. // The connection point just AddRef'ed us. If we don't want to
  140. // keep this reference count (because it would prevent us from
  141. // being deleted; our reference count wouldn't go to zero), then
  142. // we need to cancel the effects of the AddRef by calling
  143. // Release.
  144. if (bSuccess && !bRefCount)
  145. pUnkSink->Release();
  146. }
  147. pCPC->Release();
  148. }
  149. return bSuccess;
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. // AfxConnectionUnadvise
  153. BOOL AFXAPI AfxConnectionUnadvise(LPUNKNOWN pUnkSrc, REFIID iid,
  154. LPUNKNOWN pUnkSink, BOOL bRefCount, DWORD dwCookie)
  155. {
  156. ASSERT_POINTER(pUnkSrc, IUnknown);
  157. ASSERT_POINTER(pUnkSink, IUnknown);
  158. // When we call Unadvise, the connection point will Release us. If we
  159. // didn't keep the reference count when we called Advise, we need to
  160. // AddRef now, to keep our reference count consistent. Note that if
  161. // the Unadvise fails, then we need to undo this extra AddRef by
  162. // calling Release before we return.
  163. if (!bRefCount)
  164. pUnkSink->AddRef();
  165. BOOL bSuccess = FALSE;
  166. LPCONNECTIONPOINTCONTAINER pCPC;
  167. if (SUCCEEDED(pUnkSrc->QueryInterface(
  168. IID_IConnectionPointContainer,
  169. (LPVOID*)&pCPC)))
  170. {
  171. ASSERT_POINTER(pCPC, IConnectionPointContainer);
  172. LPCONNECTIONPOINT pCP;
  173. if (SUCCEEDED(pCPC->FindConnectionPoint(iid, &pCP)))
  174. {
  175. ASSERT_POINTER(pCP, IConnectionPoint);
  176. if (SUCCEEDED(pCP->Unadvise(dwCookie)))
  177. bSuccess = TRUE;
  178. pCP->Release();
  179. }
  180. pCPC->Release();
  181. }
  182. // If we failed, undo the earlier AddRef.
  183. if (!bRefCount && !bSuccess)
  184. pUnkSink->Release();
  185. return bSuccess;
  186. }
  187. /////////////////////////////////////////////////////////////////////////////
  188. // Force any extra compiler-generated code into AFX_INIT_SEG
  189. #ifdef AFX_INIT_SEG
  190. #pragma code_seg(AFX_INIT_SEG)
  191. #endif