Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.1 KiB

  1. //
  2. // wineventrefilter - utility class to filter out reentrant WinEvent events
  3. //
  4. // Copyright (C) 1998 by Microsoft Corporation. All rights reserved.
  5. //
  6. typedef
  7. void CALLBACK FN_WinEventProc( HWINEVENTHOOK hEvent,
  8. DWORD event,
  9. HWND hwnd,
  10. LONG idObject,
  11. LONG idChild,
  12. DWORD idThread,
  13. DWORD dwmsEventTime );
  14. class WinEventReentrancyFilter
  15. {
  16. public:
  17. virtual ~WinEventReentrancyFilter() { }
  18. virtual void SetCallback( FN_WinEventProc * pWinEventProc ) = 0;
  19. virtual void HandleWinEvent( HWINEVENTHOOK hEvent,
  20. DWORD event,
  21. HWND hwnd,
  22. LONG idObject,
  23. LONG idChild,
  24. DWORD idThread,
  25. DWORD dwmsEventTime ) = 0;
  26. };
  27. WinEventReentrancyFilter * CreateWinEventReentrancyFilter();
  28. // Template class that makes this easier to use.
  29. //
  30. // If your existing code looks like...
  31. //
  32. // void CALLBACK MyWinEventProc( ... );
  33. //
  34. // ...
  35. //
  36. // HWINEVENTHOOK hHook = SetWinEventHook(
  37. // ...
  38. // MyWinEventProc
  39. // ... );
  40. //
  41. //
  42. // Change it to...
  43. //
  44. // // No changes to WinEventProc
  45. // void CALLBACK WinEventProc( ... );
  46. //
  47. // // * Add a new global - the template parameter is the name of your
  48. // // existing callback...
  49. // CWinEventReentrancyFilter< MyWinEventProc > g_WinEventReFilter;
  50. //
  51. // ...
  52. //
  53. //
  54. // // * Call SetWinEventHook using g_WinEventReFilter.WinEventProc
  55. // // instead of your callback. This will filter reentrant events,
  56. // // and pass them to your callback in the correct order.
  57. // HWINEVENTHOOK hHook = SetWinEventHook(
  58. // ...
  59. // g_WinEventReFilter.WinEventProc
  60. // ... );
  61. //
  62. //
  63. // It is acceptable to use multiple filters, provided that they all
  64. // use different callbacks. For example, this is allowed:
  65. //
  66. // void CALLBACK MyWinEventProc1( ... );
  67. // void CALLBACK MyWinEventProc2( ... );
  68. //
  69. // CWinEventReentrancyFilter< MyWinEventProc1 > g_WinEventReFilter1;
  70. // CWinEventReentrancyFilter< MyWinEventProc2 > g_WinEventReFilter2;
  71. //
  72. // ... but this is NOT allowed ...
  73. //
  74. // void CALLBACK MyWinEventProc( ... );
  75. //
  76. // CWinEventReentrancyFilter< MyWinEventProc > g_WinEventReFilter1;
  77. // CWinEventReentrancyFilter< MyWinEventProc > g_WinEventReFilter2;
  78. //
  79. template < FN_WinEventProc pCallback >
  80. class CWinEventReentrancyFilter
  81. {
  82. static
  83. WinEventReentrancyFilter * m_pFilter;
  84. public:
  85. CWinEventReentrancyFilter()
  86. {
  87. m_pFilter = CreateWinEventReentrancyFilter();
  88. if( m_pFilter )
  89. {
  90. m_pFilter->SetCallback( pCallback );
  91. }
  92. }
  93. BOOL Check()
  94. {
  95. return m_pFilter;
  96. }
  97. ~CWinEventReentrancyFilter()
  98. {
  99. if( m_pFilter )
  100. {
  101. delete m_pFilter;
  102. }
  103. }
  104. static
  105. void CALLBACK WinEventProc( HWINEVENTHOOK hEvent,
  106. DWORD event,
  107. HWND hwnd,
  108. LONG idObject,
  109. LONG idChild,
  110. DWORD idThread,
  111. DWORD dwmsEventTime )
  112. {
  113. if( ! m_pFilter )
  114. return;
  115. m_pFilter->HandleWinEvent( hEvent, event, hwnd, idObject, idChild,
  116. idThread, dwmsEventTime );
  117. }
  118. };
  119. template < FN_WinEventProc pCallback >
  120. WinEventReentrancyFilter * CWinEventReentrancyFilter< pCallback >::m_pFilter = NULL;