Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Hooks (WH_CALLWNDPROC)

Hooks (WH_CALLWNDPROC)

Scheduled Pinned Locked Moved C / C++ / MFC
iosperformancehelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 1492767
    wrote on last edited by
    #1

    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

    B 1 Reply Last reply
    0
    • U User 1492767

      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

      B Offline
      B Offline
      Blake Miller
      wrote on last edited by
      #2

      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.

      M 1 Reply Last reply
      0
      • B Blake Miller

        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.

        M Offline
        M Offline
        Mike Beckerleg
        wrote on last edited by
        #3

        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

        U 1 Reply Last reply
        0
        • M Mike Beckerleg

          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

          U Offline
          U Offline
          User 1492767
          wrote on last edited by
          #4

          Thanks for the excellent suggestions. Works great now

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups