[英]correct form of struct for strings (or byte array) when importing Labview DLL into python (using ctypes)
I'm trying to import a DLL (created in Labview) into python. 我正在尝试将DLL(在Labview中创建)导入python。 I've reduced it to a very simple Labview VI, a string goes into a cluster, the cluster is a function in the DLL ie the function is: void TestCluster(Cluster *outputCluster) I can get everything but strings and arrays to read in from a cluster. 我将其简化为一个非常简单的Labview VI,字符串进入了群集,该群集是DLL中的一个函数,即该函数是:void TestCluster(Cluster * outputCluster)除了字符串和数组,我可以读取所有内容从群集。 I think I just don't have the correct Struct (?). 我想我只是没有正确的Struct(?)。
Has anyone done this? 有人这样做吗? There's a partial guide: http://www.ni.com/white-paper/8911/en/ 有部分指南: http : //www.ni.com/white-paper/8911/zh/
It might also be due to my little knowledge of ctypes in Python too. 这也可能是由于我对Python的ctypes知识也不多。
Sample Python code for the import: 用于导入的示例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()
edit: 编辑:
structs from test.h, 来自test.h的结构,
typedef struct {
int32_t dimSize;
uint8_t elt[1];
} Uint8ArrayBase;
typedef Uint8ArrayBase **Uint8Array;
typedef struct {
LStrHandle elt1;
Uint8Array unsignedByteArray;
} Cluster;
void __cdecl TestCluster(Cluster *outputCluster);
MgErr __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);
/*
* Memory Allocation/Resize/Deallocation APIs for type 'Uint8Array'
*/
Uint8Array __cdecl AllocateUint8Array (int32 elmtCount);
MgErr __cdecl ResizeUint8Array (Uint8Array *hdlPtr, int32 elmtCount);
MgErr __cdecl DeAllocateUint8Array (Uint8Array *hdlPtr);
Thanks :) 谢谢 :)
You need to allocate and deallocate strings, arrays, and clusters because LabVIEW expects a full and complete object owned by its memory manager instead of Python's in case it needs to modify them (add/remove array elements or string characters). 您需要分配和取消分配字符串,数组和群集,因为LabVIEW需要一个完整的对象(而不是Python的) 属于其内存管理器拥有,而该对象需要修改(添加/删除数组元素或字符串字符)。 For example, VIKit has a LabVIEW-built library that uses strings and clusters in its entry points. 例如, VIKit有一个LabVIEW构建的库,该库在其入口点使用字符串和簇。
For more information, while this link is the reverse (a C DLL called from LabVIEW), the examples here include clusters, strings, and arrays: Calling C/C++ DLLs Containing Simple and Complex Datatypes from LabVIEW . 有关更多信息,该链接是反向链接(从LabVIEW调用的C DLL),此处的示例包括群集,字符串和数组: 从LabVIEW调用包含简单和复杂数据类型的C / C ++ DLL 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.