Receive Windows Messages in a Service

岁酱吖の 提交于 2019-12-01 19:45:05

Uhm you simply create an ordinary message loop as you would if you wrote a pure C implementation of a Win32 windowed application - without any frameworks involved.

Example:

while(GetMessage(...)) ...

You can either use PeekMessage or GetMessage (see the linked docs). But the latter is more conventional and removes it from the queue of messages.

I.e. you don't even need a window. Every thread can have a message loop. So it will be blocking, but only the current thread. You gotta figure out on your own how to relay the information to the other thread requiring it.

The big however

But instead of working on compromising something that MS put up for you not to shoot yourself your lower body off, you should read about Shatter Attacks over on Wikipedia and use a proper IPC technique for a service (there are plenty available, from MMF to pipes to combinations with semaphores, mutexes and events).

This part is relevant if you intend to receive window messages on a user desktop but with your privileged context (which session separation should prevent anyway).

Use RegisterServiceCtrlHandlerEx function with HandlerEx callback function.

Yes, as pointed by 0xC0000022L is better use IPC technique, for example named pipes - my favourite. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!