python win32service - Getting triggered startup in

2019-07-31 01:26发布

问题:

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?

回答1:

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):

  • Let's take a look to a value that is supported: e.g. SERVICE_CONFIG_PRESHUTDOWN_INFO:
    • lpInfo is a pointer to a [MS.Docs]: SERVICE_PRESHUTDOWN_INFO structure which only has a dwPreshutdownTimeout member (which is a simple DWORD)
  • On the other hand, SERVICE_CONFIG_TRIGGER_INFO:

    • lpInfo is a pointer to a [MS.Docs]: SERVICE_TRIGGER_INFO structure
      • pTriggers member is a pointer to a [MS.Docs]: SERVICE_TRIGGER structure
        • pDataItems member is a pointer to a [MS.Docs]: SERVICE_TRIGGER_SPECIFIC_DATA_ITEM structure

    which is waaay more complex (and note that all involved structures have other members as well, I only listed the ones that increase the nesting level).

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