如何获取 Windows 服务的命令行参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6182801/
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 16:57:59  来源:igfitidea点击:

How do I get the command-line arguments of a Windows service?

c#.netwindowsservice

提问by Duat Le

I'm looking for a way to figure out the command-line arguments of anyWindows service.

我正在寻找一种方法来找出任何Windows 服务的命令行参数。

For a non-service process, the command-line arguments can be found in the Windows Task Manager, or programmatically by using WMI as shown in this post.

对于非服务过程中,命令行参数,可以在Windows任务管理器,如图中使用WMI发现,或以编程这个职位

Unfortunately, these two solutions don't work for a Windows service that is started by the ServiceController.Start(String[] args)method. Both of them show only the executable file path on the command-line, even though some arguments were passed in.

不幸的是,这两种解决方案不适用于由ServiceController.Start(String[] args)方法启动的 Windows 服务 。即使传入了一些参数,它们也仅在命令行上显示可执行文件路径。

  1. What is the difference between two scenarios (a service vs. a non-service process)?
  2. Is there a way to figure out the arguments of the Windows service?
  1. 两种场景(服务与非服务流程)之间有什么区别?
  2. 有没有办法找出 Windows 服务的参数?

I also tried creating a simple service that just logs any command-line arguments it has to the event log. I started it using "sc.exe start <my service> <arg1>"and verified that <arg1>was written to the event log.

我还尝试创建一个简单的服务,该服务仅将其具有的任何命令行参数记录到事件日志中。我开始使用它"sc.exe start <my service> <arg1>"并验证它<arg1>已写入事件日志。

However, none of the solutions has worked for me. I still only saw the path to the executable file. My OS version is Windows Server 2008 R2 SP1 x64 Enterprise.

但是,没有一个解决方案对我有用。我仍然只看到了可执行文件的路径。我的操作系统版本是 Windows Server 2008 R2 SP1 x64 Enterprise。

采纳答案by Simon Mourier

There are two types of arguments for services:

服务有两种类型的参数:

  • Arguments that were passed on the process start command line. You can get to those easily using Process Explorer, etc.
  • Arguments that were passed to the ServiceMain function. This is the WIndows API that a service is supposed to implement. The .NET equivalent is ServiceBase.OnStart. This is what is used when you do an SC START \[arguments\]. This has nothing to do with "command line process arguments".

The second type of parameters is probably only known by the service itself, if the implementation makes any use of it which is not the case for many services. I don't think Windows keep track of this when we look at low level Windows structures like the PEB: Process and Thread Structures(MSDN), even the undocumented parts of it, Undocumented functions of NTDLL.

第二种类型的参数可能只有服务本身知道,如果实现使用它,这对许多服务来说不是这种情况。当我们查看像 PEB: Process and Thread Structures(MSDN)这样的低级 Windows 结构时,我认为 Windows 不会跟踪这一点,甚至是其中未记录的部分, NTDLL 的未记录功能

回答by Michael Kennedy

You can find the service EXE file details and edit or just see the commandline options in the registry entry for the service. You'll find that under

您可以找到服务 EXE 文件的详细信息并进行编辑,或者仅查看服务注册表项中的命令行选项。你会发现在下面

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services

Be sure to restart the Serviceswindow if you decide to change this as it won't reread it live.

如果您决定更改此设置,请务必重新启动“服务”窗口,因为它不会实时重新阅读。

回答by Umesh CHILAKA

Try the Process Explorerapplication from Sysinternals

尝试来自SysinternalsProcess Explorer应用程序

It is like Task Manager, only it lists all the running processes. Select your service and see its properties.

它就像任务管理器,只是它列出了所有正在运行的进程。选择您的服务并查看其属性。

回答by chopikadze

  1. A service process is not started as a usual EXE file. Even more, a service process could be just a .dll file. See: Windows service(Wikipedia).

    Many appear in the processes list in the Windows Task Manager, most often with a username of SYSTEM, LOCAL SERVICE or NETWORK SERVICE, though not all processes with the SYSTEM username are services. The remaining services run through svchost.exe as DLLs loaded into memory.

  2. Just override the ServiceBase.OnStart(string[] args)method. See more: ServiceBase.OnStart(String[]) Method(MSDN)

  1. 服务进程不是作为通常的 EXE 文件启动的。更重要的是,服务进程可能只是一个 .dll 文件。请参阅:Windows 服务(维基百科)。

    许多出现在 Windows 任务管理器的进程列表中,最常见的用户名为 SYSTEM、LOCAL SERVICE 或 NETWORK SERVICE,但并非所有具有 SYSTEM 用户名的进程都是服务。其余服务通过 svchost.exe 作为 DLL 加载到内存中运行。

  2. 只需覆盖该ServiceBase.OnStart(string[] args)方法。查看更多: ServiceBase.OnStart(String[]) 方法(MSDN)