Receive Windows Messages in a Service

前端 未结 2 350
你的背包
你的背包 2021-01-19 08:07

I have written a service in VC++. I followed the tutorial here. Now, I am trying to find out how to receive messages like DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE, WM_COP

相关标签:
2条回答
  • 2021-01-19 08:39

    Use RegisterServiceCtrlHandlerEx function with HandlerEx callback function.

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

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题