There's always a need to listen to something, you need to create a program that runs in the background. Obviously, the service can meet your needs, but I have not written the service program, do not know how to operate the specific (should be exposed to several export functions to the operating system), but you can use other ways to achieve the desired effect.
Windows programs persist because of their message mechanism. The message loop extracts messages from the message queue, distributes them to Windows, and the system calls the window procedure. The message loop, which is similar to a dead loop, is the reason the program will not quit (for the time being).
1 while (GetMessage(&msg,NULL,0,0))
2 {
3 TranslateMessage(&msg);
4 DispatchMessage(&msg);
5 }
If you want to run a program in the background, this message loop is essential. Now it's all about getting a message, two choices:
Process directly before distributing the message, inserting a bar across the middle of the message loop.
Or use the original window process, let the operating system to execute.
In the middle of a bar, looked very simple and easy to understand, but it is said that the window process is performed by the operating system, because the operating system needs to allocate time slices to the program, if you do it, the operating system does not know how many time slices to allocate. Or use the original window process, create a window class, creating a window. All we need to do is , don't let that window show .... (In addition, I can not think how to let the window handle and the window process, which people know to be able to guide twos)
When CreateWindow is created, you can specify that the window display is not displayed. (previously tested on WIN32, did not pay attention to the WM experiment ...) Results create a window that is directly displayed)
01 hWnd = CreateWindow(szWindowClass,
02 szTitle,
03 WS_DISABLED,//丫的就是这个参数
04 CW_USEDEFAULT,
05 CW_USEDEFAULT,
06 CW_USEDEFAULT,
07 CW_USEDEFAULT,
08 NULL,
09 NULL,
10 hInstance,
11 NULL);
Later vs default generated code, it will be Showwindow,updatewindow, only the two lines can be commented out
1 //ShowWindow(hWnd, nCmdShow);
2 //UpdateWindow(hWnd);
The rest is findwindow,sendmessage, or postmessage.
Ps:
This is also true under. NET cf. Application.Run (Form f) is actually a message loop +f.show (). You can p/invoke a message loop yourself, and it's your own business to show it.