LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

creating dll in c++ and using labview to call it

Greetings All,
 
I have created a .dll file in visual C++ 2005 Express... I have attached both the c file and header file I used to create this dll in the .txt files cfile and header file. I have also included the .dll file itself so that you can use the vi. Please let me know if you have problems using it after you have configured the dll to go to the path you store and call the function ReturnMaxVal.  The problem is the max value keeps giving me a rediculous number. I did this with a 1-D array and it was no problem. If anyone can help it would be much appreciated.
Thanks,
Clint
0 Kudos
Message 1 of 11
(4,542 Views)
I forgot to mention .. the purpose of dll is to take in array , number of columns, number of rows .. and output the max value in the array
0 Kudos
Message 2 of 11
(4,538 Views)

Hey There,

I recompiled your c code and go the same results you got.  I see that in the below for loop code, "inputaray", has one r.  Was it spelled wrong?  I tried to fix it but I"m  a bit rusty with pointers.  What were you doing in this for loop?

 

_declspec (dllexport) int ReturnMaxVal(double **inputarray,long rows,long columns,double *max)

{

double **inputaray = new double*[rows];

for (int k = 0; k < rows; k++)

{

inputarray[k] = new double[columns];

}

Message 3 of 11
(4,514 Views)
Hello,
 
are you sure, that you want to reallocate inputaray over and over again everytime on calling ReturnMaxVal()? 🙂
Message 4 of 11
(4,504 Views)
Hi martin and hyatt. No, I am not sure if i want to realocate the array every time. What i'm really trying to do is make a function which I don't have to specify the dimensions "row" and "collumn" inside the function decleration.  I want to send  the row and collumn size into that dll along with the array and have the function realocate enough memory to accept the array and compute the max value. Thus, I was trying to dynamically allocate memory for the array i'm going to send it. If this is a bad way to do this please let me know as i'm definately no C++ expert!
0 Kudos
Message 5 of 11
(4,479 Views)

Hello,

Microsofts VC++ is the worlds most important C- and C++-Compiler.

With C/C++ an array has no attached "really used" dimensions, so you will always have get those values from somewhere. LabVIEW internally of course knows the dimensions of its arrays. But I never wrote a LabVIEW-dll up to know, so I can't really help you on this. 😐

0 Kudos
Message 6 of 11
(4,442 Views)
Hi,

Could you define exactly what you want your C++ function to do?
Currently, I don't understand where do you need dynamic allocation,
if the only thing you want is to return the biggest value from the array.

Or, did I misunderstand your problem?
0 Kudos
Message 7 of 11
(4,436 Views)

Hi kaem,

The function should accept dimensions of array "row" and "column" (ie. 8 x 10 ) . It should then be able to accept the dbl array without me having to write func(**array[][10]) for example. Where you see I have specified the number of collumns. For example if I write func(*array) this function will accept any 1xN dimensional array. I do not have to specify the size of the array. However, in two dimensions it seems that C++ does not accept it. So what I would like is a function which accepts a 2-D array without me having to specify the dimensions of the array in the function decleration. Rather, I would like the row and collumn dimensions to be "inputs" into the function and then have code allocate enough memory to handle that array. If you open my code that I attached earlier you may be able to follow along on what I am asking better; if you need more clarification please ask as I would really like to get a handle on this. Thank you very much for your time.

 

Clint

0 Kudos
Message 8 of 11
(4,398 Views)
OK, here's a one way to do it. This is the C way, using malloc() to reserve memory.
There's propably a more C++ way to do this, using new and delete, but this works,
although I don't guarantee that it doesn't have memory leaks Smiley Very Happy

This will initialize a 2D int array, but you should get the hang of things with this
example.

#include <iostream>
using namespace std;

int **a = NULL;

void f2(int rows, int cols)
{
    // create array
    a = (int **)malloc(rows * sizeof(int *));
   
    for(int i = 0 ; i < rows ; i++)
    {
        a[i] = (int *)malloc(cols * sizeof(int));
       
    }

    // init values
    for(int i = 0 ; i < rows ; i++)
    {
        for(int j = 0 ; j < cols ; j++)
        {
            a[i][j] = i+j;
            cout << a[i][j];
        }
        cout << endl;
    }
}

int main(void)
{
    int rows = 0;
    int cols = 0;

    cout << "Enter number of rows: ";
    cin >> rows;
    //cout << endl;
    cout << "Enter number of colums: ";
    cin >> cols;
    //cout << endl;
    f2(rows, cols);
   
    // free reserved memory   
    for(int i = 0 ; i < rows ; i++)
        free(a[i]);
    free(a);
    a = NULL;

    return 0;
}

Hopefully this helps. You can try to google for other possibilities, there seems
to be a lot of forum post about this matter.
0 Kudos
Message 9 of 11
(4,292 Views)

Hello,

C and thus C++ can handle higher dimensional arrays en-bloc. 🙂

So a[i] actually also is i[a] because a[i] = *(a+i) = *(i+a) = i[a] as the best C book "A Book on C" explains. 🙂

So we can write these things more compact, but that doesn't really help you with your problem.

0 Kudos
Message 10 of 11
(4,275 Views)