Send message to a service process

Started by
2 comments, last by Rattrap 13 years, 4 months ago
I've been using events to communicate between my app and my watchdog service, but after I noticed some crash events in the app can sometimes cause it to corrupt the log file, I decided to have it also send error logs to the service which would write them out in case the app crashes.

Except I don't know how to send a string to a service. I only see how to send it control codes, but not data. I couldn't figure out how to use WM_COPYDATA even if I make the service impersonate the user account (which I already do to access the user's registry). What do? I thought of creating a hidden window to get an HWND but I don't even know if that would work, so I didn't want to put in the time before asking...
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)
Advertisement
One way would be to just send it through the message pump.

MSDN - SendMessge

You could just send the message using the HWND_BROADCAST. See the remarks section about creating messages for inter-application communication.

[edit]
Instead you might use MSDN - PostMessage as it doesn't wait for the function call to return, if you don't want any kind of acknowledgement.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

You can't send a window message to a service. Services don't have a window handle.
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)
While I've never tried it, the documentation would seem to suggest that it is possible (at least in a similar fashion as I described), because the message pump can receive messages that are just for the thread, not for any particular window.

From the MSDN -
Quote:
MSG structure
Members

hwnd
Type: HWND

A handle to the window whose window procedure receives the message. This member is NULL when the message is a thread message.


So while I'm not guaranteeing that SendMessage/PostMessage are the correct method of sending the message (MSDN - BroadcastSystemMessageEx or MSDN - PostThreadMessage looks like they might be candidates as well), I don't see any reason why it can't receive a message, just because it doesn't have a window handle.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

This topic is closed to new replies.

Advertisement