views:

61

answers:

2

I'm working on a C++ Windows application, that runs as a process but not a windowed application (does not have WndProc). It has it's own message loop, which is used to process a quit message, and that's how we safely exit the application from within it's self.

What I want to do is somehow send a message to the process from another process, in order to tell the process to exit safely.

In Linux, we would do this by trapping signals, but I'm not sure how it's done on Windows.

A: 

Do you have the control over both processes i.e., do you have the code of both processes? If yes I suggest to expose a API to exit safely.

Vinay
Yes, have full code access. Just trying to figure out if there's a simple way of doing a sort of IPC for closing the process.
nbolton
There are many IPC mechanisms you can use in this situation. For instance, a simple one would be to have the main app call CreateEvent() at startup to create a named kernel event in an unsignaled state, and then your other app can use OpenEvent() and SetEvent() to signal it when needed. The message loop in the main app can then use MsgWaitForMultipleObjects() to detect both queued messages and event signals at the same time, and act according to whichever it reported during each loop iteration.
Remy Lebeau - TeamB
+1  A: 

PostThreadMessage can post messages to threads without requiring a window proc.

In the first process, do a GetCurrentThreadId to get a system wide id for the current thread. And somehow get that id to the second app. In the second app, OpenThread will convert to a thread handle, that you can then use with PostThreadMessage.

Do note that if your 'windowprocless' application ever pops up a message box, the message box enters its own modal message loop, which will silently destroy any thread messages. If any kind of window is ever created on the thread you would be far better off creating an invisible message window that messages can be sent to to control the app.

Chris Becke
PostThreadMessage() takes a thread ID as input, not a thread handle, so OpenThread() is not needed.
Remy Lebeau - TeamB
heh. even better.
Chris Becke