07-01-2022 04:57 AM
Hello everyone, I'm new to Labwindows/CVI, and I want to try to write a meshgrid function and deploy it as a DLL to be used in LabView. However, I have a problem with the output data type. I use double** for 2D arrays, and the function code seems to work, but in the main function, the output values are not correct. I noticed that Labwindows uses void* for the 2D arrays, and I am not sure how to create my own function by using void*. I tru to build a DLL and import it into LabView and it doesn't work either.
this is my function file mesh.c:
#include <stdlib.h>
#include <stdio.h>
void Mesh(double x[], double y[], int nx, int ny, double** X, double** Y)
{
int i, j;
X = (double**)malloc(nx * sizeof(double*));
for (i = 0; i < nx; i++){
X[i] = (double*)malloc(ny * sizeof(double));
}
Y = (double**)malloc(nx * sizeof(double*));
for (i = 0; i < nx; i++){
Y[i] = (double*)malloc(ny * sizeof(double));
}
for (i = 0; i < nx; ++i){
for (j = 0; j < ny; ++j){
X[i][j] = x[i];
Y[i][j] = y[j];
}
}
}
this is the main file main.c:
#include <stdio.h>
#include <stdlib.h>
#include <mesh.h>
int main(){
#define na 3
#define nb 5
double a[] = {1,2,3};
double b[] = {-5,-4,-3,-2,-1};
double A[na][nb];
double B[na][nb];
Mesh(a, b, na, nb, (double**) &A, (double**) &B);
return 0;
}
and this is the header file mesh.h:
__declspec(dllexport) void Mesh(double x[], double y[], int nx, int ny, double** X, double** Y);
Hope someone can help me with this. Thanks!
07-01-2022 04:58 AM
these are the files!
07-04-2022 03:27 AM - edited 07-04-2022 03:29 AM
There are a couple problems in your project.
First of all, you are allocating memory both in the calling and in the dll function: do it only in one place! I have tested it by dimensioning arrays in the calling function and passing them to the dll function and it works:
int CVICALLBACK CallDLL (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int i, j;
int na = 3, nb = 5;
double a[] = {1,2,3};
double b[] = {-5,-4,-3,-2,-1};
double **A = NULL, **B = NULL;
switch (event) {
case EVENT_COMMIT:
A = malloc (na * sizeof (double*));
for (i = 0; i < na; i++){
A[i] = calloc (nb, sizeof(double));
}
B = malloc (na * sizeof (double*));
for (i = 0; i < na; i++){
B[i] = calloc(nb, sizeof(double));
}
Mesh (a, b, na, nb, A, B);
InsertTableRows (panelHandle, PANEL_TABLEA, -1, nb, VAL_CELL_NUMERIC);
InsertTableColumns (panelHandle, PANEL_TABLEA, -1, na, VAL_CELL_NUMERIC);
for (i = 0; i < na; i++) {
for (j=0; j < nb;j++) {
SetTableCellVal (panelHandle, PANEL_TABLEA, MakePoint (i + 1, j + 1), A[i][j]);
}
}
InsertTableRows (panelHandle, PANEL_TABLEB, -1, nb, VAL_CELL_NUMERIC);
InsertTableColumns (panelHandle, PANEL_TABLEB, -1, na, VAL_CELL_NUMERIC);
for (i = 0; i < na; i++) {
for (j = 0; j < nb; j++) {
SetTableCellVal (panelHandle, PANEL_TABLEB, MakePoint (i + 1, j + 1), B[i][j]);
}
}
break;
}
return 0;
}
(note that Ihave used calloc that inizializes arrays to zero)
For this to work you must strip off the allocating section from the DLL source.
Take also note of how I call the DLL function.
Second, in the DLL project you were missing to tell CVI what to export:
The highlighted checkmark was missing and it caused the DLL to not compile as expected.You should be receiving a message stating that no symbol were exported: didn't you?
07-04-2022 03:45 AM
Just as a side note, since you are new toCVI, as you said, I inform you that postig cvibuild.xxx folder is useless, since its content is recreated by scratch at every program run. If you happen to post another project of yours in the future do not add that folder to avoid bloating this board with unnecessary files.