Link to home
Start Free TrialLog in
Avatar of Thalox
Thalox

asked on

Problem getting data from cpp dll

Hi experts,

I'm trying to write a cpp dll and access it using c#.

this is my code (part of it)

cpp

int _cdecl readData(long UserCode, long *result, int index) {

// declare vars
...
long data[256];

// call api-function
Dongle_ReadData(UserCode, data, vars, Count, Port);

// assign result to param for "output"
result = data;

// when I print it out for debugging I'm getting the right values
for (int i = 0; i < 10; i++) {
 printf("11>>>: %d\n", result[i]);
}

return 0;
}

I import this in c#

[DllImport(@"..\..\..\Api\Debug\test.dll")]
public static extern int readData(int UserCode, ref int[] result, int index);

and call it with

int[] hlp = new int[256];
int err = Import.readData(123456, ref hlp, 3);


but after the call hlp has a length of 1 and hlp[0] is 0
but hlp[0] should be 123 (is printed when I use this            printf("11>>>: %d\n", result[0]);          in the dll)

So what I am doing wrong here?

Thalox
Avatar of Deepu Abraham
Deepu Abraham
Flag of United States of America image

Try this:

[DllImport(@"..\..\..\Api\Debug\test.dll")]
public static extern int readData(int UserCode, ref UInt32 [] result, int index);

Best Regards,
DeepuAbrahamK
Or try with ref Int32 [] result

Best Regards,
DeepuAbrahamK
Avatar of Thalox
Thalox

ASKER

thanks for the comment.

but it doesn't work. I'm getting the same result as with int
At least you have error in this function as I understood your code
int _cdecl readData(long UserCode, long *result, int index) {
{
// declare vars
...
long data[256];

// assign result to param for "output"
result = data;
..
}

You declare data inside the function /on the stack/ and when you return from the function stack is destroyed and yor data are not more valid.
Avatar of Thalox

ASKER


yes, sounds logical ;)

what would you suggest.

I have a function (
// call api-function
Dongle_ReadData(UserCode, data, vars, Count, Port);
) which demands a long* to store data in it.
I need to store this data in the param to access it from outside.
If I pass result directly in the function this doesn't work either (Dongle_ReadData(UserCode, result, vars, Count, Port);)
I don't know the best solution. I would prevent  to use dynamic allocation or use only managed types.
http://www.functionx.com/vcnet/references/managedarrays.htm
You can declare a struct with 10 items in C# and pass it to your CPP function.
>>>> what would you suggest

the problem is because of that statement.

     result = data;

here you assign an address of a local array to *a pointer* which formerly had a correct address of a storage allocated in C#.

Simply copy the data by

    for (int i = 0; i < Count; ++i)
           result[i] = data[i];

and it should work.

Regards, Alex

SOLUTION
Avatar of Deepu Abraham
Deepu Abraham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thalox

ASKER

Hi Alex,

I tried your solution, but got error saying access to invalid memory.

now I tried to change my param to
... long **result
(because of ref AND array)
and copied the data, as you said

for (int i = 0; i < Count; ++i)
           *result[i] = data[i];

now this works "nearly" ;)
I get an array with length = 1 and the right value in the first field.
but there should be more than 10 values. If I change the for - loop
for (int i = 0; i < 10; ++i)
...
I'm getting also only an array with length 1

so I believe that somehow my code in cpp tells c# that the array ends after the first value.
What could that be?

@DeepuAbrahamK
yes. I also tried other types (Int16, ...) but this seems to have no effect at all.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thalox

ASKER


Ok, I think I figured it out now. Not really what I wanted, but it works.

Thanks for all your help,

Thalox