Python使用ctypes处理dll - 结构体输出参数

1 投票
1 回答
2651 浏览
提问于 2025-04-16 23:19

在这个dll的头文件里,我有一个这样的结构:

typedef struct USMC_Devices_st{
DWORD NOD;          // Number of the devices ready to work

char **Serial;      // Array of 16 byte ASCII strings
char **Version;     // Array of 4 byte ASCII strings
} USMC_Devices;         // Structure representing connected devices

我想调用一个dll里的函数:

DWORD USMC_Init( USMC_Devices &Str );

我试着这样做:

class USMCDevices(Structure):
   _fields_ = [("NOD", c_long),
            ("Serial", c_char_p),
            ("Version", c_char_p)]

usmc = cdll.USMCDLL #this is the dll file
init = usmc.USMC_Init
init.restype = c_int32; # return type
init.argtypes = [USMCDevices]; # argument
dev = USMCDevices()
init(dev)

但是这里出现了一个错误。我猜问题出在“Serial”和“Version”这两个数组上,它们的大小是和设备数量(NOD)对应的。

有没有什么办法可以解决这个问题呢?

非常感谢你的帮助!!!

1 个回答

2

对于 char ** 指针,使用 POINTER(c_char_p)。当你对 SerialVersion 进行索引时,会为给定的以空字符结尾的字符串创建一个 Python 字符串。请注意,如果你在数组中索引超过 NOD - 1,要么会得到一些无效的值,要么会导致解释器崩溃。

C:

#include <windows.h>

typedef struct USMC_Devices_st {
    DWORD NOD;       // Number of the devices ready to work
    char **Serial;   // Array of 16 byte ASCII strings
    char **Version;  // Array of 4 byte ASCII strings
} USMC_Devices;

char *Serial[] = {"000000000000001", "000000000000002"};
char *Version[] = {"001", "002"};

__declspec(dllexport) DWORD USMC_Init(USMC_Devices *devices) {

    devices->NOD = 2;
    devices->Serial = Serial;
    devices->Version = Version;

    return 0;
}

// build: cl usmcdll.c /LD

Python:

import ctypes
from ctypes import wintypes

class USMCDevices(ctypes.Structure):
    _fields_ = [("NOD", wintypes.DWORD),
                ("Serial", ctypes.POINTER(ctypes.c_char_p)),
                ("Version", ctypes.POINTER(ctypes.c_char_p))]

usmc = ctypes.cdll.USMCDLL
init = usmc.USMC_Init
init.restype = wintypes.DWORD
init.argtypes = [ctypes.POINTER(USMCDevices)]
dev = USMCDevices()
init(ctypes.byref(dev))

devices = [dev.Serial[i] + b':' + dev.Version[i]
           for i in range(dev.NOD)]
print('\n'.join(d.decode('ascii') for d in devices))

输出:

000000000000001:001
000000000000002:002

撰写回答