Search code examples
c#.netwinapiwindows-servicesshutdown

Send windows message to a Windows Service


Is there any tool to send (mimic) a windows message like 'WM_ENDSESSION' to a windows service?

OR

How can I send a windows message to a process using C#?

(I know only C#)

EDIT: Purpose: Basically I have to debug a windows service for fixing a bug that occurs only on system shut down.


Solution

  • Generally, services don't have windows (let alone message pumps) to receive a windows message.

    If the bug really does only happen on shutdown (as opposed to just stopping the service), it may be the case that something depends on a resource that is going away, which is not being handled gracefully (in which case, the bug fix might be to set up the service dependencies correctly). Have you tried using the remote debugging tools to attach to the process prior to shutdown?

    It's worth investigating if you can get the problem to occur without shutting down, perhaps when just stopping the service with the Service Control Manager (no need to do this programmatically, as this is a debugging scenario), in which case, you can breakpoint OnStop() in your service (I'm assuming C#) and watch what happens.