From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Can I import a cluster with strings (or a byte array) from a Labview DLL into python (using ctypes)

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()
0 Kudos
Message 1 of 3
(3,499 Views)

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);
0 Kudos
Message 2 of 3
(3,459 Views)

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.

Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 3
(3,442 Views)