如果我尝试通过win32service.OpenService()(以管理员身份运行)控制甚至打开一些服务,我得到" pywintypes.error:(5,' OpenService',& #39;访问被拒绝。')"。但是,从服务控制台控制相同的服务是成功的。这是为什么?以下是复制问题的示例代码:
import win32service as ws
def get_handle(service_name):
# service_name is the internal service name, not the display name.
hSCManager = ws.OpenSCManager(None, None, ws.SC_MANAGER_ALL_ACCESS)
return ws.OpenService(hSCManager, service_name, ws.SERVICE_ALL_ACCESS)
sh = get_handle("CertPropSvc") # Certificate Propagation, same problem with
# BitLocker Drive Encryption Service (BDESVC)
答案 0 :(得分:1)
通过在打开SCM和服务时请求较低权限来解决问题:
import win32service as ws
def get_handle(service_name):
# service_name is the internal service name, not the display name.
# SC_MANAGER_CONNECT is enough.
hSCManager = ws.OpenSCManager(None, None, ws.SC_MANAGER_CONNECT)
# SERVICE_CHANGE_CONFIG is enough.
return ws.OpenService(hSCManager, service_name, ws.SERVICE_CHANGE_CONFIG)
sh = get_handle("CertPropSvc") # Certificate Propagation, BitLocker Drive
# Encryption Service (BDESVC) can also be used.