We help IT Professionals succeed at work.

Problem getting data from cpp dll

Thalox
Thalox asked
on
323 Views
Last Modified: 2013-12-17
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
Comment
Watch Question

Deepu AbrahamR & D Engineering Manager

Commented:
Try this:

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

Best Regards,
DeepuAbrahamK
Deepu AbrahamR & D Engineering Manager

Commented:
Or try with ref Int32 [] result

Best Regards,
DeepuAbrahamK

Author

Commented:
thanks for the comment.

but it doesn't work. I'm getting the same result as with int

Commented:
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.

Author

Commented:

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);)

Commented:
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

Deepu AbrahamR & D Engineering Manager
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:

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

Thanks for all your help,

Thalox
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.