How to send an array to DLL and receive it back?

Posted By: pjotr987

How to send an array to DLL and receive it back? - 07/04/11 11:54

Hey guys,
to process single variables in DLLFUNCs is easy, but how works the stuff with arrays? In my particular case i want to receive an array from a DLLFUNC. How to do this?

thanks in advance for any help
Posted By: Quad

Re: How to send an array to DLL and receive it back? - 07/04/11 12:27

return it.

Code:
DLLFUNC int* get_array(){
    int* int_array[10];
    int i = 0;
    for(i=0;i<10;i++){
        int_array[i] = 10;
    }
    return int_array;
}


Posted By: pjotr987

Re: How to send an array to DLL and receive it back? - 07/04/11 13:15

Hi Quad, thanks for ur fast reply.
The code u provided causes in my compiler an error telling "
error C2440: 'return': 'int *[10]' cant be converted to 'int *'
any idea?
would be nice if u also can give an example how to call this function from liteC
since u are Expert wink
any other is also welcome
thanks
Posted By: Quad

Re: How to send an array to DLL and receive it back? - 07/04/11 13:27

oops sorry it should be
int int_array[10];

you call it just you call any other DLLFUNC
Posted By: pjotr987

Re: How to send an array to DLL and receive it back? - 07/05/11 12:40

Hi Quad, i hope you are in today.
somehow i fail to call the function you provided in liteC.
i try it the following way:

int test[10]; //the receiving array
int get_array();// preparing function for calling

then in main:

test=get_array();////the call itself

something i make definitely wrong, but could not figure it out frown

i would be very grateful for a little help
thanks for everyone in advance
Posted By: 3dgs_snake

Re: How to send an array to DLL and receive it back? - 07/05/11 12:46

Hi,

I think that the array is already allocated in the DLL, so all you need to do is :

Code:
int *test;  // the receiving array
int *get_array(); // preparing function for calling

....
int main()
{
...
test = get_array();
...
}



But you must be carefull when doing something like this : how and where to free the memory allocated to the array, etc...

Best regards.
Posted By: pjotr987

Re: How to send an array to DLL and receive it back? - 07/08/11 12:58

3dgs_snake thank you for your help
here i put some examples for DLL functions for people, who like me, use DLLs for the first time. But one doubt left. I want now send two arrays to DLL and receive a processed one back but cant get it working.

LiteC:
int* array1[5];//input array
int* array2[5];// input array
int* array3[5];//output array
int* two_arrays(int* xx,int* yy);///declaration
array3=two_arrays(array1,array2);//executing

///DLL code///
DLLFUNC int* two_arrays(int* array1,int* array2)////NOT-OK
{
int array3[5];
array3[0]=array1[0]+array2[0];
array3[1]=array1[1]+array2[1];
array3[2]=array1[2]+array2[2];
array3[3]=array1[3]+array2[3];
array3[4]=array1[4]+array2[4];
return array3;

}

what is wrong?
anyone has an idea?


here is the mentioned working code for processing arrays in DLL:
Functions in DLL file


DLLFUNC int* tester( )///////////////Function 1
{
int r[10];
// set the seed

for (int i = 0; i < 5; ++i)
{
r[i] = (50+i)*20;
}
return r;
}

DLLFUNC void get_array(VECTOR* int_array)/////////////Function 2
{
int_array->x = 10;
int_array->y = 20;
int_array->z = 30;
}
////////////77

Code in liteC:
/////////////////FUNCTION 1
int* array1[10];//array preparation
int* tester( );//Function declaration
array=tester();// executing the function
printf("array1 %d",array[1]);///shows the result in array[1]

////////////////function 2///////////////

VECTOR* test_vec [3];////array for DLL Function 2
void get_array(VECTOR* xx);///Declaration of the DLL Function 2
get_array(test_vec);/////call of the function
printf("test %d",test_vec->x);////////shows the result in x(y and z can also be shown just by using them instead of x)

i hope it helps someone
Posted By: 3dgs_snake

Re: How to send an array to DLL and receive it back? - 07/08/11 13:40

Hi,

  • The parameters are int arrays not array of int pointers => int array1[5].
  • The result is an array of int so an int pointer to the first element of the array => int* array3.


Code:
int array1[5];//input array
int array2[5];// input array
int* array3;//output array
int* two_arrays(int* xx,int* yy);///declaration
array3=two_arrays(array1,array2);//executing



Best regards.
Posted By: pjotr987

Re: How to send an array to DLL and receive it back? - 07/08/11 15:55

thanks man ur my hero
© 2024 lite-C Forums