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

问题描述 投票:1回答:1

以下代码在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。

python winapi ctypes
1个回答
3
投票

您应该使用argtypesrestype,否则所有参数都默认为int并在64位中截断。此外,你不应该直接调用GetLastError,但使用ctypes.get_last_error()which缓存最后一个错误代码(执行调用后,解释器可能已经调用了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())
© www.soinside.com 2019 - 2024. All rights reserved.