Help me to use windows hook exactly.
-
Purpose of my program is to get notification (a messagebox) whenever a new window is created on the operating system. In my win32 dll I put following code.
#include "stdafx.h" #include "hookdll.h" HHOOK WndHook; HINSTANCE hInst; __declspec(dllexport) bool InstallHook(void); __declspec(dllexport) bool UninstallHook(void); __declspec(dllexport) LRESULT CALLBACK WndHookProc(int, WPARAM, LPARAM); BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } hInst = (HINSTANCE)hModule; return TRUE; } // This is an example of an exported variable HOOKDLL_API int nHookdll=0; // This is an example of an exported function. HOOKDLL_API int fnHookdll(void) { return 42; } // This is the constructor of a class that has been exported. // see hookdll.h for the class definition CHookdll::CHookdll() { return; } __declspec(dllexport) bool InstallHook(void) { if(WndHook == NULL) WndHook = SetWindowsHookEx(WH_SHELL, (HOOKPROC)WndHookProc, hInst, 0); return (WndHook != NULL); } __declspec(dllexport) bool UninstallHook(void) { if(WndHook != NULL) { if(UnhookWindowsHookEx(WndHook)) WndHook = NULL; } return (WndHook == NULL); } __declspec(dllexport) LRESULT CALLBACK WndHookProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode<0) return CallNextHookEx(WndHook, nCode, wParam, lParam); /* my codes...*/ ::MessageBox(NULL,"Window created","here",MB_OK); return CallNextHookEx(WndHook, nCode, wParam, lParam); }
Now watch my client program.. MFC exe, dialog based. On its OkButton handler I put following code. I am not using load library and getProce address , because I am linking the library in the project settings and copying the hookdll in the debug directory of my application .void CHookclientDlg::OnOK() { InstallHook(); }
But my client application can not call InstallHook. Is this coding right or will it inform all the windows creation. (other applications , threads). Please tell me Marcoslav -
Purpose of my program is to get notification (a messagebox) whenever a new window is created on the operating system. In my win32 dll I put following code.
#include "stdafx.h" #include "hookdll.h" HHOOK WndHook; HINSTANCE hInst; __declspec(dllexport) bool InstallHook(void); __declspec(dllexport) bool UninstallHook(void); __declspec(dllexport) LRESULT CALLBACK WndHookProc(int, WPARAM, LPARAM); BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } hInst = (HINSTANCE)hModule; return TRUE; } // This is an example of an exported variable HOOKDLL_API int nHookdll=0; // This is an example of an exported function. HOOKDLL_API int fnHookdll(void) { return 42; } // This is the constructor of a class that has been exported. // see hookdll.h for the class definition CHookdll::CHookdll() { return; } __declspec(dllexport) bool InstallHook(void) { if(WndHook == NULL) WndHook = SetWindowsHookEx(WH_SHELL, (HOOKPROC)WndHookProc, hInst, 0); return (WndHook != NULL); } __declspec(dllexport) bool UninstallHook(void) { if(WndHook != NULL) { if(UnhookWindowsHookEx(WndHook)) WndHook = NULL; } return (WndHook == NULL); } __declspec(dllexport) LRESULT CALLBACK WndHookProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode<0) return CallNextHookEx(WndHook, nCode, wParam, lParam); /* my codes...*/ ::MessageBox(NULL,"Window created","here",MB_OK); return CallNextHookEx(WndHook, nCode, wParam, lParam); }
Now watch my client program.. MFC exe, dialog based. On its OkButton handler I put following code. I am not using load library and getProce address , because I am linking the library in the project settings and copying the hookdll in the debug directory of my application .void CHookclientDlg::OnOK() { InstallHook(); }
But my client application can not call InstallHook. Is this coding right or will it inform all the windows creation. (other applications , threads). Please tell me Marcoslav -
Purpose of my program is to get notification (a messagebox) whenever a new window is created on the operating system. In my win32 dll I put following code.
#include "stdafx.h" #include "hookdll.h" HHOOK WndHook; HINSTANCE hInst; __declspec(dllexport) bool InstallHook(void); __declspec(dllexport) bool UninstallHook(void); __declspec(dllexport) LRESULT CALLBACK WndHookProc(int, WPARAM, LPARAM); BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } hInst = (HINSTANCE)hModule; return TRUE; } // This is an example of an exported variable HOOKDLL_API int nHookdll=0; // This is an example of an exported function. HOOKDLL_API int fnHookdll(void) { return 42; } // This is the constructor of a class that has been exported. // see hookdll.h for the class definition CHookdll::CHookdll() { return; } __declspec(dllexport) bool InstallHook(void) { if(WndHook == NULL) WndHook = SetWindowsHookEx(WH_SHELL, (HOOKPROC)WndHookProc, hInst, 0); return (WndHook != NULL); } __declspec(dllexport) bool UninstallHook(void) { if(WndHook != NULL) { if(UnhookWindowsHookEx(WndHook)) WndHook = NULL; } return (WndHook == NULL); } __declspec(dllexport) LRESULT CALLBACK WndHookProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode<0) return CallNextHookEx(WndHook, nCode, wParam, lParam); /* my codes...*/ ::MessageBox(NULL,"Window created","here",MB_OK); return CallNextHookEx(WndHook, nCode, wParam, lParam); }
Now watch my client program.. MFC exe, dialog based. On its OkButton handler I put following code. I am not using load library and getProce address , because I am linking the library in the project settings and copying the hookdll in the debug directory of my application .void CHookclientDlg::OnOK() { InstallHook(); }
But my client application can not call InstallHook. Is this coding right or will it inform all the windows creation. (other applications , threads). Please tell me MarcoslavWell I guess you need to use the WH_CBT hook and check the LPARAM for the HCBT_CREATEWND. Regards, FarPointer Blog:http://farpointer.blogspot.com/
-
Uninitialized
WndHook
value can cause problems in yourInstallHook
function. You should initialize it like this:HHOOK WndHook = NULL;
What do you mean by "my client application can not call InstallHook"?
HHOOK WndHook is a global variable and by default its zero. Regards, FarPointer Blog:http://farpointer.blogspot.com/
-
Uninitialized
WndHook
value can cause problems in yourInstallHook
function. You should initialize it like this:HHOOK WndHook = NULL;
What do you mean by "my client application can not call InstallHook"?
-
Thanks it worked. But it went in to infinite loop, cos it was creating messagebox to show that some window was created. so for messagebox themselves also messagebox was getting displayed.. so infinite loop. My first virus I guess ;)
In order to avoid infinite loop, I think you can first try this:
static bool isMyMessageBox = false; if( ! isMyMessageBox) { isMyMessageBox = true; ::MessageBox(....); isMyMessageBox = false; }
If it works, you should take care it works in multi-threaded case too. (Probably
TlsGetValue
andTlsSetValue
can help).