calling a dll function that takes a pointer to a handle (win32) from python

StackOverflow https://stackoverflow.com/questions/23288781

  •  09-07-2023
  •  | 
  •  

Pregunta

I'm trying to write a very simple controller for a camera on a WinXP machine. Instead of writing c code, I thought I would simply use ctypes to access the dll.

To start the camera, you have to call:

BeginHVDevice(int nDevice, HHV *pHandle)

*pHandle is a pointer to a camera handle, which in the.h file is simply defined as

typedef HANDLE HHV;

I had thought that the following should work

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

but, when I call initDev(), I get a ValueError: Procedure called with not enough arguments (8 bytes missing) or wrong calling convention. I'm pretty sure this means that I have not produced a compatible pointer to pass, but I can't figure out what the function actually wants to receive.

I've spent a couple of days search stackoverflow, looking at ctypes documentation and trying all sorts of permutations, but I have not found the answer.

¿Fue útil?

Solución

It seems the function is using stdcall instead of the cdecl calling convention, i.e. use ctypes.WinDLL instead of ctypes.CDLL. Also, it wants a pointer to a memory location where it can store the handle, but you passed it a NULL pointer. Instead pass it a reference to a 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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top