python win32service-获取服务的触发启动信息 [英] python win32service - Getting triggered startup information for service

查看:553
本文介绍了python win32service-获取服务的触发启动信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

win32 API QueryServiceConfig2 函数支持SERVICE_CONFIG_TRIGGER_INFO结构来获取触发服务启动的事件。但是,python的 win32service.QueryServiceConfig2()不会列出此类值,例如参数选项。可以通过win32service模块获取该信息吗?

解决方案

不幸的是,没有。这是在 Python 3.5 PyWin32 v221 下运行的简单代码段:

 #!/ usr / bin / env python3 

导入win32service


如果__name__ == __main__:
用于dir(win32service)中的名称:
如果name.startswith( SERVICE_CONFIG_):
print(name,getattr(win32service,name))

输出


 (py35x64_test)e:\Work\Dev\StackOverflow\q046916726> c:\Work\Dev\ \VEnvs\py35x64_test\Scripts\python.exe a.py 
SERVICE_CONFIG_DELAYED_AUTO_START_INFO 3
SERVICE_CONFIG_DESCRIPTION 1
SERVICE_CONFIG_FAILURE_ACTIONS 2
SERVICE_CONFIG_FAILURE_ACTIONS_FLAG 4
PP b SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO 6
SERVICE_CONFIG_SERVICE_SID_INFO 5


我还检查了 win32con (这是另一个 PyWin32 模块,仅包含常量定义),但也没有运气。



然后,我看了 $ {PYWIN32_SRC_DIR} /pywin32-221/win32/src/win32service.i ,并注意到了 ChangeServiceConfig 2 (以及 QueryServiceConfig 2 ), InfoLevel 参数专门针对上述内容进行了检查常量,因此直接传递其值( 8 ),将引发异常( NotImplementedError )。



之前更进一步,让我们花一点时间来理解调用这样的 Python 包装器(例如 win32service.QueryServiceConfig2 )时会发生什么:


  1. 参数( Python 样式-如果有)转换为 C 样式

  2. C 函数使用上述转换后的参数调用

  3. 函数结果(或输出)参数)-如果有的话-会转换回 Python

对于 [MS.Docs]:ChangeServiceConfig2W函数,数据传回并通过参数(取决于 dwInfoLevel 的值, lpInfo 可以具有各种含义):





添加对所有这些参数的支持并不是一件容易的事,因此(至少目前不会)对它们进行处理。



有还有更多这样的示例,我想这是一个优先事项,因为没有多少人要求该功能,再加上 MS 的(不幸的?)设计决策,使功能具有如此复杂的行为。 / p>

作为一种替代方法(一种相当复杂的方法),您可以使用 [Python 3.Docs]:ctypes-A f Python的原始函数库,但是您必须定义我在 Python 中上面列出的所有结构(以扩展 ctypes.Structure )。




  • 作为补充,我曾经处于确切的情况:我不得不打电话给> [MS.Docs]:LsaLogonUser函数! !! 14个令人毛骨悚然的争论!!! )。显然,它不是由 PyWin32 导出的,因此我通过 ctypes 对其进行了调用,但是我不得不写:


    • 〜170 行代码来定义结构(仅适用于我的方案所需的结构)

    • 〜50 行以填充它们并调用函数

  • $ li $ b

win32 API QueryServiceConfig2 function supports the SERVICE_CONFIG_TRIGGER_INFO structure to get event(s) that trigger the service startup. However, python's win32service.QueryServiceConfig2() does not list such value as a parameter option. Is it possible to get that information with the win32service module?

解决方案

Unfortunately, no. Here's a simple code snippet ran under Python 3.5 and PyWin32 v221:

#!/usr/bin/env python3

import win32service


if __name__ == "__main__":
    for name in dir(win32service):
        if name.startswith("SERVICE_CONFIG_"):
            print(name, getattr(win32service, name))

Output:

(py35x64_test) e:\Work\Dev\StackOverflow\q046916726>"c:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" a.py
SERVICE_CONFIG_DELAYED_AUTO_START_INFO 3
SERVICE_CONFIG_DESCRIPTION 1
SERVICE_CONFIG_FAILURE_ACTIONS 2
SERVICE_CONFIG_FAILURE_ACTIONS_FLAG 4
SERVICE_CONFIG_PRESHUTDOWN_INFO 7
SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO 6
SERVICE_CONFIG_SERVICE_SID_INFO 5

I've also checked win32con (which is another PyWin32 module, that only contains constant definitions), but no luck either.

Then, I took a look at ${PYWIN32_SRC_DIR}/pywin32-221/win32/src/win32service.i, and noticed that for ChangeServiceConfig2 (and also QueryServiceConfig2), the InfoLevel argument is specifically checked against the above constants, and thus passing its value (8) directly, would raise an exception (NotImplementedError).

Before going further, let's spend a little bit understanding what happens when calling such a Python wrapper (like win32service.QueryServiceConfig2):

  1. Arguments (Python style - if any) are converted to C style
  2. C function is called with the above converted arguments
  3. Function result (or output arguments) - if any - are converted back to Python

For [MS.Docs]: ChangeServiceConfig2W function, data is transferred back and forth via arguments (depending on dwInfoLevel value, lpInfo can have various meanings):

Adding support for all those arguments is not exactly a trivial task, so they aren't handled (at least for the moment).

There are more examples like this one, I guess it's a matter of priority, as not many people requested the functionality, combined with MS's (unfortunate?) design decision to have functions with such complex behaviors.

As an alternative (a quite complex one), you can use [Python 3.Docs]: ctypes - A foreign function library for Python, but you'll have to define all the structures that I listed above in Python (to extend ctypes.Structure).

  • As a side note, I was in the exact situation once: I had to call [MS.Docs]: LsaLogonUser function (!!! 14 freaking arguments !!!). Obviously, it wasn't exported by PyWin32, so I called it via ctypes, but I had to write:
    • ~170 lines of code to define structures (only the ones that I needed for my scenario)
    • ~50 lines to populate them and call the function

这篇关于python win32service-获取服务的触发启动信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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