如何将类对象传递给ctypes.windll.LoadLibrary(DLL)函数?

时间:2012-10-10 22:49:34

标签: python class dll ctypes windows-7-x64

我正在使用Python并尝试使用WinUSB(来自Microsoft)与这个简单的USB设备通信。我有一个问题,因为为了找到设备,调用setupAPI函数SetupDiEnumDeviceInterfaces采用一个struct对象,我将其定义为一个类:

class _SP_DEVINFO_DATA:
    def __init__(self, ClassGUID, DevInst = ""):        
        '''flags = SPINT_DEFAULT, SPINT_REMOVED, or SPINT_ACTIVE'''
        self._ClassGUID = None
        self._DevInst = None
        self._Reserved = None
        self._cbSize = None

    ###Getters:
    def getClassGUID(self):
        return self._ClassGUID
    def getDevInst(self):
        return self._DevInst
    def getReserved(self):
        return self._Reserved
    def getcbSize(self):
        return self._cbSize

    ###Setters:
    def setClassGUID(self, value):
        self._ClassGUID = value
    def setDevInst(self, value):
        self._DevInst = value
    def setReserved(self, value):
        self._Reserved = value
    def setcbSize(self):
        self._cbSize = sys.getsizeinfo(self)       

    ClassGUID = property(getClassGUID, setClassGUID, None, "Class GUID")
    DevInst = property(getDevInst, setDevInst, None, "Device Instance")
    Reserved = property(getReserved, setReserved, None, "RESERVED: DO NOT USE")
    cbSize = property(getcbSize, setcbSize, None, "CB Size. Set automatically")

我尝试使用该属性,因为它给了我错误:

<type 'exceptions.TypeError'>: Don't know how to convert parameter

否则,我会读到像这样的定义参数可以解决问题,但事实并非如此,我不知道该怎么做。

我想使用WinUSB,因为我只需要从设备读取并写入设备,就是这样,WinUSB似乎有我需要的东西,但直到我能解决这个问题,我才有点儿粘

有什么建议吗?如何将类对象传递给加载了ctypes.windll.LoadLibrary(DLL)的DLL函数?

如果有更简单的方法可以做到这一点,我也都是为了这个。

感谢。

2 个答案:

答案 0 :(得分:2)

正如@Roland所说,你必须来自ctypes.Structure。这是一个工作版本:

import ctypes
from ctypes import wintypes
import uuid

class _SP_DEVINFO_DATA(ctypes.Structure):
    _fields_ = [("cbSize", wintypes.DWORD),
                ("ClassGuid", ctypes.c_char * 16),
                ("DevInst", wintypes.DWORD),
                ("Reserved", wintypes.LPVOID)]

    def __init__(self, guid, inst):
        self.cbSize = ctypes.sizeof(_SP_DEVINFO_DATA)
        self.ClassGuid = uuid.UUID(guid).get_bytes()
        self.DevInst = (ctypes.c_ulong)(inst)
        self.Reserved = None

    def __repr__(self):
        return "_SP_DEV_INFO_DATA(cbsize={},ClassGuid={},DevInst={})".format(
            self.cbSize,uuid.UUID(bytes=self.ClassGuid),hex(self.DevInst))

sp = _SP_DEVINFO_DATA('08751880-13bb-11e2-96f0-402cf4ca5e51',0x12345678)
print sp

输出:

_SP_DEV_INFO_DATA(cbsize=28,ClassGuid=08751880-13bb-11e2-96f0-402cf4ca5e51,DevInst=0x12345678L)

答案 1 :(得分:0)

您的课程应来自ctypes.Structure

import ctypes

class _SP_DEVINFO_DATA(ctypes.Structure):

    _fields_ = [("cbSize", ctypes.c_ulong),
                ("ClassGuid", ctypes.c_ubyte * 12),
                ("DevInst", ctypes.c_ulong),
                ("Reserved", ctypes.c_voidp)]

    def __init__(self, guid, inst):
        self.cbSize = 24
        # GUID doesn't work like this... Needs proper conversion
        #self.ClassGuid = (ctypes.c_ubyte * 12)(bytearray(guid))
        self.DevInst = (ctypes.c_ulong)(inst)
        self.Reserved = None