Iconizer DLL.cpp
上传用户:songshun
上传日期:2007-01-04
资源大小:51k
文件大小:8k
源码类别:

按钮控件

开发平台:

Visual C++

  1. //===========================================================================
  2. //
  3. // HomeWork from Belgium Not licensed software  
  4. // 1999 - 2000 No rights reserved
  5. //
  6. //===========================================================================
  7. //
  8. // Project/Product : Iconizer DLL
  9. //  FileName : Iconizer DLL.cpp
  10. // Author(s) : Bart Gysens
  11. //
  12. // Description : Implementation file of the Iconize DLL
  13. //
  14. // Classes :
  15. //
  16. // Information :
  17. //   Compiler(s) : Visual C++ 6.0
  18. //   Target(s) : Windows 95/98 and Windows NT (x86)
  19. //   Editor : Visual C++ 6.0 internal editor
  20. //
  21. // History
  22. // Vers.  Date      Aut.  Type     Description
  23. //  -----  --------  ----  -------  -----------------------------------------
  24. // 1.00   22 05 99  BG    Create   Original
  25. //
  26. //===========================================================================
  27. #include "stdafx.h"
  28. #define DLLAPI_ICONIZER __declspec( dllexport )
  29. #include "iconizer DLL.h"
  30. #include "hooklist.h"
  31. #include "caption.h"
  32. #include "trayicon.h"
  33. #include "log.h"
  34. //===========================================================================
  35. // Macros and typedefs
  36. //===========================================================================
  37. #define WM_UNHOOK ( WM_APP + 0x2500 )
  38. #define GETDEFWNDPROC( h ) ( (WNDPROC) GetWindowLong( h, GWL_WNDPROC ) )
  39. //===========================================================================
  40. // Global variables
  41. //===========================================================================
  42. HINSTANCE g_hModule = NULL ;
  43. #pragma data_seg( "Shared" )
  44. HHOOK g_hHook = NULL ;
  45. CHookList g_HookList ;
  46. CCaption g_Caption ;
  47. CTrayIcon   g_TrayIcon ;
  48. #pragma data_seg()
  49. #pragma comment( linker, "/section:Shared, rws" ) // read, write and share
  50. //===========================================================================
  51. // 
  52. // Function : HookProc
  53. // Author(s) : Bart Gysens
  54. //
  55. // Description : Function that encapsulates the hook
  56. //
  57. // Comments : none
  58. //
  59. // History : 1.00  : Create
  60. //
  61. //===========================================================================
  62. LRESULT CALLBACK HookWndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam )
  63. {
  64. CHookItem * pItem;
  65. // Search for a known window, quit if unknown
  66. if ( ( pItem = g_HookList.GetItem( hWnd ) ) == NULL )
  67. return 0;
  68. // Hook messages
  69. switch( Msg )
  70. {
  71. case WM_UNHOOK:
  72. // Message send by DLL on unhook!
  73. // CLog::PutLog( "* WM_UNHOOK     * HWND = %08X * P: %08X *rn", hWnd, GETDEFWNDPROC( hWnd ) );
  74. SetWindowLong( hWnd, GWL_WNDPROC, (long) pItem->m_DefWndProc );
  75. g_HookList.DelItem( hWnd );
  76. SendMessage( hWnd, WM_NCPAINT, 1, 0 );
  77. return 0;
  78. case WM_NCDESTROY:
  79. // CLog::PutLog( "* WM_NCDESTROY  * HWND = %08X * P: %08X *rn", hWnd, GETDEFWNDPROC( hWnd ) );
  80. SetWindowLong( hWnd, GWL_WNDPROC, (long) pItem->m_DefWndProc );
  81. CallWindowProc( pItem->m_DefWndProc, hWnd, Msg, wParam, lParam );
  82. g_HookList.DelItem( hWnd );
  83. return 0;
  84. case WM_NCACTIVATE:
  85. g_Caption.OnNcActivate( hWnd, Msg, wParam, lParam, pItem );
  86. return TRUE;
  87. case WM_NCPAINT:
  88. g_Caption.OnNcPaint( hWnd, Msg, wParam, lParam, pItem );
  89. return 0;
  90. case WM_SETTEXT:
  91. g_Caption.OnSetText( hWnd, Msg, wParam, lParam, pItem );
  92. return TRUE;
  93. case WM_NCLBUTTONDOWN:
  94. g_Caption.OnNcLButtonDown( hWnd, Msg, wParam, lParam, pItem );
  95. return 0;
  96. case WM_NCLBUTTONUP:
  97. g_Caption.OnNcLButtonUp( hWnd, Msg, wParam, lParam, pItem );
  98. return 0;
  99. case WM_NCMOUSEMOVE:
  100. g_Caption.OnNcMouseMove( hWnd, Msg, wParam, lParam, pItem );
  101. return 0;
  102. case WM_NOTIFYICON:
  103. g_TrayIcon.OnNotifyIcon( hWnd, Msg, wParam, lParam, pItem );
  104. return 0;
  105. default:
  106. break;
  107. }
  108. // We didn't process the message, so process with default window procedure
  109. LRESULT RetVal = CallWindowProc( pItem->m_DefWndProc, hWnd, Msg, wParam, lParam );
  110. return RetVal;
  111. }
  112. //===========================================================================
  113. // 
  114. // Function : HookProc
  115. // Author(s) : Bart Gysens
  116. //
  117. // Description : Function that will replace the normal WndProc with
  118. // the HookWndProc procedure
  119. //
  120. // Comments : none
  121. //
  122. // History : 1.00  : Create
  123. //
  124. //===========================================================================
  125. LRESULT WINAPI CallWndHookProc( int nCode, WPARAM wParam, LPARAM lParam )
  126. {
  127. LRESULT RetVal = CallNextHookEx( g_hHook, nCode, wParam, lParam );
  128. if ( nCode == HC_ACTION )
  129. {
  130. LPCREATESTRUCT pCs;
  131. LPCWPSTRUCT    pCwp  = (LPCWPSTRUCT) lParam ;
  132. CHookItem *    pItem = g_HookList.GetItem( pCwp->hwnd );
  133. switch( pCwp->message )
  134. {
  135. case WM_NCCREATE:
  136. pCs = (LPCREATESTRUCT) pCwp->lParam;
  137. if (   ( ( pCs->style & WS_CAPTION ) == WS_CAPTION )
  138. && ( ( pCs->style & WS_MINIMIZEBOX ) == WS_MINIMIZEBOX )
  139. && ( ( pCs->style & WS_SYSMENU ) == WS_SYSMENU )
  140. && ( pCs->hwndParent == NULL )
  141. )
  142. {
  143. //CLog::PutLog( "* WM_NCCREATE   * HWND = %08X * P: %08X *rn", pCwp->hwnd, GETDEFWNDPROC( pCwp->hwnd ) );
  144. // Add item to hook list
  145. if ( g_HookList.GetItem( pCwp->hwnd ) == NULL )
  146. g_HookList.AddItem( CHookItem( pCwp->hwnd, HookWndProc, NULL ) );
  147. }
  148. break;
  149. case WM_NCPAINT:
  150. {
  151. CHookItem * pItem = g_HookList.GetItem( pCwp->hwnd );
  152. if ( ( pItem != NULL ) && ( pItem->m_DefWndProc == NULL ) )
  153. {
  154. pItem->m_DefWndProc = GETDEFWNDPROC( pCwp->hwnd );
  155. SetWindowLong( pCwp->hwnd, GWL_WNDPROC, (long) HookWndProc );
  156. }
  157. }
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. return RetVal;
  164. }
  165. //===========================================================================
  166. // 
  167. // Function : StartHook
  168. // Author(s) : Bart Gysens
  169. //
  170. // Description : Function to start system wide hook
  171. //
  172. // Arguments : None
  173. //
  174. // Return value : TRUE  = hook successful installed
  175. // FALSE = hook already installed
  176. //
  177. // Comments : none
  178. //
  179. // History : 1.00  : Create
  180. //
  181. //===========================================================================
  182. BOOL WINAPI StartHook( void )
  183. {
  184. if ( g_hHook != NULL )
  185. return FALSE;
  186. g_hHook = SetWindowsHookEx( WH_CALLWNDPROC, CallWndHookProc, g_hModule, 0 );
  187. return( g_hHook != NULL ); 
  188. }
  189. //===========================================================================
  190. // 
  191. // Function : StopHook
  192. // Author(s) : Bart Gysens
  193. //
  194. // Description : Function to stop system wide hook
  195. //
  196. // Comments : none
  197. //
  198. // History : 1.00  : Create
  199. //
  200. //===========================================================================
  201. BOOL WINAPI StopHook( void )
  202. {
  203. if ( g_hHook != NULL )
  204. {
  205. SendMessage( HWND_BROADCAST, WM_UNHOOK, 0, 0 );
  206. Sleep( 100 );
  207. if ( UnhookWindowsHookEx( g_hHook ) != 0 )
  208. g_hHook = NULL;
  209. }
  210. return( g_hHook == NULL );
  211. }
  212. //===========================================================================
  213. // 
  214. // Function : DllMain
  215. // Author(s) : Bart Gysens
  216. //
  217. // Description : Implementation of the main dll procedure
  218. //
  219. // Comments : none
  220. //
  221. // History : 1.00  : Create
  222. //
  223. //===========================================================================
  224. BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved )
  225. {
  226.     switch (ul_reason_for_call)
  227. {
  228. case DLL_PROCESS_ATTACH:
  229. g_hModule = (HINSTANCE) hModule;
  230. break;
  231. case DLL_PROCESS_DETACH:
  232. // g_HookList.~CHookList();
  233. // TODO: Major problem:
  234. // All the windows that where hooked will crash on dll process
  235. // detach, even if we have unhooked them. I have not yet found the
  236. // reason why this happens...
  237. break;
  238.     }
  239.     return TRUE;
  240. }