Write a Windows daemon (6) Windows Service, windows daemon

Source: Internet
Author: User

Write a Windows daemon (6) Windows Service, windows daemon
Write a daemon on Windows (6) Windows Service

The daemon process requires high permissions because it needs to be started up, so I made it a Windows service.

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

 

In general, service behaviors differ from common applications in the following aspects:

1. In general, the service runs under the System user. You can also specify the service by yourself. That is to say, the service can run without user logon.

2. In general, services do not have user interaction.

3. Services can be managed through the Service Manager (start, stop, and so on)

The compiling of service programs is different from that of common applications in the following aspects:

1. The main body of service program execution is the ServiceMain function. You need to call StartServiceCtrlDispatcher in the main function to register your ServiceMain function. That is to say, your function code should be written in your ServiceMain function. StartServiceCtrlDispatcher will not be returned until the service is stopped

2. In the ServiceMain function, you must promptly report your status to the service manager so that it can display your status to users. Generally, the process of the ServiceMain function is as follows:

Class CWin32Service: public Singleton <CWin32Service> {friend class Singleton <CWin32Service>; private: CWin32Service (void); public :~ CWin32Service (void); public: bool init (const ServiceInfo & info); typedef std: vector <tstring> ArgList; typedef boost: function <bool (const ArgList &)> startingFunction; // ArgList is the application's command line parameter typedef boost: function <void (const ArgList &)> ServiceFunction; void register_starting_function (const StartingFunction & f ); void register_running_function (const ServiceFunction & f); void register_control_code_function (const DWORD c, const ServiceFunction & f); bool go ();};

The above is the main external interface, which basically corresponds to the above ServiceMain process:

L Init: Inform the service name and other information

L StartingFunction: Initialize the job, load the configuration, and start the working thread.

L running_function: Generally, it is waiting for the exit signal, waiting for the end of the working thread or something. This function indicates that the service is finished.

L control_code_function: the control code to be processed. We usually need to process the stop control code, but it doesn't matter if you don't set it.

L go: Call StartServiceCtrlDispatcher to register the ServiceMain function. It is not returned until the service ends. In ServiceMain, the registered functions and report status are called according to the process.

 

Of course, we can't just write the service program, but also let users install the service, start the service, notify the service, and uninstall the service. I provided these in the command line:

L DaemonSvc.exe-intsall

L DaemonSvc.exe-start

L DaemonSvc.exe-stop

L DaemonSvc.exe-remove

The implementations of these actions are all carried out in ServiceUtil.

 

The service cannot be directly debugged by pressing F5 in VC. What you start is not a service, but a common application. For convenience of debugging, I divided the service into two modes: Normal Mode, service mode: the common mode is started without parameters, the service mode is used with the-svc parameter (so you will see that-svc is followed by the command line when I install the implemented service ). In normal mode, do not go to StartServiceCtrlDispatcher, directly ServiceMain, and do not deal with the Service Manager.

After development and debugging in normal mode, you can install and start the service and check whether the service mode is normal. At this time, you can also use VC to attach the service process for debugging.

 

Example of using a service class:

int main(int argc, char * argv[]){    InitLog("", 0, LOG_DEBUG);    //try to enable debug privilege for querying other processes' info    WindowsUtil::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(200, restart);        if (!svc.go())        {            ErrorLog("make service go fail");        }        else        {            InfoLog("everything is OK");        }    }    return 0;}

 

Source code: https://git.oschina.net/mkdym/DaemonSvc.git (main) & https://github.com/mkdym/DaemonSvc.git (to improve the Force Grid ).

 

 

Saturday, November 7, 2015

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.