Use C ++ to compile Win32 service (1)

Source: Internet
Author: User
Both. NET and Delphi encapsulate services well, but writing services in C ++ is still very cumbersome. Write some helper classes first:

// Manage the Service handle
Class servicehandle {
Public:
Servicehandle (SC _handle h): H _ (h ){
If (! H _)
Throw syserror ();
}
~ Servicehandle () {: closeservicehandle (H _);}
Operator SC _handle () {return H _;}
PRIVATE:
SC _handle H _;
Servicehandle (const servicehandle &); // disallowed
Servicehandle & operator = (const servicehandle &); // disallowed
};

// Analyze command line parameters
Class outline line {
Public:
Using line (void ){
ARGs _ =: commandlinetoargvw (getcommandlinew (), & argcount _);
If (! ARGs _)
Throw syserror ();
Current _ = 1;
}
~ Using line (void ){
If (ARGs _)
: Globalfree (reinterpret_cast }
Const wchar_t * getnext (void ){
If (current _ <argcount _)
Return ARGs _ [Current _ ++];
Else
Return 0;
}
Int getcount (void) const {return argcount _-1 ;}
PRIVATE:
Wchar_t ** ARGs _;
Int argcount _;
Int current _;
};

Anything that needs to release resources (such as globalfree and closeservicehandle), I like to write a simple class for encapsulation and let the Destructor prevent me from forgetting to do this. :)
A single EXE can contain multiple services (though not common). Therefore, you need to maintain a list of multiple services, it is also easy to find the corresponding service class based on the service name (because the windows callback function can only be a static function, not a class method ). I use servicebase as the base class to represent a service. It inherits from this base class (template mode) in use and manages and maintains the servicebase list using serviceapplication, to install and uninstall services and parse the command line, serviceapplication should be a singleton, however, it seems that only the "service list" member variable can be made a static member (Using STD: Map <STD: wstring, servicebase *> as a variable type, it seems to be a good choice ).
It is worth noting that the API with 13 parameters: createservice () needs to be recorded in several places:

  • If the service_interactive_process flag is specified, the service is allowed to interact with the desktop. However, according to other documents, this service contains insecure factors (mainly improper elevation of permissions ), in future Windows versions, this feature may be canceled.
  • By default, the account for starting the service is LocalSystem. This account has high permissions. In XP and later versions, we recommend that you use the accounts with lower permissions of LocalService or NetworkService to start the service.
  • The service we create usually has some description, which can be implemented by using changeserviceconfig2 () after the service is successfully installed.
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.