How to use C to register a Windows Service Program (Analysis of Java service wrapper code)

Source: Internet
Author: User

How to use C to register a Windows ServiceProgram(Analysis of Java service wrapperCode)

Prerequisites:
Download the Java service wrapperSource code.

Because Java service wrapper provides both Windows and Linux implementations, the code in the source folder is divided into two parts.
To register the Windows Service Code in windows, goWrapper_win.cFile.
After reading the code, you will be interested in the author's professionalism.Profound experience,There are more than 300 lines of historical changes in the wrapper_win.c file, which details the changes of each change.. The specific implementation methods are also described in detail, so it is basically no effort to look at such code.

If you have to consider the code as a pleasure, the source code of Java service wrapper is regarded as one.

Next, let's take a look at how it registers an EXE file for Windows Services.

The entry main function in wrapper_win.c is defined as follows:

Void _ crtapi1 main (INT argc, char ** argv )//Note the modifier _ crtapi1 defined by main.

Run the following commands based on the input parameters of argv: , Register, cancel, start, stop Windows service operations:
The Code is as follows:
If (! _ Stricmp (argv [1], "-I") |! _ Stricmp (argv [1], "/I ")){
Result = wrapperinstall (argc, argv); // register the service
} Else if (! _ Stricmp (argv [1], "-R") |! _ Stricmp (argv [1], "/R ")){
Result = wrapperremove (); // cancel registration
} Else if (! _ Stricmp (argv [1], "-T") |! _ Stricmp (argv [1], "/t ")){
Result = wrapperstartservice (); // start the service
} Else if (! _ Stricmp (argv [1], "-P") |! _ Stricmp (argv [1], "/P ")){
Result = wrapperstopservice (true); // stop the service.
}

We focus on the implementation of Windows Service Registration, that is, the wrapperinstall (argc, argv) method.
Step 1: Use Openscmanager Winapi obtains the handle of a Service Control Manager.
Step 2: Use Createservice Winapi creates a service with many parameters, which are important: Service name, service type, executable file path And so on
Step 3: Add registration information in the Registry: the code is as follows:
Sprintf (regpath, "System // CurrentControlSet // services // % s" , Wrapperdata-> ntservicename );
// Ntservicename indicates the service name displayed in the Service Manager.
If (wrapperdata-> ntservicedescription! = NULL & strlen (wrapperdata-> ntservicedescription)> 0)
& (Regopenkeyex (HKEY_LOCAL_MACHINE, regpath, 0, key_write, (phkey) & hkey) = error_success )){
Regsetvalueex (hkey, "Description", (DWORD) 0, (DWORD) REG_SZ,
(Const unsigned char *) wrapperdata-> ntservicedescription,
(Strlen (wrapperdata-> ntservicedescription) + 1 ));
Regclosekey (hkey );
}

The process is actually very simple, right?

There are two excellent articles that explain how a simple service application can be created.

    • Creating a simple Win32 service in C ++ by Thompson Nigel.
    • Five steps to writing Windows Services in C by Yevgeny menaker.

The following describes how to use the JNI and Java classes as well as the implementation of registration service interaction.

Other: Zhou Xingxing
Void _ crtapi1 main (INT argc, char ** argv )//Note the modifier _ crtapi1 defined by main.
The above keywords_ Crtapi1 usage can be described in detail?

Tian Chunfeng 20041221 Modification

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.