通过 REG 或命令行创建 Windows 启动服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7465396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:06:32  来源:igfitidea点击:

Creating Windows Startup Services via REG or Command Line

windowsserviceregistrystartupadministrator

提问by Dorothy

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 away, which I cannot tell for sure.

我有一个 Java 程序,它需要是一个以管理员身份运行的启动程序。如果不将其作为服务,似乎就无法完成。我试过使用HKLM\SYSTEM\CurrentControlSet\Services\Services\MyService. 我尝试了类似于 Google Updater 使用的东西(他们使用...\Services\gupdate)。该过程没有开始(或者至少它立即停止,我无法确定。

I think it is something wrong with how I am using the registry because the service does not show up in msconfig.exeunder 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")

我认为我使用注册表的方式有问题,因为该服务没有显示在msconfig.exe“服务”选项卡下。它也不会出现在控制面板“查看本地服务”中(Windows 7,在开始菜单搜索“服务”中找到)

I tried a much simpler approach found here. I create a .regfile with these contents.

我尝试了在这里找到更简单的方法。我.reg用这些内容创建了一个文件。

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 .regfile would not add an item to msconfigthat is named "MyService"?

是否有任何原因使上述.reg文件不会向msconfig名为“MyService”的项目添加项目?

The program which I wrote is written in Java. It does not have a GUI interface.

我写的程序是用Java编写的。它没有 GUI 界面。

采纳答案by Harry Johnston

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 KB137890for instructions. If you want to write your own service, see this.

要从服务中运行任意程序,请使用Windows Server 2003 资源工具包中提供的 srvany.exe 服务。有关说明,请参阅KB137890。如果您想编写自己的服务,请参阅

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.

要创建服务,您可以使用 sc 命令行工具或 Windows Server 2003 资源工具包中的 instsrv.exe 工具。或者使用 CreateService Win32 API 函数。

回答by BB9z

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

也可以使用命令行:SCHTASKS /create

回答by Shrout1

You cancreate a service by editing the registry, but (as should be apparent) you must have a service executable associated with the service. Reg Addwill allow for the addition of these keys/values.

可以通过编辑注册表创建服务,但是(应该很明显)您必须有一个与服务关联的服务可执行文件Reg Add将允许添加这些键/值。

The registry must be reloaded by the system before the service is recognized, I find a reboot gets the job done.

在识别服务之前,系统必须重新加载注册表,我发现重新启动即可完成工作。

  • Add the key ServiceNameto HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\.
  • Next add the following values within the ServiceNamekey:
    • 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)
  • 将密钥添加ServiceNameHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\.
  • 接下来在ServiceName键中添加以下值:
    • 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(通常是本地系统)
    • Start - REG_DWORD - 2 (decimal)(这取决于所需的启动行为)
    • Type - REG_DWORD - 16 (decimal)

The following websites were helpful in decoding the meaning of the various values:

以下网站有助于解码各种值的含义:

Just root around inside the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\key for more examples!

只需在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\密钥内扎根即可获得更多示例!