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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to catch WM_CLOSE by using hook

How to catch WM_CLOSE by using hook

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
6 Posts 4 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.
  • N Offline
    N Offline
    N Q H
    wrote on last edited by
    #1

    Hi all, I have written a program which can monitor a specific program when it was closed.I used hook to do it and everything seem be ok but the problem is that I cannot hook WM_CLOSE message. I have tried to use WH_GETMESSAGE, WH_CBT, WH_CALLWNDPROC. But all of them seem not work properly! here is my code:

    hHook = SetWindowsHookEx(WH_CALLWNDPROC,fnHookProc,hInstDLL,tid);

    DLLEXPORT LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, LPARAM lParam)
    {

    LPMSG msg = (LPMSG)lParam;
    
       if(msg->message == WM\_CLOSE )
       {
    	  //do somethings here
    	
       }
    
       return CallNextHookEx(\_hHook,nCode,wParam, lParam);
    

    }

    anybody help me. thanks

    [N][Q][H]

    D N L 3 Replies Last reply
    0
    • N N Q H

      Hi all, I have written a program which can monitor a specific program when it was closed.I used hook to do it and everything seem be ok but the problem is that I cannot hook WM_CLOSE message. I have tried to use WH_GETMESSAGE, WH_CBT, WH_CALLWNDPROC. But all of them seem not work properly! here is my code:

      hHook = SetWindowsHookEx(WH_CALLWNDPROC,fnHookProc,hInstDLL,tid);

      DLLEXPORT LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, LPARAM lParam)
      {

      LPMSG msg = (LPMSG)lParam;
      
         if(msg->message == WM\_CLOSE )
         {
      	  //do somethings here
      	
         }
      
         return CallNextHookEx(\_hHook,nCode,wParam, lParam);
      

      }

      anybody help me. thanks

      [N][Q][H]

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      There must be something special about the WM_CLOSE message. Are you using a DLL? Are you calling SetWindowLong() in that DLL? Are you injecting the DLL into the process of the "specific window?"

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      N 1 Reply Last reply
      0
      • N N Q H

        Hi all, I have written a program which can monitor a specific program when it was closed.I used hook to do it and everything seem be ok but the problem is that I cannot hook WM_CLOSE message. I have tried to use WH_GETMESSAGE, WH_CBT, WH_CALLWNDPROC. But all of them seem not work properly! here is my code:

        hHook = SetWindowsHookEx(WH_CALLWNDPROC,fnHookProc,hInstDLL,tid);

        DLLEXPORT LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, LPARAM lParam)
        {

        LPMSG msg = (LPMSG)lParam;
        
           if(msg->message == WM\_CLOSE )
           {
        	  //do somethings here
        	
           }
        
           return CallNextHookEx(\_hHook,nCode,wParam, lParam);
        

        }

        anybody help me. thanks

        [N][Q][H]

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        [N][Q][H] wrote:

        DLLEXPORT LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, LPARAM lParam)

        You hook handler seems to be incorrect. For instance if you are using the WH_CALLWNDPROC, the handler should be like

        LRESULT CALLBACK CallWndProc(int nCode,WPARAM wParam, LPARAM lParam)
        {
        PCWPSTRUCT msg = (PCWPSTRUCT)lParam;

           if(msg->message == WM\_CLOSE )
           {
        	  //do somethings here
        	
           }	
           return CallNextHookEx(\_hHook,nCode,wParam, lParam);
        

        }

        nave [OpenedFileFinder] [My Blog]

        N 1 Reply Last reply
        0
        • N Naveen

          [N][Q][H] wrote:

          DLLEXPORT LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, LPARAM lParam)

          You hook handler seems to be incorrect. For instance if you are using the WH_CALLWNDPROC, the handler should be like

          LRESULT CALLBACK CallWndProc(int nCode,WPARAM wParam, LPARAM lParam)
          {
          PCWPSTRUCT msg = (PCWPSTRUCT)lParam;

             if(msg->message == WM\_CLOSE )
             {
          	  //do somethings here
          	
             }	
             return CallNextHookEx(\_hHook,nCode,wParam, lParam);
          

          }

          nave [OpenedFileFinder] [My Blog]

          N Offline
          N Offline
          N Q H
          wrote on last edited by
          #4

          ok, KeyboardProc must be renamed to fnHookProc. But this is not the cause. This just a mistake when I copy from my code.

          LRESULT CALLBACK LRESULT CALLBACK fnHookProc(int nCode,WPARAM wParam, LPARAM lParam)
          {
          PCWPSTRUCT msg = (PCWPSTRUCT)lParam;

             if(msg->message == WM\_CLOSE )
             {
          	  //do somethings here
          	
             }	
             return CallNextHookEx(\_hHook,nCode,wParam, lParam);
          

          }

          [N][Q][H]

          1 Reply Last reply
          0
          • D David Crow

            There must be something special about the WM_CLOSE message. Are you using a DLL? Are you calling SetWindowLong() in that DLL? Are you injecting the DLL into the process of the "specific window?"

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            N Offline
            N Offline
            N Q H
            wrote on last edited by
            #5

            Because this is a global hook which will monitor all message before they really reach to target window. So I think It doesn't need inject the dll to the target window. But I will try. I took a round from google and know that many people have the same problem with me when they try to catch the WM_CLOSE. But I still cannot find the solution.

            [N][Q][H]

            1 Reply Last reply
            0
            • N N Q H

              Hi all, I have written a program which can monitor a specific program when it was closed.I used hook to do it and everything seem be ok but the problem is that I cannot hook WM_CLOSE message. I have tried to use WH_GETMESSAGE, WH_CBT, WH_CALLWNDPROC. But all of them seem not work properly! here is my code:

              hHook = SetWindowsHookEx(WH_CALLWNDPROC,fnHookProc,hInstDLL,tid);

              DLLEXPORT LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, LPARAM lParam)
              {

              LPMSG msg = (LPMSG)lParam;
              
                 if(msg->message == WM\_CLOSE )
                 {
              	  //do somethings here
              	
                 }
              
                 return CallNextHookEx(\_hHook,nCode,wParam, lParam);
              

              }

              anybody help me. thanks

              [N][Q][H]

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              It's an old Win32 FAQ (see onWin32 grp[^]) (same method than on Win 9x)

              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