从可执行文件的路径获取服务名称 [英] Get service name from the path of its executable

查看:125
本文介绍了从可执行文件的路径获取服务名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有可执行文件的路径,该可执行文件是正在运行的服务应用程序。

I have the path to an executable which is a running service application.

例如: C:\Program Files(x86)\Someapp\somesvc.exe

我想停止并启动它,为此,我想获取 [this] 答案。

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?

推荐答案

使用 EnumServicesStatus() EnumServicesStatusEx() 来枚举已安装的服务。这将为您提供每个服务的名称,但不会提供其 ImagePath 。为此,可以使用 OpenService(SERVICE_QUERY_CONFIG) ,然后使用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().

这篇关于从可执行文件的路径获取服务名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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