Send windows message to a Windows Service

前端 未结 7 1946
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 05:57

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#?

7条回答
  •  情话喂你
    2021-02-06 06:32

    I don't think there is a tool to send arbitrary messages because each message can have arbitrary LPARAM and WPARAM values.

    But the most useful tool surrounding windows messages is spy++. Spy++ is included with Visual Studio and it helps you see what messages are sent, window hierarchy, and more.

    You can send messages via C# with the SendMessage Win32 API. You can get the window handle it asks for by using a Win32 API like FindWindow or FindWindowEx.

    Edit (to match question's edit): Services are stopped by windows automatically on shutdown. So to fix your bug it sounds like you need to modify the code of the service itself to properly shut down.

    Edit2: Or if you want to stop the service you should use the Win32 API ControlService passing in SERVICE_CONTROL_STOP 0x00000001.

提交回复
热议问题