LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

dll to generate array

Solved!
Go to solution

Hi All,

 

I am very new in the using of dll for Labview so maybe my question is completely stupid. I would like to get an array of values which correspond to a kind of waveform (only the Y part). Here is the part of the cpp program

 

#include "stdafx.h"

#include <math.h>

 

#ifdef _MANAGED

#pragma managed(push, off)

#endif

 

extern "C" _declspec(dllexport) void SquaresWave(double *squares, int samples, double amp, double offset);

 

 

void SquaresWave(double *squares, int samples, double amp, double offset)

{

int step = (int) floor((double) samples/8);

samples = 8*step;

squares = new double[samples];

for (int i=0; i < 2*step; i++)

squares[i]=amp * (2-i/step) + offset;

for (int i=2*step; i < 3*step; i++)

squares[i]=-amp + offset;

for (int i=3*step; i < 4*step; i++)

squares[i]=amp * (i/step-4) + offset;

for (int i=4*step; i < 6*step; i++)

squares[i]= offset;

for (int i=6*step; i < 7*step; i++)

squares[i]=amp *(i/step-6) + offset;

for (int i=7*step; i < 8*step; i++)

squares[i]=amp + offset;

}

 

#ifdef _MANAGED

#pragma managed(push, off)

#endif

 

 

but when I try to use in Labview it is does not work. Do you have an idea where is my problem ?

thanks

0 Kudos
Message 1 of 7
(3,198 Views)


extern "C" _declspec(dllexport) void SquaresWave(double *squares, int samples, double amp, double offset);

 [...]

squares = new double[samples];




Do not create a new array in the DLL with "squares = new double[samples];".  

Just use the LabVIEW array. In LabVIEW, make sure the array is large enough for the data you will write to it else you will face the next trouble.

0 Kudos
Message 2 of 7
(3,194 Views)

Please look at the examples that ship with LabVIEW. There's a very extensive example on how to call DLLs from LabVIEW with a variety of datatypes. Including arrays.

0 Kudos
Message 3 of 7
(3,191 Views)

double *squares should be an output

int samples should be input and output

double amp, double offset should be input

 

How to define them in Labview ?

thanks

0 Kudos
Message 4 of 7
(3,186 Views)

Steve already mentioned: There is a good example that ships with LabVIEW: You can take C function prototypes for different data types from it.  Search for the example named "Call DLL.vi".

0 Kudos
Message 5 of 7
(3,182 Views)
Solution
Accepted by topic author mnemo15

@mnemo15 wrote:

double *squares should be an output


That and your code indicates that the DLL will be allocating the memory for the array. This is also known as the "hard way of doing things". As such, LabVIEW cannot use that buffer directly. If you search this site for calling DLLs where you allocate memory inside the DLL you will find numerous posts. They basically all tell you that you have to use the MoveBlock function to get the memory into LabVIEW's memory space. See, e.g., Dereferencing Pointers from C/C++ DLLs in LabVIEW

 


int samples should be input and output

Then it should be a pointer, right?

 

 

As to the code, shouldn't the second #pragma directive be "#pragma managed(pop)"?

0 Kudos
Message 6 of 7
(3,175 Views)

LabVIEW code interface is not C++ but only simple C. C++ would be a total fiasco as memory allocation by C++ runtime version x is not compatible with C++ runtime version Y. So you could anhow not allocate memory in your DLL to be further handled by LabVIEW.

 

Standard C has clear rules. Unless you agree to use both in Caller and Callee exactly the same runtime version for things like memory management, you always have to allocate buffers in the caller, even if they are output buffers. So in order to use array pointers you have to allocate the array in LabVIEW, pass it to the function and inside the function filling in the values, being very carefull to write only until the last allocated element in the array and not a single element further.

 

Or as alternative you can pass the LabVIEW native datatype to your DLL and use the LabVIEW memory manager functions, that are defined in extcode.h (and labviewv.lib). But this last one is an advanced topic and clearly not suitable for anyone not being intimately familiar with both C and LabVIEW.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 7 of 7
(3,173 Views)