繁体   English   中英

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

[英]Reading double c structures of Dll with Ctypes in Python

我对这个问题做了很多研究..但是没有地方,我找不到它。 我试图通过调用 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.")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM