02-07-2016 08:38 PM
I've saved a DLL (created in Labview) and i'm trying to import it into python, as per the guide, http://www.ni.com/white-paper/8911/en/
I can get other types except strings and arrays to read in from a cluster. I've reduced it to a very simple vi: a string goes into a cluster, the cluster is a function in the DLL i.e. the function is:
void TestCluster(Cluster *outputCluster)
I think I just don't have the correct Struct (?).
Has anyone done this? It might also be due to my little knowledge of ctypes in Python too.
Theres a similar item: http://forums.ni.com/t5/LabVIEW/Loading-Labview-DLL-from-Python-Hits-Error/m-p/2756534/highlight/tru...
Do I have to use the CLN? (I tried but couldn't work it out)
Sample Python code for the import:
#!/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()
02-08-2016 02:13 AM
Is it possible to match up with the structs from the test.h file or am I missing something? (Does Labview allow this? or is the memory not accessible? or does the CLN have to be used?)
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);
02-08-2016 07:13 AM - edited 02-08-2016 07:14 AM
You overlooked the definition of
**Uint8Array
You see the double * in front?
That means it is a pointer to a pointer not just a pointer nor the entire structure entirely embedded in the cluster!
Same for the LStrHandle element in the cluster, that is also a pointer to a pointer to a similar structure.