从Python 3调用Windows API时无效的句柄

时间:2019-03-17 18:18:25

标签: python winapi ctypes

以下代码在Python 2中运行良好:

import ctypes

def test():
    OpenSCManager      = ctypes.windll.advapi32.OpenSCManagerA
    CloseServiceHandle = ctypes.windll.advapi32.CloseServiceHandle

    handle = OpenSCManager(None, None, 0)
    print(hex(handle))
    assert handle, ctypes.GetLastError()
    assert CloseServiceHandle(handle), ctypes.GetLastError()

test()

它在Python 3中不起作用

0x40d88f90
Traceback (most recent call last):
  File ".\test1.py", line 12, in <module>
    test()
  File ".\test1.py", line 10, in test
    assert CloseServiceHandle(handle), ctypes.GetLastError()
AssertionError: 6

6表示无效的句柄。

此外,似乎在Python 2中检索到的句柄是较小的数字,例如0x100ffc0。 CloseServiceHandle并不特定。此句柄不能与任何服务功能一起使用。

两个Python版本都是64位本机Windows Python。

1 个答案:

答案 0 :(得分:3)

您应该使用argtypesrestype,否则所有参数默认为int并以64位截断。另外,您不应该直接调用GetLastError,而应使用ctypes.get_last_error()来缓存最后一个错误代码(执行调用后,解释器可能已经调用了Windows API,因此无法确定)。

这是一个可行的示例:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import ctypes


def test():
    advapi32 = ctypes.WinDLL("advapi32", use_last_error=True)
    OpenSCManager = advapi32.OpenSCManagerA
    OpenSCManager.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_ulong]
    OpenSCManager.restype = ctypes.c_void_p

    CloseServiceHandle = advapi32.CloseServiceHandle
    CloseServiceHandle.argtypes = [ctypes.c_void_p]
    CloseServiceHandle.restype = ctypes.c_long

    handle = OpenSCManager(None, None, 0)
    if not handle:
        raise ctypes.WinError(ctypes.get_last_error())
    print(f"handle: {handle:#x}")

    result = CloseServiceHandle(handle)
    if result == 0:
        raise ctypes.WinError(ctypes.get_last_error())

def main():
    test()


if __name__ == "__main__":
    sys.exit(main())