Sign in to follow this  

Send message to a service process

Recommended Posts

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...

Share this post

Link to post
Share on other sites
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.

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.

Share this post

Link to post
Share on other sites
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 -

MSG structure

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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this