如何获取Windows服务的命令行参数? [英] How do I get the command-line arguments of a Windows service?

查看:408
本文介绍了如何获取Windows服务的命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

对于非服务过程中,可以在Windows任务管理器中找到命令行参数,或者使用WMI以编程方式使用命令行参数,如

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服务,由 ServiceController.Start(String [] args)方法启动。即使传递了一些参数,它们两个都只在命令行上显示可执行文件路径。

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.


  1. 有什么区别$两种情况(服务与
    a非服务过程之间)中的b $ b?

  2. 是否有
    的方法可以弄清楚$ b $的论点b Windows服务?

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

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.

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

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.

推荐答案

服务有两种类型的参数:

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".

第二种类型的参数可能仅由服务本身知道,如果实现对任何服务都没有用,则该实现不使用它。当我们查看低级Windows结构(例如PEB)时,我认为Windows不会跟踪此情况: 进程和线程结构 (MSDN),甚至是其中未记录的部分, NTDLL的未记录功能

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.

这篇关于如何获取Windows服务的命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆