Start any program as a Windows Service

Source: Internet
Author: User

When the company server is started every day, it does not log on to the system. Therefore, to run a specific program, you only need to start it as a Windows service.

We do not need to modify the original program, but need to communicate with SCM (Service Control Manager). We need to create another windows service program so that it can call our target program.

Some clients on the internet can register an exetool as a service tool (such as srvinstw.exe, instsrv.exe, and srvany.exe), but it does not work, or it only works for programs with the SCM Communication function.

 

The following is the windows service implementation of C #.

Implement the OnStart and OnStop functions in the ServiceBase derived class.

                            EventLog log =            Thread thread =            Process proc =            exePath =                           log.Source = ;               dir =             ExeConfigurationFileMap map =              map.ExeConfigFilename = dir + ;              Configuration config =             exePath = config.AppSettings.Settings[].Value;                OnStart(                                 (thread ==                       thread =  Thread(startExe);                                                                   (proc !=  && !                      proc.Kill();                    (thread !=  && thread.ThreadState ==                      thread.Abort();                                                                proc =                  ProcessStartInfo startInfo =  ProcessStartInfo(exePath);                  proc.StartInfo =                     }

1. A new thread is required to start the target program. If it is started in the service thread, communication between the service and SCM will be blocked, resulting in service Startup failure.

2. All exceptions that may be thrown in OnStart and OnStop must be caught. Otherwise, if an exception occurs during startup, the system will prompt that the service is started and stopped.

3. The target program to be started can only run with the SYSTEM account, so the program interface is invisible.

4. The service program can also be implemented using C ++, and at least two virtual functions OnStart and OnStop can be implemented.

 

There are two ways to install the service:

1. Write and install/uninstall programs and call functions in windows API, CreateService () and DeleteService ()

2. Use the. Net frameworktool installutil.exe, which is generally located at % SystemRoot % \ Microsoft. NET \ Framework \ version \

Install the service: installuitl [Service Program path]

Uninstall service: installuitl/u [Service Program path]

 

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.