Home > database >  How to get Notified when a background program get closed in Windows?
How to get Notified when a background program get closed in Windows?

Time:10-29

the Program entry point function is WinMain and it doesn't create a Window so it runs in the background. I want to get a notification when the program is closed like: by the user through task manager or through system shutdown so I can save some progress.

something like the windows messages WM_QUITE but I can't access that as far as I know cuz i don't create a window

CodePudding user response:

If there is no window, you may consider modifying the program registration as a DLL service plug-in.

The service also has no window and executes in the background.

The process is closed. Only the kernel callback knows that a process is killed. There will be no event whether the R3 process is closed.

Unless you have another process intermittently scanning the process list continuously.

Registering as a service at least you can receive event callbacks when your service is stopped by others.

CreateServiceW

https://learn.microsoft.com/zh-cn/windows/win32/services/installing-a-service

CodePudding user response:

Is the background program something that your own code started, using CreateProcess()? If so, you can use the process handle (hProcess in the PROCESS_INFORMATION structure) in functions like WaitForSingleObject() etc. to check if the process still runs.

  • Related