Hooks (WH_CALLWNDPROC)
-
Hi guys, I'm writing a program that should monitor the "active" application's copying and pasting.I decided on using use a system-wide WH_CBTPROC hook and a thread-specific WH_CALLWNDPROC hook to capture the WM_PASTE and WM_COPY messages. The hooking works OK, the problem is that the WH_CALLWNDPROC causes the menus in "Microsoft Word" not to display. Am i using the hook wrong? (Maybe not passing the messages along). And it makes the programs realllllyy slowww. I'm running a Duron 800mhz, should it have such a big influence on performance? Any Ideas? Thanks The base code looks like this.
HINSTANCE hins; HHOOK hmsg = NULL; HHOOK hcbt = NULL; BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { hins=(HINSTANCE)hModule; switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } //--------------------------------------------------------------------------- LRESULT CALLBACK MsgProc(int nCode, WPARAM wParam, LPARAM lParam) { char *file = new char[100]; file = "c:\\test.txt"; fstream out(file,ios::out | ios::app); if (nCode < 0) { LRESULT RetVal = CallNextHookEx( hmsg, nCode, wParam, lParam ); return RetVal; } if (HC_ACTION==nCode) { CWPSTRUCT *msgInfo = (CWPSTRUCT*)lParam; switch (msgInfo->message) { case WM_COPY: out << "COPY" << endl; break; case WM_PASTE: out << "PASTE" << endl; break; } } LRESULT RetVal = CallNextHookEx( hmsg, nCode, wParam, lParam ); return RetVal; } //--------------------------------------------------------------------------- LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) { char *file = new char[100]; file = "c:\\test.txt"; fstream out(file,ios::out | ios::app); DWORD ProcessId; char szModName[MAX_PATH]; HWND current_window; HANDLE hProcess; SYSTEMTIME SystemTime; if (nCode == HCBT_ACTIVATE) { current_window = (HWND)wParam; GetWindowThreadProcessId( current_window,&ProcessId); hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ProcessId ); int length = GetModuleFileNameEx( hProcess, NULL, szModName,sizeof(szModName)); out << szModName << endl; if (hmsg == NULL) { hms
-
Hi guys, I'm writing a program that should monitor the "active" application's copying and pasting.I decided on using use a system-wide WH_CBTPROC hook and a thread-specific WH_CALLWNDPROC hook to capture the WM_PASTE and WM_COPY messages. The hooking works OK, the problem is that the WH_CALLWNDPROC causes the menus in "Microsoft Word" not to display. Am i using the hook wrong? (Maybe not passing the messages along). And it makes the programs realllllyy slowww. I'm running a Duron 800mhz, should it have such a big influence on performance? Any Ideas? Thanks The base code looks like this.
HINSTANCE hins; HHOOK hmsg = NULL; HHOOK hcbt = NULL; BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { hins=(HINSTANCE)hModule; switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } //--------------------------------------------------------------------------- LRESULT CALLBACK MsgProc(int nCode, WPARAM wParam, LPARAM lParam) { char *file = new char[100]; file = "c:\\test.txt"; fstream out(file,ios::out | ios::app); if (nCode < 0) { LRESULT RetVal = CallNextHookEx( hmsg, nCode, wParam, lParam ); return RetVal; } if (HC_ACTION==nCode) { CWPSTRUCT *msgInfo = (CWPSTRUCT*)lParam; switch (msgInfo->message) { case WM_COPY: out << "COPY" << endl; break; case WM_PASTE: out << "PASTE" << endl; break; } } LRESULT RetVal = CallNextHookEx( hmsg, nCode, wParam, lParam ); return RetVal; } //--------------------------------------------------------------------------- LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) { char *file = new char[100]; file = "c:\\test.txt"; fstream out(file,ios::out | ios::app); DWORD ProcessId; char szModName[MAX_PATH]; HWND current_window; HANDLE hProcess; SYSTEMTIME SystemTime; if (nCode == HCBT_ACTIVATE) { current_window = (HWND)wParam; GetWindowThreadProcessId( current_window,&ProcessId); hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ProcessId ); int length = GetModuleFileNameEx( hProcess, NULL, szModName,sizeof(szModName)); out << szModName << endl; if (hmsg == NULL) { hms
1. You are 'new' the data each time the function is called? Where do you ever delete your character buffers? 2. Each time HCBT_ACTIVATE is activated you set a windows hook, where do you release the window hook? 3. You write to the same file from each handler. Are you ensured that none of these will be called from multiple threads at the same time? 4. What CloseHandle? "When you are finished with the handle, be sure to close it using the CloseHandle function." 5. I would probably only set hIns on the DLL_PROCESS_ATTACH call, and not on the others.
-
1. You are 'new' the data each time the function is called? Where do you ever delete your character buffers? 2. Each time HCBT_ACTIVATE is activated you set a windows hook, where do you release the window hook? 3. You write to the same file from each handler. Are you ensured that none of these will be called from multiple threads at the same time? 4. What CloseHandle? "When you are finished with the handle, be sure to close it using the CloseHandle function." 5. I would probably only set hIns on the DLL_PROCESS_ATTACH call, and not on the others.
The constant calls to 'new' the character buffers (which you don't delete) and opening files are going to have a big performance hit. Doing that each time it will be very slow. Mike
-
The constant calls to 'new' the character buffers (which you don't delete) and opening files are going to have a big performance hit. Doing that each time it will be very slow. Mike
Thanks for the excellent suggestions. Works great now