将Labview DLL导入python时字符串(或字节数组)的正确结构形式(使用ctypes)

2024-06-10 21:56:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将DLL(在Labview中创建)导入python。 我把它简化为一个非常简单的Labview VI,一个字符串进入一个集群,这个集群是DLL中的一个函数,也就是说,这个函数是:void TestCluster(cluster*outputCluster) 我可以从集群中读取除了字符串和数组之外的所有内容。我想我只是没有正确的结构(?)。在

有人这样做过吗?有部分指南: http://www.ni.com/white-paper/8911/en/

这也可能是因为我对Python中的ctypes知之甚少。在

导入的Python代码示例:

#!/usr/bin/env python
import sys, os, string
from ctypes import *

class byteArrayStructure(Structure):
    _fields_ = [("dimSize", c_int),("bytes", c_uint8 )]

class clusterStructure(Structure):
    _fields_ = [("stringField", c_char*4 ),
                ("byteArray", byteArrayStructure )]

dll = cdll.LoadLibrary("test.dll")
libc = cdll.msvcrt

def testMain():
    retValue = 0
    try:
        clusterIn = clusterStructure()
        dll.TestCluster( byref(clusterIn) )

        print clusterIn.byteArray.bytes 
        print cast(clusterIn.byteArray.bytes,c_char_p)

    except ValueError, Argument:
        retValue = "Error: " + str(ValueError) + "  " + str(Argument)
    return retValue

testMain()

编辑:

来自test.h的结构

^{pr2}$

谢谢:)


Tags: 函数字符串importbytes集群ctypes结构class
1条回答
网友
1楼 · 发布于 2024-06-10 21:56:51

您需要分配和取消分配字符串、数组和集群,因为LabVIEW需要一个full and complete object owned by its memory manager而不是Python,以防需要修改它们(添加/删除数组元素或字符串字符)。例如,VIKit有一个LabVIEW构建的库,在其入口点使用字符串和集群。在

要了解更多信息,虽然这个链接是相反的(从LabVIEW调用的C DLL),这里的示例包括集群、字符串和数组:Calling C/C++ DLLs Containing Simple and Complex Datatypes from LabVIEW。在

相关问题 更多 >