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. Help me to use windows hook exactly.

Help me to use windows hook exactly.

Scheduled Pinned Locked Moved C / C++ / MFC
c++debugginghelptutorial
6 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.
  • M Offline
    M Offline
    marcoslav
    wrote on last edited by
    #1

    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

    V F 2 Replies Last reply
    0
    • M 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

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      Uninitialized WndHook value can cause problems in your InstallHook function. You should initialize it like this:

      HHOOK WndHook = NULL;
      

      What do you mean by "my client application can not call InstallHook"?

      F M 2 Replies Last reply
      0
      • M 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

        F Offline
        F Offline
        FarPointer
        wrote on last edited by
        #3

        Well 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/

        1 Reply Last reply
        0
        • V Viorel

          Uninitialized WndHook value can cause problems in your InstallHook function. You should initialize it like this:

          HHOOK WndHook = NULL;
          

          What do you mean by "my client application can not call InstallHook"?

          F Offline
          F Offline
          FarPointer
          wrote on last edited by
          #4

          HHOOK WndHook is a global variable and by default its zero. Regards, FarPointer Blog:http://farpointer.blogspot.com/

          1 Reply Last reply
          0
          • V Viorel

            Uninitialized WndHook value can cause problems in your InstallHook function. You should initialize it like this:

            HHOOK WndHook = NULL;
            

            What do you mean by "my client application can not call InstallHook"?

            M Offline
            M Offline
            marcoslav
            wrote on last edited by
            #5

            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 ;)

            V 1 Reply Last reply
            0
            • M marcoslav

              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 ;)

              V Offline
              V Offline
              Viorel
              wrote on last edited by
              #6

              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 and TlsSetValue can help).

              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