correct form of struct for strings (or byte array) when importing Labview DLL into python (using ctypes) -
i'm trying import dll (created in labview) python. i've reduced simple labview vi, string goes cluster, cluster function in dll i.e. function is: void testcluster(cluster *outputcluster) can strings , arrays read in cluster. think don't have correct struct (?).
has done this? there's partial guide: http://www.ni.com/white-paper/8911/en/
it might due little knowledge of ctypes in python too.
sample python code import:
#!/usr/bin/env python import sys, os, string 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 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 type 'uint8array' */ uint8array __cdecl allocateuint8array (int32 elmtcount); mgerr __cdecl resizeuint8array (uint8array *hdlptr, int32 elmtcount); mgerr __cdecl deallocateuint8array (uint8array *hdlptr);
thanks :)
you need allocate , deallocate strings, arrays, , clusters because labview expects full , complete object owned its memory manager instead of python's in case needs modify them (add/remove array elements or string characters). example, vikit has labview-built library uses strings , clusters in entry points.
for more information, while link reverse (a c dll called labview), examples here include clusters, strings, , arrays: calling c/c++ dlls containing simple , complex datatypes labview.
Comments
Post a Comment