المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Return int* to C# from C++ DLL?



C# Programming
05-07-2009, 08:12 PM
Hi all,

I've wasted a day just trying to get a simple pointer back to C# from C++!

I have a C++ DLL returning an int* to a C# program. The problem is the int* in C# remains null after the assignment.

When I assign the C++ result to an IntPtr, I get a correct non-null value. However any attempt to assign this to an int* instantly results in null.

I've tried (from C#):

IntPtr intp = cppFunction (); // Non-null.

int* pi = (int *) intp; // Results in null.

int* pi = (int *) intp.ToPointer (); // Results in null.


void* vp = intp.ToPointer (); // Non-null.
int* pi = (int *) vp; // Results in null.
I then gave up on simple assignment, and tried to get the value by reference:

C#:
[DllImport("vdrdll.dll", EntryPoint = @"?getLineContentsAsArg@@YAXAAPAH@Z")]
public unsafe static extern void getLineContentsAsArg (ref int* lca);

C++:
DLLEXPORT void getLineContentsAsArg (int*& lca)
{
lca = calculateLineContents ();
}

A non-null pointer value is assigned in the DLL, but when it gets back to C#, it's null.

Can anyone tell me how to get an int* back to C#?
Thanks!
Alan