【问题标题】:calling a dll function that takes a pointer to a handle (win32) from python调用一个 dll 函数,该函数从 python 获取指向句柄(win32)的指针
【发布时间】:2014-04-25 09:08:21
【问题描述】:

我正在尝试为 WinXP 机器上的相机编写一个非常简单的控制器。而不是编写 c 代码,我想我会简单地使用 ctypes 来访问 dll。

要启动相机,你必须调用:

BeginHVDevice(int nDevice, HHV *pHandle)

*pHandle是一个指向相机句柄的指针,在.h文件中简单定义为

typedef 处理 HHV;

我认为以下应该可以工作

from ctypes import *
from ctypes.wintypes import *


ailt_lib = cdll.LoadLibrary("HVDAILT")
load_camera = ailt_lib.BeginHVDevice
load_camera.restype = c_int
load_camera.argtypes = [c_int, POINTER(HANDLE)]

def initDev(res=(800,600)):

    cam_int = c_int(1)
    cam_handle_type = POINTER(HANDLE)
    print cam_handle_type
    cam_handle = cam_handle_type()
    print cam_handle

    cam_stat = load_camera(cam_int, cam_handle )
    print cam_stat
    return cam_handle

但是,当我调用 initDev() 时,我得到一个 ValueError: 调用的过程没有足够的参数(缺少 8 个字节)或错误的调用约定。我很确定这意味着我没有生成要传递的兼容指针,但我无法弄清楚函数实际想要接收什么。

我花了几天时间搜索 stackoverflow,查看 ctypes 文档并尝试各种排列,但我没有找到答案。

【问题讨论】:

    标签: python ctypes


    【解决方案1】:

    似乎该函数使用stdcall 而不是cdecl 调用约定,即使用ctypes.WinDLL 而不是ctypes.CDLL。此外,它需要一个指向可以存储句柄的内存位置的指针,但是您向它传递了一个NULL 指针。而是将其传递给 wintypes.HANDLE 的引用。

    from ctypes import *
    from ctypes.wintypes import *
    
    ailt_lib = WinDLL("HVDAILT")
    load_camera = ailt_lib.BeginHVDevice
    load_camera.restype = c_int
    load_camera.argtypes = [c_int, POINTER(HANDLE)]
    
    def initDev(res=(800,600)):    
        cam_int = 1
        cam_handle = HANDLE()    
        cam_stat = load_camera(cam_int, byref(cam_handle))
        print 'cam_stat:', cam_stat
        print 'cam_handle:', cam_handle
        return cam_handle
    

    【讨论】:

    • 是的,这正是问题所在。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2018-12-31
    相关资源
    最近更新 更多