Search code examples
c++windowsservicewxwidgetstray

Tray icon app to be closed from service


So the title is quite self explanatory.

I have this one service written in C++ that creates a tray icon with CreateProcessAsUser and everything seems to work fine.

The problem comes when stopping the service, which I also want to stop the tray icon application but this has to be done gently, as this application may recieve some data that has to save to a file before exiting.

If useful, the wxWidgets API may be used, but all the process creation is now made with the Windows API, even the tray icon itself is made with wxWiidgets in mind, as well as the service.

Any advice is welcomed!


Solution

  • The tray app needs a way of receiving a signal when it should exit gracefully.

    If the tray app already has a window, you can send it a WM_CLOSE or WM_QUIT message. Window messages can't be sent over session boundaries, though. But, if you can change the tray app, you can make it accept an optional command-line parameter that just sends the message to a previous instance of the app and then exits. And then the service can run a new instance of the app with that parameter when needed. Or, if you can't change the app, then just run a second instance of your own service EXE, or another helper app that you write.

    Otherwise, if the tray app doesn't have a window yet, but you can change the app, then add a hidden window. Or better, add another IPC mechanism, like a named kernel event via CreateEvent() that the app can wait on, or a pipe, or a socket, etc. Something that the service can then send a signal to directly.

    Otherwise, if you can't change the tray app, and if it doesn't have a window you can send messages to, then you are stuck using TerminateProcess(), which is not graceful.