问题描述
我具有可执行文件的路径,该可执行文件是正在运行的服务应用程序。
I have the path to an executable which is a running service application.
例如: C:\Program Files(x86)\Someapp\somesvc.exe
我想停止并启动它,为此,我想获取答案。
I want to stop and start it, and for that I suppose I need to get the name of the service as it is outlined in [this] answer.
我可以通过遍历 HKLM\System\CurrentControlSet\Services
中所有服务的所有注册表项,并检查 ImagePath
注册表项与我的可执行文件路径匹配。
I might get this by iterating trough all the registry entries of all the services at HKLM\System\CurrentControlSet\Services
, and checking if the ImagePath
registry key matches my executable path.
此解决方案应该可行,但对我来说似乎是错误的。有更好的方法吗?
This solution should work, but it seems wrong to me. Is there a better way to do this?
推荐答案
使用或来枚举已安装的服务。这将为您提供每个服务的名称,但不会提供其 ImagePath
。为此,可以使用,然后使用ImagePath zh-cn / library / windows / desktop / ms684932.aspx rel = noreferrer> QueryServiceConfig()
。
Use EnumServicesStatus()
or EnumServicesStatusEx()
to enumerate installed services. That will give you the name of each service, but not its ImagePath
. To retrieve that, you can open each service using OpenService(SERVICE_QUERY_CONFIG)
and then get its ImagePath
using QueryServiceConfig()
.
这篇关于从可执行文件的路径获取服务名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!