Write a daemon on Windows (6) Windows services

Source: Internet
Author: User

Write a daemon on Windows (6) Windows services

The daemon is going to boot up, but also high-privilege, so I made it into a Windows service.

For official documentation on Windows services, you can see https://msdn.microsoft.com/en-us/library/windows/desktop/ms686953 (v=vs.85). aspx.

In general, the behavior of the service differs from the common application where there are the following points:

1. In general, services are run under System users and can of course be specified by themselves. This means that the service can run without a user login

2. In general, services are not user-interactive

3. Services can be managed through the Service Manager (start, stop, etc.)

The programming of a service program differs from a common application in the following points:

1. The main body of the service program execution is the ServiceMain function, and you need to call StartServiceCtrlDispatcher in the main function to register your ServiceMain function. That means your function code should be written in your ServiceMain function. StartServiceCtrlDispatcher not return until service is stopped

2. In the ServiceMain function, you should report your status to the Service Manager in a timely manner so that it will present itself to the user. In general, the flow of the ServiceMain function is this:

3. As a normal service, in response to a user's request, such as stop, this calls RegisterServiceCtrlHandler to register their own processing function, you can also handle the custom control code, in response to system requests, such as shutdown, This calls SetConsoleCtrlHandler to register its own handler function.

4. To make your own program a service, you have to register with Windows, call CreateService, stop the service, start the service, and more, see MSDN for examples.

I encapsulated these steps and made a singleton class--Obviously, a single case is well suited for a process that corresponds to a service--cwin32service:

classCwin32service: PublicSingleton<cwin32service>{friendclassSingleton<cwin32service>;Private: Cwin32service (void); Public:    ~cwin32service (void); Public:    BOOLInitConstserviceinfo&info); typedef std::vector<tstring>ArgList; typedef boost::function<BOOL(Constarglist&) > startingfunction;//arglist is the command-line argument for the applicationtypedef boost::function<void(Constarglist&) >servicefunction; voidRegister_starting_function (Conststartingfunction&f); voidRegister_running_function (Constservicefunction&f); voidRegister_control_code_function (ConstDWORD C,Constservicefunction&f); BOOLgo ();};

Above is its main external interface, the interface basically corresponds to the above ServiceMain flow:

L Init: Inform the service name and other information

L Startingfunction: Usually do the initialization work, load the configuration, and then start the worker thread

L Running_function: Usually wait for the exit signal and wait for the work thread to finish. The return of this function means that the service has ended

L Control_code_function: This is the control code to be processed. Generally we will have to deal with stop control code, but you do not set it okay

L Go: This is called StartServiceCtrlDispatcher registration ServiceMain function. Will not be returned until the end of the service. ServiceMain is the function and report status that is registered on the process call

Of course, we can not only complete the service program, but also to allow users to install services, start services, Notification Services, uninstall the service methods, I put these on the command line:

L Daemonsvc.exe–intsall

L Daemonsvc.exe–start

L Daemonsvc.exe–stop

L Daemonsvc.exe–remove

The implementation of these actions was carried out by me and put into the serviceutil inside.

Service is no way in the VC directly press F5 debugging, you start up like that is not a service, is a common application. In order to facilitate debugging, I divided the service into two modes: normal mode, service mode: Without parameters to start up is the normal mode, with the-SVC parameter up is the service mode (so you will see me implement the Service installation, the command line behind the-svc). Normal mode, do not go startservicectrldispatcher, direct servicemain, and do not deal with the service manager.

Normal mode development debugging finished, you can install the start service, look under the service mode is not normal, at this time can also be used to attach to the VC service process debugging.

Example of using a service class:

intMainintargcChar*argv[]) {Initlog ("",0, Log_debug); //try to enable debug privilege for querying other processes ' infoWindowsutil::set_privilege (Se_debug_name,true);    ServiceInfo si; Si.name= Tstr ("daemonsvc"); Si.display_name=Si.name; Cwin32service& svc =Cwin32service::get_instance_ref (); if(!svc.init (SI)) {Errorlog ("init service fail"); }    Else{InfoLog ("Init Service Success");        Svc.register_starting_function (starting);        Svc.register_running_function (running);        Svc.register_control_code_function (Service_control_stop, stopping); Svc.register_control_code_function ( $, restart); if(!Svc.go ()) {Errorlog ("Make service go fail"); }        Else{InfoLog ("everything is OK"); }    }    return 0;}

Source: Https://git.oschina.net/mkdym/DaemonSvc.git (Master) && Https://github.com/mkdym/DaemonSvc.git (for lifting).

Saturday, November 7, 2015

Write a daemon on Windows (6) Windows services

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.