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.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Call LabVIEW dll from C# with parameters including clusters with arrays

Hi
 
I am writing my main application in C# and calling dlls which I have created in LabVIEW. Overall I have managed to do this successfully and, after much internet trawling, I have overcome each hurdle as it has appeared (passing arrays, strings and clusters, etc). There's just one final combination of complications that I just can't seem to solve and that is how do I define a structure in C# that includes array(s) of lengths that are unknown at design time? I would like to do this where the LabVIEW dll requires a cluster as input or output which contains, amongst other things, arrays.
 
The only possible solution I have seen so far is to decide on a maximum size and declare a fixed size array in my C# structure. This is not really ideal and I would prefer more flexibility if possible.
 
If you do know how to solve this, then I might be able to save you a bit of unnecessary typing by giving you the following information:
1) I understand how to create and structures that don't include arrays.
2) I understand how to use marshalling to pass arrays that are not in clusters.
3) I do realise that I can pass all the elements of the cluster separately and then bundle them in LabVIEW (but I'm running out of space on my connector pane in some cases).
However, I am fairly new to C# so am still struggling with some of the syntax of that.
 
Thanks in advance for any help you can give me.
Cheryl
0 Kudos
Message 1 of 7
(6,151 Views)
Hi Cheryl,
 
It sounds like you want to be passing handles (or data by reference) from/to a LabVIEW DLL so you don't have to define a fixed sized array.  Have you taken a look at this example?:
 
It's written in C++ but it shows the concept of passing data to a LabVIEW DLL by reference. 
 
Way S.
NI UK Applications Engineer
0 Kudos
Message 2 of 7
(6,109 Views)

Thanks for your suggestion but I'm afraid that if the information to help me is in that article/code then I'm not bright enough to figure it out. The example in the article appears to require the size of the array to be known (the array dimension being part of the TD1 structure). Also, the way in which pointers are handled is so different in C# that I'm struggling to match the two up. Strictly speaking, the problem is probably more with understanding C# than LabVIEW.

Further suggestions welcome.

Cheryl

0 Kudos
Message 3 of 7
(6,105 Views)
Hi Cathy,
 
I've personally not done much with C# but as far as I'm aware the size of the array must always be known before run time as with most languages, you can dynamically allocate memory in a fashion by creating the array of size n at the declarations. If however you need to dynamically build and destroy array data you may need to use a different method (such as linked lists).
 
 
Although plenty more examples exist of this method on Google, if you'd like to place your code up on the forum (or atleast the relevant sections) I can take a look at what you are doing and give more suggestions.
 
Hope this helps,
All the best,
Applications Engineer
0 Kudos
Message 4 of 7
(6,054 Views)

Hi Cheryl,

Just wondering if the above links were of any use? If you could let me know how your getting on that would be great, all the best and Happy New Year!

Regards,

Applications Engineer
0 Kudos
Message 5 of 7
(5,933 Views)

Dear Rob

And a Happy New Year to you. I'm afraid I haven't got any further with this problem. I have checked out your link to the linked list info but this is not really what I need (I have used linked lists a few years ago so understand the concept). Also, the link in the earlier reply was for C++ code and does not translate to C#.

I have muddled on and managed to find, by experiment, the size of my output arrays in advance.  I'm calling LabVIEW dlls which use some of the frequency analysis and order analysis toolkit functions. So, for example, when calculating the order magnitude plot from a set of data I have found that the output array is 77 elements shorter than the input. I'm sure that if I had time to look into all the calculations behind this that I could've determined that number but I'm a bit pushed for time. I determine the size of the input array at run time by reading the data from file then I allocate my output array to be 77 elements shorter. If worst comes to the worst, I thought I could always call the calculations twice, the first time passing back the size of the output array only, allowing me to then allocate the array and then call the function again with the array as output. Not the most efficient process.

To try and keep it simple, a good example would be if I were passing an array of data from C# to a LabVIEW dll which discards all values below a threshold and returns an array of values greater than or equal to that threshold. I don't know in advance how big my output array would be. Where I do know the array sizes I am using code like this:

int iArrayInLength = 100;
double
[] adArrayIn = new double[iArrayInLength];
//....populate adArrayIn here from e.g. file or user input
int iArrayOutLength = iArrayInLength - 10;
double[] adArrayOut = new double[iArrayOutLength];
IntPtr ptrdArrayOut = Marshal.AllocHGlobal(iArrayOutLength* 8);

//call dll
MyLabVIEWDLL(adArrayIn, ptrdArrayOut, iArrayInLength, iArrayOutLength);

//retrieve array from pointer
Marshal.Copy(ptrdArrayOut , adArrayOut , 0, iArrayOutLength);
Marshal.FreeHGlobal(ptrdArrayOut );

Obviously, I can't allocate the output array before I pass it without knowing the size and, when creating the LabVIEW dll, the length of the output array is also requested.

I imagine that there is some sort of equivalent to the method using handles in C++ but I just can't work out what it is.

Cheryl

0 Kudos
Message 6 of 7
(5,930 Views)

Hi Cheryl,

I see your reasoning for not wishing to use Linked Lists, I'm afraid that I've personally done very little c# in the past so am unable to offer you much more assistance with regards to your c# code. I have found a few documents on google relating to Handles in c# via the "ref" keyword but none with example code. I would recommend looking into open source forums for c# and finding out the best way to allocate your array memory.

Apologies I can't be of more help,

All the best,

Applications Engineer
0 Kudos
Message 7 of 7
(5,928 Views)