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. Win32: Get message notification of other application's close/exit.

Win32: Get message notification of other application's close/exit.

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 8 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.
  • W Offline
    W Offline
    WindowsPistha
    wrote on last edited by
    #1

    Hello , My application needs to monitor all other running applications on the system. Is there some way I could get notified on exit of every application exe? The methods I could find: 1) Use PSAPI functions to get the list of running exes at frequent intervals. At each poll compare with the previous list to find which application/process has exited. Disadvantage: Requires constant polling, will take CPU time. 2) Set a global hook for WM_CLOSE message: Using this I would be able to get a notification when any application gets closed through the close button on the title bar Disadvantage: (-) Not all the applications are generating a WM_CLOSE message (Ex: Total Video Player Exe) (-) If the application was closed through the "Exit" menu or button (e.g. File->Exit) , I can't trap that message Is there any other better way that I missed? Please advise.

    T S J I 4 Replies Last reply
    0
    • W WindowsPistha

      Hello , My application needs to monitor all other running applications on the system. Is there some way I could get notified on exit of every application exe? The methods I could find: 1) Use PSAPI functions to get the list of running exes at frequent intervals. At each poll compare with the previous list to find which application/process has exited. Disadvantage: Requires constant polling, will take CPU time. 2) Set a global hook for WM_CLOSE message: Using this I would be able to get a notification when any application gets closed through the close button on the title bar Disadvantage: (-) Not all the applications are generating a WM_CLOSE message (Ex: Total Video Player Exe) (-) If the application was closed through the "Exit" menu or button (e.g. File->Exit) , I can't trap that message Is there any other better way that I missed? Please advise.

      T Offline
      T Offline
      Taran9
      wrote on last edited by
      #2

      WindowsPistha wrote:

      Not all the applications are generating a WM_CLOSE message (Ex: Total Video Player Exe) (-) If the application was closed through the "Exit" menu or button (e.g. File->Exit) , I can't trap that message

      Why don't you capture WM_QUIT instead? This is the only message that finally halts the Message Loop. But, you won't be able to track abnormal termination of an application with this.

      W 1 Reply Last reply
      0
      • T Taran9

        WindowsPistha wrote:

        Not all the applications are generating a WM_CLOSE message (Ex: Total Video Player Exe) (-) If the application was closed through the "Exit" menu or button (e.g. File->Exit) , I can't trap that message

        Why don't you capture WM_QUIT instead? This is the only message that finally halts the Message Loop. But, you won't be able to track abnormal termination of an application with this.

        W Offline
        W Offline
        WindowsPistha
        wrote on last edited by
        #3

        Taran9 wrote:

        Why don't you capture WM_QUIT instead? This is the only message that finally halts the Message Loop. But, you won't be able to track abnormal termination of an application with this.

        But our application needs to handle that case too.

        R _ 2 Replies Last reply
        0
        • W WindowsPistha

          Taran9 wrote:

          Why don't you capture WM_QUIT instead? This is the only message that finally halts the Message Loop. But, you won't be able to track abnormal termination of an application with this.

          But our application needs to handle that case too.

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          Just out of curiousity, what kind of an application are you writing?

          It is a crappy thing, but it's life -^ Carlo Pallini

          1 Reply Last reply
          0
          • W WindowsPistha

            Taran9 wrote:

            Why don't you capture WM_QUIT instead? This is the only message that finally halts the Message Loop. But, you won't be able to track abnormal termination of an application with this.

            But our application needs to handle that case too.

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            You will need to hook APIs like TerminateProcess in addition to installing a message hook.

            «_Superman_» I love work. It gives me something to do between weekends.

            1 Reply Last reply
            0
            • W WindowsPistha

              Hello , My application needs to monitor all other running applications on the system. Is there some way I could get notified on exit of every application exe? The methods I could find: 1) Use PSAPI functions to get the list of running exes at frequent intervals. At each poll compare with the previous list to find which application/process has exited. Disadvantage: Requires constant polling, will take CPU time. 2) Set a global hook for WM_CLOSE message: Using this I would be able to get a notification when any application gets closed through the close button on the title bar Disadvantage: (-) Not all the applications are generating a WM_CLOSE message (Ex: Total Video Player Exe) (-) If the application was closed through the "Exit" menu or button (e.g. File->Exit) , I can't trap that message Is there any other better way that I missed? Please advise.

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6
              1. Use WMI, as shown in this message[^]. The sample code is VBScript (and would need to be rewritten as below), but it works well enough.

              ' Get a reference to the WMI service
              Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
              ' Open a WMI query for __instancedeletionevents where the target instance is a Win32_Process
              Set colMonitoredProcesses = objWMIService. _
              ExecNotificationQuery("select * from __instancedeletionevent " & _
              "within 1 where TargetInstance isa 'Win32_Process'")
              ' This call will complete the next time a process is deleted.
              colMonitoredProcesses.NextEvent

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              W 1 Reply Last reply
              0
              • W WindowsPistha

                Hello , My application needs to monitor all other running applications on the system. Is there some way I could get notified on exit of every application exe? The methods I could find: 1) Use PSAPI functions to get the list of running exes at frequent intervals. At each poll compare with the previous list to find which application/process has exited. Disadvantage: Requires constant polling, will take CPU time. 2) Set a global hook for WM_CLOSE message: Using this I would be able to get a notification when any application gets closed through the close button on the title bar Disadvantage: (-) Not all the applications are generating a WM_CLOSE message (Ex: Total Video Player Exe) (-) If the application was closed through the "Exit" menu or button (e.g. File->Exit) , I can't trap that message Is there any other better way that I missed? Please advise.

                J Offline
                J Offline
                Joe Woodbury
                wrote on last edited by
                #7

                I don't know if this is possible, but I'd explore the possibility of getting a list of running exes. Duplicate their handle. Wait on that handle.

                Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                1 Reply Last reply
                0
                • S Stuart Dootson
                  1. Use WMI, as shown in this message[^]. The sample code is VBScript (and would need to be rewritten as below), but it works well enough.

                  ' Get a reference to the WMI service
                  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
                  ' Open a WMI query for __instancedeletionevents where the target instance is a Win32_Process
                  Set colMonitoredProcesses = objWMIService. _
                  ExecNotificationQuery("select * from __instancedeletionevent " & _
                  "within 1 where TargetInstance isa 'Win32_Process'")
                  ' This call will complete the next time a process is deleted.
                  colMonitoredProcesses.NextEvent

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  W Offline
                  W Offline
                  WindowsPistha
                  wrote on last edited by
                  #8

                  Stuart Dootson wrote:

                  1. Use WMI, as shown in this message[^]. The sample code is VBScript (and would need to be rewritten as below), but it works well enough.

                  Does this required WMI service running in the machine. If it is turned off , will i able to catch this event "__InstanceCreationEvent" Thanks

                  L 1 Reply Last reply
                  0
                  • W WindowsPistha

                    Stuart Dootson wrote:

                    1. Use WMI, as shown in this message[^]. The sample code is VBScript (and would need to be rewritten as below), but it works well enough.

                    Does this required WMI service running in the machine. If it is turned off , will i able to catch this event "__InstanceCreationEvent" Thanks

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

                    WindowsPistha wrote:

                    Does this required WMI service running in the machine.

                    Yes, if you want your application to be capable of monitoring process creation/termination and also not rely on any Microsoft services then you will need to implement your own device driver. Typically anti-virus and other types of security products are using PsSetCreateProcessNotifyRoutine[^] to be notified of process creation and termination. An example of its usage is available here on codeproject: Detecting Windows NT/2K process execution[^] Best Wishes, -David Delaune

                    I 1 Reply Last reply
                    0
                    • L Lost User

                      WindowsPistha wrote:

                      Does this required WMI service running in the machine.

                      Yes, if you want your application to be capable of monitoring process creation/termination and also not rely on any Microsoft services then you will need to implement your own device driver. Typically anti-virus and other types of security products are using PsSetCreateProcessNotifyRoutine[^] to be notified of process creation and termination. An example of its usage is available here on codeproject: Detecting Windows NT/2K process execution[^] Best Wishes, -David Delaune

                      I Offline
                      I Offline
                      Identity Undisclosed
                      wrote on last edited by
                      #10

                      Hello, I am also looking for a similar solution. My Questions are: 1) The solution requires DDK to be installed. Where can I download DDK from ? 2) Will this solution work on Vista ? Thanks.

                      Top 10, Top ten, Top 10 lists, Top ten lists Top 10 about everything

                      L 1 Reply Last reply
                      0
                      • W WindowsPistha

                        Hello , My application needs to monitor all other running applications on the system. Is there some way I could get notified on exit of every application exe? The methods I could find: 1) Use PSAPI functions to get the list of running exes at frequent intervals. At each poll compare with the previous list to find which application/process has exited. Disadvantage: Requires constant polling, will take CPU time. 2) Set a global hook for WM_CLOSE message: Using this I would be able to get a notification when any application gets closed through the close button on the title bar Disadvantage: (-) Not all the applications are generating a WM_CLOSE message (Ex: Total Video Player Exe) (-) If the application was closed through the "Exit" menu or button (e.g. File->Exit) , I can't trap that message Is there any other better way that I missed? Please advise.

                        I Offline
                        I Offline
                        Identity Undisclosed
                        wrote on last edited by
                        #11

                        Hello, This is the solution I found from another source: ======================================================= Apart from WMI, a nice and elegant way to do that is to place a small DLL "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs" (see also http://support.microsoft.com/kb/197571 - "Working with the AppInit_DLLs registry value") DLLs listed in AppInit_DLLs will be loaded into every newly created process and will allow you to perform any notification via the DLL's 'DllMain(). Let me know, if you need help with source code.

                        Top 10, Top ten, Top 10 lists, Top ten lists Top 10 about everything

                        L 1 Reply Last reply
                        0
                        • I Identity Undisclosed

                          Hello, I am also looking for a similar solution. My Questions are: 1) The solution requires DDK to be installed. Where can I download DDK from ? 2) Will this solution work on Vista ? Thanks.

                          Top 10, Top ten, Top 10 lists, Top ten lists Top 10 about everything

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

                          Hello there.

                          Identity Undisclosed wrote:

                          1. The solution requires DDK to be installed. Where can I download DDK from ?

                          The marketing people over at Microsoft change the name of their products every few years which really drives me crazy. The Microsoft DDK is now called WDK (Windows Driver Kit[^]) and the Download Kits and Tools[^] page will eventually bring you to the direct download link.

                          Identity Undisclosed wrote:

                          1. Will this solution work on Vista ?

                          Yes the PsSetCreateProcessNotifyRoutine [^] callback is avilable for Windows Vista and the driver will function correctly. However, Vista is very strict about loading unsigned drivers. You will need to sign the driver in order for Vista to load it. Below are some methods to get around the issue during the development phase: Installing an Unsigned Driver during Development and Test[^] TESTSIGNING Boot Configuration Option[^] Best Wishes, -David Delaune

                          1 Reply Last reply
                          0
                          • I Identity Undisclosed

                            Hello, This is the solution I found from another source: ======================================================= Apart from WMI, a nice and elegant way to do that is to place a small DLL "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs" (see also http://support.microsoft.com/kb/197571 - "Working with the AppInit_DLLs registry value") DLLs listed in AppInit_DLLs will be loaded into every newly created process and will allow you to perform any notification via the DLL's 'DllMain(). Let me know, if you need help with source code.

                            Top 10, Top ten, Top 10 lists, Top ten lists Top 10 about everything

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

                            Just wanted to add some comments for your consideration. The AppInit_DLLs entry will not have any effect on applications which are not linked with user32.dll. Generally this isn't much of an issue because 99% of all usermode applications are linked against user32. However if your developing a security product then this might not be acceptable. Also you need to be aware that the future of AppInit DLLs is uncertain and is changing. Just like loading device drivers, future AppInit DLLs will have a code signature requirement. Microsoft has outlined this new behavior here: AppInit DLLs in Windows 7 and Windows Server 2008 R2[^] Best Wishes, -David Delaune

                            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