Writing Windows Services in ATL

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.