ConnectionAdvisor.cpp
上传用户:fda_625
上传日期:2007-01-02
资源大小:32k
文件大小:2k
源码类别:

文件操作

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "ConnectionAdvisor.h"
  3. /*----------------------------------------------------------------------------*/
  4. #ifdef _DEBUG
  5. #undef THIS_FILE
  6. static char THIS_FILE[]=__FILE__;
  7. #define new DEBUG_NEW
  8. #endif
  9. /*----------------------------------------------------------------------------*/
  10. CConnectionAdvisor::CConnectionAdvisor(REFIID iid) : m_iid(iid)
  11. {
  12. m_pConnectionPoint = NULL;
  13. m_AdviseCookie = 0;
  14. }
  15. /*----------------------------------------------------------------------------*/
  16. CConnectionAdvisor::~CConnectionAdvisor()
  17. {
  18. Unadvise();
  19. }
  20. /*----------------------------------------------------------------------------*/
  21. BOOL CConnectionAdvisor::Advise(IUnknown* pSink, IUnknown* pSource)
  22. {
  23. // Advise already done 
  24. if (m_pConnectionPoint != NULL)
  25. {
  26. return FALSE;
  27. }
  28. BOOL Result = FALSE;
  29. IConnectionPointContainer* pConnectionPointContainer;
  30. if (FAILED(pSource->QueryInterface(
  31. IID_IConnectionPointContainer,
  32. (void**)&pConnectionPointContainer)))
  33. {
  34. return FALSE;
  35. }
  36. if (SUCCEEDED(pConnectionPointContainer->FindConnectionPoint(m_iid, &m_pConnectionPoint)))
  37. {
  38. if (SUCCEEDED(m_pConnectionPoint->Advise(pSink, &m_AdviseCookie)))
  39. {
  40. Result = TRUE;
  41. }
  42. else
  43. {
  44. m_pConnectionPoint->Release();
  45. m_pConnectionPoint = NULL;
  46. m_AdviseCookie = 0;
  47. }
  48. }
  49. pConnectionPointContainer->Release();
  50. return Result;
  51. }
  52. /*----------------------------------------------------------------------------*/
  53. BOOL CConnectionAdvisor::Unadvise()
  54. {
  55. if (m_pConnectionPoint != NULL)
  56. {
  57. HRESULT hr = m_pConnectionPoint->Unadvise(m_AdviseCookie);
  58. // If the server is gone, ignore the error
  59. // ASSERT(SUCCEEDED(hr));
  60. m_pConnectionPoint->Release();
  61. m_pConnectionPoint = NULL;
  62. m_AdviseCookie = 0;
  63. }
  64. return TRUE;
  65. }
  66. /*----------------------------------------------------------------------------*/