Creating Windows Startup Services via REG or Command Line
I have a Java program which needs to be a startup program that runs as administrator. It seems that cannot be done without making it a service. I have tried using HKLM\SYSTEM\CurrentControlSet\Services\Services\MyService
. I tried something similar to what Google Updater uses (they use ...\Services\gupdate
). The process does not start (or at least it stops right awa开发者_如何学JAVAy, which I cannot tell for sure.
I think it is something wrong with how I am using the registry because the service does not show up in msconfig.exe
under the Services tab. Also it doe not show up in the Control Panel "View local services" (Windows 7, found in the Start Menu search for "services")
I tried a much simpler approach found here. I create a .reg
file with these contents.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService]
"Description"="My Service starts the Special Process."
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService\Parameters]
"Application"="C:\\Test\\MyProcess.cmd"
I am willing to consider an alternative command-line alternative if necessary, but I like the registry approach because if I tell my installer to add certain registry items, it will automatically remove those items on uninstall.
Is there any reason that above .reg
file would not add an item to msconfig
that is named "MyService"?
The program which I wrote is written in Java. It does not have a GUI interface.
You can create a service by editing the registry, but (as should be apparent) you must have a service executable associated with the service. Reg Add
will allow for the addition of these keys/values.
The registry must be reloaded by the system before the service is recognized, I find a reboot gets the job done.
- Add the key
ServiceName
toHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\
. - Next add the following values within the
ServiceName
key:DisplayName - REG_SZ - Sample Service
Description - REG_SZ - This Service is the Hello World Service!
ErrorControl - REG_DWORD - 1 (decimal)
ImagePath - REG_EXPAND_SZ - C:\ProgramData\Program\service_executable.exe
ObjectName - REG_SZ - Username_For_Execution
(often LocalSystem)Start - REG_DWORD - 2 (decimal)
(this varies according to the desired start behavior)Type - REG_DWORD - 16 (decimal)
The following websites were helpful in decoding the meaning of the various values:
- Using the registry editor to change the service state
- What are the ErrorControl, Start and Type values under the Services subkeys?
Just root around inside the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\
key for more examples!
If you want to run a program with administrative privileges, there is another way instead of using service.
You can use Task Scheduler, for example.
Also command line is available: SCHTASKS /create
You can't create a service by manipulating the registry. Nor can you run an arbitrary application as a service.
To run an arbitrary program from within a service, use the srvany.exe service available in the Windows Server 2003 resource kit. See KB137890 for instructions. If you want to write your own service, see this.
To create a service you can use the sc command line tool, or the instsrv.exe tool from the Windows Server 2003 resource kit. Or use the CreateService Win32 API function.
精彩评论