在 Python 中使用 Ctypes 读取 Dll 的双 c 结构

什么了不起

我对这个问题做了很多研究..但是没有地方,我找不到它。我试图通过调用 c dll 来调用双 c 结构。

我的问题是,我在 python 中声明“类结构”的方法正确吗?我无法认为我就在路上。因为即使我想从 dll 调用的函数,它也没有输出任何东西。

[Visual C++/C]

我确实尝试过 C 语法代码,

typedef sturct {
    int nBoardNum;
    struct{
        char  pBoardName[16];
        int   nBoardID;
    }BOARDINDEX[8];
}AAPBOARDINFO, *PAAPBOARDINFO;

HANDLE AcapOpen(char* cpBoardName, int nBoardNo, int nCh)

[Python]

我像这样更改了 Python 语法。

import ctypes as c

class BOARDINDEX(c.Structure):
    _field_ = [("nBoardName", c.c_char_p * 16),("nBoardID", c.c_int)]

class AAPBOARDINFO(c.Structure):
    _field_ = [("nBoardNum", c.c_int), ("BOARDINDEX", BOARDINDEX * 8)]

AapLib2 = c.WinDLL("AapLib2.dll")

BoardName = ["ABC","FWD","HGW"]
BoardNo = 0
ch = 1

output = Open(BoardName, BoardNo, ch)

def Open(BoardName, BoardNo, ch)

    func = AapLib2.AcapOpen
    func.argtypes = [c.POINTER(BOARDINDEX),c.c_int, c.c_int]
    func.restype = c.c_int

    ref = BOARDINDEX()

    res = func(c.byref(ref.nBoardName),BoardNo, ch)
    return res

调用 Open() 函数时没有任何结果......

请考虑我的要求,任何答案都会很棒...

克里斯蒂·法蒂

你需要知道的一切,都可以在[Python 3.Docs]: ctypes - A external function library for Python 中找到

代码有几个问题:

这是您的代码的修改版本。不用说,我实际上并没有测试它,因为我没有.dll

#!/usr/bin/env python3

import sys
import ctypes
from ctypes import wintypes


class BOARDINDEX(ctypes.Structure):
    _fields_ = [
        ("nBoardName", ctypes.c_char * 16),
        ("nBoardID", ctypes.c_int),
    ]


class AAPBOARDINFO(ctypes.Structure):
    _fields_ = [
        ("nBoardNum", ctypes.c_int),
        ("BOARDINDEX", BOARDINDEX * 8),
    ]


def open_board(board_name, board_no, ch):
    AcapOpen = aaplib2.AcapOpen
    AcapOpen.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_int]
    AcapOpen.restype = wintypes.HANDLE
    ref = BOARDINDEX(board_name, board_no)  # Probably this line should be replaced by the 3 (commented) ones below (AcapGetBoardInfo prototype would have to be specified as well)
    #abi = AAPBOARDINFO()
    #AcapGetBoardInfo(ctypes.byref(abi))
    #ref = abi.BOARDINDEX[0]
    res = AcapOpen(ref.nBoardName, ref.nBoardID, ch)
    return res


def main():
    board_names = ["ABC", "FWD", "HGW"]
    board_no = 0
    ch = 1
    aaplib2 = ctypes.WinDLL("AapLib2.dll")
    output = open_board(board_names[0], board_no, ch)
    print(output)


if __name__ == "__main__":
    print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    main()
    print("\nDone.")

让我知道这对你有什么影响。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python无法使用Python的ctypes读取包含char数组的结构

如何使用ctypes从Python创建C结构?

如何使用ctypes读取具有自定义结构的dll的输出?

C结构的Python ctypes定义

使用Python Ctypes加载dll

使用 ctypes 将结构从 C 传递到 Python

在C编程中使用结构的双指针将文本文件中的Matrix读取为2D数组

在ctypes中使用dll函数 python 2.7与3.6

在OS X中使用python的ctypes调用DLL

相当于python ctypes的C dll

Python ctypes使用winmode加载DLL

带有DLL的Python ctypes参数-指向双精度数组的指针

python 3中使用嵌套重复结构的双菱形结构

如何使用ctypes调用带有双下划线函数名称的DLL函数?

在不使用C函数的情况下更新ctypes python中的结构指针的值

如何使用ctypes将python列表传递给C函数(dll)

使用 ctypes 的 .dll 中的 python 中的 C++ 函数 - 找不到函数和访问冲突

使用Python ctypes运行C dll函数时遇到问题(大小未知的数组输出)

将python列表传递给使用ctypes返回数组数据的“c”DLL函数

python:ctypes,在python中读取POINTER(c_char)

C ++代码在python中使用ctypes和mmap进行转换

在Python中使用C#DLL

python中的ctypes结构数组

ctypes,python中的循环结构

使用未知字段类型定义python ctypes结构

在python中使用ctypes读取不同类型的剪贴板数据

Python Ctypes双取消引用指针

使用Python调用DLL函数时出现ctypes.ArgumentError

在C中写入缓冲区,该缓冲区使用python使用ctypes分配,然后在python中再次读取