Sometimes, we need to write the program without the user login, as long as the Windows system started running, then we can write our program as a Windows service.
Services can be used by a variety of users, including local and remote users, with the ability to administer at a user's authorization level, and whether the user is physically connected to the computer running the application.
Next, I'll use a simple example to illustrate how to write a Windows service program in ATL.
First, we create a new project. As shown in Figure one, select "ATL COM AppWizard", the project name is: Servicedemo.
Figure I
Click "OK", appear figure two, select Service [EXE]. Click Finish.
Figure II
Complete the above steps, a "do nothing" service is done! Compile... Open the Control Panel-> Administrative Tools
-> "service", eh? How come the services we write are not listed in the Services Manager (Service Control Manager, short name (SCM))? Oh, I cheated? Don't worry, we still need to do some work.
First, let's introduce the code that the wizard generates for us:
The entry point of the program is the global function _tWinMain, take a closer look at this function, and we'll find that when we run the program, we can add parameters, such as: Servicedemo/regserver or Servicedemo-regserver, This is used for local server registration (register as locality S Register as Service erver); Servicedemo/service or Servicedemo-service, this is the registration of the service (regist ER as service); Servicedemo/unregserver or Servicedemo-unregserver, this is the deletion of the service. So, when we have written the service program, as long as the runtime with the parameter service, this time in the SCM will see our services. You can try to "start", "Stop", and change the way it starts in the SCM for this service.
Every time you encode a test, you have to add parameters to the command line to run the service before it can be listed in the SCM is it troublesome? Let me introduce a lazy method, select the VC IDE's menu Project-> Setting, and then select the custom build panel, as shown in Figure three:
Figure Three