LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

dll problem with pascal and arrays

My problem is with Labview code compiled into a DLL.
I have several VI's compiled into one DLL as seperate functions.
I can pass values (such as doubles, with no problem)
 
The problem is that I cannot pass an array into the DLL from Pascal.
I have tried many different methods (pointers/not pointers etc.)
I am trying to pass an array of doubles and the length as a long int.
 
Please see the attached code. I have spent 3 days on this already so anyone that solves this gets a jam doughnut in the post 🙂
The exe is the compiled Delphi code.
 
Regards
 
Marek
0 Kudos
Message 1 of 9
(4,419 Views)


@marekm wrote:
My problem is with Labview code compiled into a DLL.
I have several VI's compiled into one DLL as seperate functions.
I can pass values (such as doubles, with no problem)
 
The problem is that I cannot pass an array into the DLL from Pascal.
I have tried many different methods (pointers/not pointers etc.)
I am trying to pass an array of doubles and the length as a long int.
 
Please see the attached code. I have spent 3 days on this already so anyone that solves this gets a jam doughnut in the post 🙂
The exe is the compiled Delphi code.
 
Regards
 
Marek


My Delphi knowledge approximates 0 and the last time I have used Pascal was when it was still called Turbo Pascal, so this may be not what you are looking for.

Judging from the LabVIEW generated header file I would think this part should be ok. I assume you talk about Array_Add10().

As far as I can say you are doing a bit strange things in your DLLTest_Main.pas file. You seem to try to do something about handles probably trying to deal with LabVIEW array handles but that is entirely wrong. First you have configured the DLL function to use a normal C style pointer array and second dealing with LabVIEW handles is only really possible if the calling process is LabVIEW controlled due to special memory managment requirements of LabVIEW handles.

So basically you have to find a way to tell Delphi to treat your DOUBLE array not as some kind of Pascal datatype but instead as a C style pointer. Now last when I used Pascal, arrays where simply pointers so having a DOUBLE array as parameter should have worked. Probably Delphi uses some other means for arrays similar to what Visual Basic calls SAFEARRAY  so there you would have to find a way to tell Delphi to treat it as pointer. I'm not sure POINTER is the right type here as I have no idea what Delphi will do with a DOUBLE array passed to a POINTER parameter, but maybe that does work. The problem most probably seems on the Delphi side as the LabVIEW DLL does look according to the header file just like an ordinary C DLL. Without seeing the LabVIEW project file it is hard to say for sure that the build definition might not contain an error but I assume you got that right, since it would seem hard to me to make there an error if you get the included DLL and header file.
So if you can figure out in the Delphi documentation how to deal with arrays of DOUBLE to be passed to a C DLL you should all be set.

Rolf Kalbermatter

Message Edited by rolfk on 02-15-2007 11:23 AM

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 2 of 9
(4,406 Views)

Thanks for your reply!

The handles part of the code is actually commented out, that was something we tried earlier.

Someone mentioned to me that Labview is big endian and Delphi may be passing a little endian array which maybe causing my access violations. I presume this would explain why I can read single values but not arrays?

0 Kudos
Message 3 of 9
(4,390 Views)


@marekm wrote:

Thanks for your reply!

The handles part of the code is actually commented out, that was something we tried earlier.

Someone mentioned to me that Labview is big endian and Delphi may be passing a little endian array which maybe causing my access violations. I presume this would explain why I can read single values but not arrays?



I see! Guessed so much but curly braces for comments is really something I didn't remember anymore.

The endianess of LabVIEW only matters when you flatten or unflatten data. That means when you change any datatype in LabVIEW into a byte stream or vice versa. The common flatten data format is important, since changing data into a byte stream is necessary for network transport or external binary files and other communication. Using a standard endian format for that instead of whatever the current system uses allows you to write LabVIEW applciations that can run on any LabVIEW platform and do communicate over network or through binary files with each other.

But in memory the data is kept in whatever endianess the CPU operates and also passed between VIs and to external code such as DLLs in that format.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 4 of 9
(4,383 Views)
Hi again, here is the Labview code and bld file. I am raising the stakes, if anyone can get this working I will buy them 2 doughnuts and a cream cake!
 
PS I had to rename the RAR to a ZIP (incase of probs opening!)
0 Kudos
Message 5 of 9
(4,365 Views)


@marekm wrote:
Hi again, here is the Labview code and bld file. I am raising the stakes, if anyone can get this working I will buy them 2 doughnuts and a cream cake!
 
PS I had to rename the RAR to a ZIP (incase of probs opening!)


Ok I just looked at Add25.vi and Array_Add10.vi as I don't have the Anritsu Instrument driver. Frist the writing of the result to a local variable of the input makes no sense at all. Probably shouldn't really hurt but it does definitely not help.

As far as the Array_Add10.vi is concerned this will not work the way you want. The fact that you wire the result to the input array "Array" will not make it magically appear in the caller. You defined the parameter as input only and that is what LabVIEW makes from it. It allocates internally a LabVIEW array handle copies all the data from the input parameter into it does the calculation and that is it. What you want to do is either add an extra parameter for the output array (and an extra length parameter too) or make that first parameter an Input/Output parameter and connecting the input and output connections to the correct control.

You will have to wire the ouput array to the connector pane first in any case. Then for the second option basically select Param Type: Input/Output,
VI Input: Input
VI Output: Output
Pass as: Array Data Pointer
Length Input: len
Length Output: len (could also leave this to none which allows to keep the integer passed by value)

This will create a function such as:

void Array_Add10(double array[], long *len)

Meaning you pass in the array of doubles as first parameter, then the length of the array as second parameter and by reference (Pascal used VAR name:type for this). On return you get the resulting data in the array and the actual length used in len. This obviously only works if the generated array is equal or smaller than the input array.

And defining your function in Delphi like this:

PROCEDURE Array_Add10( Vector_10ElementsOfDOUBLE  : POINTER; ArrayLength_INTEGER : POINTER);StdCall;

PROCEDURE since you define no return value in the LabVIEW interface, and the length as POINTER too since we changed it to be passed by reference now.

@and in the call to the function you might need to use the (reference of) operator which I think is the @ char.

So something like this

   len: LONGINT;
   arr: ARRAY[1..15] OF DOUBLE;

   len := High(arr);
   {fill in some values in array};

@   Array_Add10(@arr, @len);

should work. For High() use the Delphi operator that gives the number of elements of an array, I think I saw somewhere that the pseudo function High() does do that.

Rolf Kalbermatter



Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 6 of 9
(4,359 Views)
Rolf,

Thanks for your very helpful reply. Thanks to you the problem is now solved. If you send me your paypal email, I will quite happily send you the doughnuts and the cream cake.

Regards

Marek
0 Kudos
Message 7 of 9
(4,341 Views)


@marekm wrote:
Rolf,

Thanks for your very helpful reply. Thanks to you the problem is now solved. If you send me your paypal email, I will quite happily send you the doughnuts and the cream cake.

Regards

Marek


Na! Just give me five stars. That's enough! Smiley Very Happy
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 8 of 9
(4,334 Views)
Sorry, it is late now, so I didn't read you code. I tried to call c
dll from delphi. it looks like:

......
var
arr: array of double;
begin
......
dll_func(@arr[0]); //
......
end;

Actually I made dll with c and i call it from labview. I tested this
dll with delphi.
I don't know if it help you, but worth to try:)
good luck

0 Kudos
Message 9 of 9
(4,246 Views)