LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

|How to pass LV Cluster containing Cluster to Dll?

Hi!

Is it possible to pass LabView Cluster containing another Cluster to Dll? And if it is possible, then how 🙂

Regards,
Marcin
0 Kudos
Message 1 of 12
(4,318 Views)
This is one of those "yes" and "no" answers.

You cannot pass a Cluster directly to a DLL using the Call Library Node.  Although, I will admit that I have not yet tried that in LabVIEW 8.

What you can do is Flatten the cluster to a string and then pass the string to the DLL.  The DLL must be expecting a "struct" that happens to be a typedef that is constructed exactly the same way as your cluster.  Make sure that each data element in the cluster ends on a multiple of 4 byte boundary.  If you have a U16 in there, put another one right after it in the cluster order to get you to 4 bytes.

The Flatten to String function gives you two outputs.  One is just the data in the cluster you flattened, and the other describes things such as what element belongs in what sub-cluster, etc.

All that being said... you really have to want to do all that.  Your DLL must be constructed to expect the series of data elements as they appear in the cluster.

So I would say it is possible, but not recommended as it introduces that feared "brittle"ness to you code.

Dan Press
Certified LabVIEW Architect
PrimeTest Automation
0 Kudos
Message 2 of 12
(4,301 Views)
Hi,
I don't know if this is what you want,but look in the VI
cheers
cosmin
0 Kudos
Message 3 of 12
(4,295 Views)

I have successfully passed a cluster with another cluster to a DLL using the following code.  The cluster must be unbundled and then bundled as shown.  Strings must be changed to byte arrays, and arrays must be changed into clusters.  It probably depends on how the DLL is written also.

 

tst:  I used your block diagram capture tool.  WOW, what a time saver.  Thanx again (also to CC, Altenbach, JPD,.....)

Message Edited by tbob on 02-20-2006 09:59 AM

Message Edited by tbob on 02-20-2006 10:01 AM

- tbob

Inventor of the WORM Global
Message 4 of 12
(4,279 Views)
Hello,
why do anyone need to unbundle and flatten to string to pass a cluster with a cluster in a dll? Why using Adapt to type and considering the cluster structure in the C code isn't enough?
cosmin
0 Kudos
Message 5 of 12
(4,262 Views)
Labview clusters are not stored in the same binary format as C structures.  So you can't just pass the cluster, C would not recognize the format as a structure.  In my previous post, I showed how to unbundle, re-structure the cluster elements, then bundle again.  This puts it in a binary form similar to a C structure.  Adapt to Type can then work properly to convert the cluster into a C structure.  Strings must be converted to byte arrays then to clusters.  Any 1D array must be converted to a cluster.  You can't pass 2D arrays at all, you must separate each row and pass each one as a 1D array converted to a cluster separately.  It all comes down to how the languages store the data type in binary format.
- tbob

Inventor of the WORM Global
Message 6 of 12
(4,258 Views)


@tbob wrote:
Labview clusters are not stored in the same binary format as C structures.  So you can't just pass the cluster, C would not recognize the format as a structure.  In my previous post, I showed how to unbundle, re-structure the cluster elements, then bundle again.  This puts it in a binary form similar to a C structure.  Adapt to Type can then work properly to convert the cluster into a C structure.  Strings must be converted to byte arrays then to clusters.  Any 1D array must be converted to a cluster.  You can't pass 2D arrays at all, you must separate each row and pass each one as a 1D array converted to a cluster separately.  It all comes down to how the languages store the data type in binary format.

That is not entirely correct. LabVIEW stores flat data the same way as C. Flat data is anything that does not contain a string or an array. There may be alignment issues (LabVIEW always uses byte packing) whereas external code can use whatever packing the programmer chose either through compiler settings or specific #pragma pack() statments in the code, but for the rest flat data is exactly the same in both LabVIEW and C.

A structure inside a structure for that matter is simply extended. The difficulty arises when strings and arrays get into the picture. LabVIEW stores them always in handles, where as in C you have the choice to store them in a pointer

struct {
    int *ptr;
} somedata;

or inlined

struct {
    int ptr[20];
} somedata;

Both are not the same as a LabVIEW handle but while the first one is best solved with an extra external code wrapper library, the second one can be simulated in LabVIEW with a cluster of a fixed number of elements inside another cluster. Of course if you have many different datatypes and fucntions taking structures I always advice to create an external code wrapper library to translate between them and more LabVIEW friendly datatypes.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 7 of 12
(4,254 Views)

I wasn't talking about flat data, I was comparing the way Labview stores a cluster to the way C stores a structure.  What I mean is that Labview may store the number of elements first, then the elements, and so on.  I am not quite sure exactly how LV does it but I do know that C stores structures differently.  If it did, it would be quite easy to pass a Labview cluster to a C structure.

Whatever the case, I am able to pass LV clusters successfully to and from C structures in a DLL using the methods I described earlier, without use a wrapper.  Again it may depend on the DLL and how it was written.

- tbob

Inventor of the WORM Global
0 Kudos
Message 8 of 12
(4,244 Views)
Hello,

As always rolf is on track. He explained better what i meant earlier, the main problem when passing clusters in a DLL is the case when you have in the C structure a pointer to variable sized arrays (numeric or string), otherwise the passing is quite straight. Actually in the code from tbob when the “array to cluster “is used a cluster of fixed size is created so the above problem is avoided, so in the initial cluster tbob considers that the string and the U8 array has fixed sizes.

cosmin

0 Kudos
Message 9 of 12
(4,236 Views)
There is a way to generate the equivalent C struct of the cluster you want to pass. What you do is create a cluster constant and wire it into a CIN node. It is in the connectivity >> Libraries and executable pallete. Connect the cluster to the input of the node, right click on the node and say generate .c code.
Next open the c code and you should see the struct equivalent of your cluster. Now you can copy this to your c++ code and use passed by array handle to manipulate the data.


0 Kudos
Message 10 of 12
(4,230 Views)