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]