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
Use RegisterServiceCtrlHandlerEx function with HandlerEx callback function.
Yes, as pointed by 0xC0000022L is better use IPC technique, for example named pipes - my favourite. :)
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.
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).