The program in this appendix is just a framework program without any function, the program itself is an MFC dialog box, but also an NT service, you can enter the Windows, the user before landing to start running.
The name of the service, display name, and dependent services can be modified in a string table in the resource, corresponding to the Ids_szservicename, Ids_szservicedisplayname, ids_szdependencies Note that you cannot store an empty string in string table, so I put a space in the ids_szdependencies without a dependent service;
Registering the service and uninstalling the service only requires command-line arguments-install and-remove at run time; for ease of debugging, a parameter-debug is attached that can be started without the NT Service Manager. The code for NT services comes from MSDN.
For non-MFC dialog box programs, you can simplify a lot of code; the MFC dialog box handles a wm_endsession message more than the Non-mfc dialog box, or the program loses its response after the user logs off and lands again. This is the focus of this procedure.
At the beginning of the program used SetCurrentDirectory to set the current directory for the module is the directory, otherwise the current directory is the NT Service Manager directory, so when debugging need to note that the current directory is not the project directory, but the engineering directory under the Debug directory, which is very convenient.
This program also increased the system tray function, for the system tray, this program handled two special cases, one is the system tray pop-up menu in the loss of focus will automatically disappear, another is in the Explorer.exe crash rebuild system tray also automatically rebuilt.
Use the-debug parameter to run with the normal program, omit; run and stop this service can be done in NT Service Manager or console command net Shart/stop, if you are willing to add some code, you can also start directly. You can also use the Exit menu in the system tray to stop this service.
NT services are often used for unmanned operation, so should also avoid users accidentally quit the program, so I will minimize the button and turn off the function of the button into a hidden, restore display and exit programs can use the System tray menu.
Users who are unfamiliar with NT services need to be aware that the NT service will start running before the user logs on, so for example, the connection to remote sharing should be handled as follows:
1. Add the service name of the network communication to the ids_szdependencies, so as to ensure that the network communication service starts before the secondary service;
2. Because the user has not logged in, so there is no default username, so you should provide a username and password to access remote sharing. Of course these are not the things that this procedure should explain.
Many articles say that to display the interface in NT service programs, you must Openwindowstation (_t ("winsta0") ...), but I did not do that, which is a question.
Finally, in order to avoid legal disputes, we hereby declare that this code is written in the spare time, although it has been used in some commercial projects, but ownership does not belong to any company, and I do not need any ownership.
Test Platform: Win2K pro,vc++6.0 + SP5
This article supporting source code