首页 > 解决方案 > 选择 ctypes dll 加载机制

问题描述

我正在研究一些可以使用 ctypes 访问某些 dll 功能的代码。库加载是通过以下方式完成的:

U2_C_INTERFACE = ctypes.cdll.LoadLibrary("mydllname")

这适用于原始开发人员 Windows 10 机器(和其他机器)。但是在我自己的机器上调用库方法时出现了这个错误:

Procedure called with not enough arguments (24 bytes missing) or wrong calling convention

谷歌搜索后,我可以修复它,将 dll 加载方法更改为:

U2_C_INTERFACE = ctypes.windll.LoadLibrary("mydllname")

这适用于我自己的 Windows 10 机器(和其他机器)。但它在原来的 Windows 10 机器上不起作用,我们在调用库方法时遇到了另一个错误:

Procedure probably called with too many arguments (24 bytes in excess)

有没有办法以编程方式知道何时使用cdll.LoadLibrary以及何时使用windll.LoadLibrary

这是麻烦的代码,调用o_session是抛出注释异常

if U2_C_INTERFACE is None:
    # U2_C_INTERFACE = ctypes.cdll.LoadLibrary("uvic32")
    U2_C_INTERFACE = ctypes.windll.LoadLibrary("uvic32")

subkey  = c_char_p(bytes(0))
status  = c_long(0)
host_l  = bytes(servidor_uv,    servidor_universe.devolver_sistema_codificacion())
user_l  = bytes(usuario_uv,     servidor_universe.devolver_sistema_codificacion())
pass_l  = bytes(password_uv,    servidor_universe.devolver_sistema_codificacion())
acc_l   = bytes(cuenta_uv,      servidor_universe.devolver_sistema_codificacion())

# Gestión de apertura de sesión
o_session = U2_C_INTERFACE.ic_universe_session

o_session.argtypes = (
    c_char_p, c_char_p, c_char_p,
    c_char_p, c_char_p, _ctypes.POINTER(c_long))
res = o_session(
    c_char_p(host_l), c_char_p(user_l), c_char_p(pass_l),
    c_char_p(acc_l), subkey, _ctypes.byref(status))

我们使用的 Python 版本是 3.4.0(32 位)

Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32

我的 ctypes 版本是 1.1.0。

标签: pythonwindowsctypesloadlibrary

解决方案


推荐阅读