"Bjarne Nielsen" <bn******@post11.tele.dkwrote in message
news:1h*****************************@40tude.net...
On Tue, 27 Feb 2007 16:49:27 +0100, Willy Denoyette [MVP] wrote:
>"Bjarne Nielsen" <bn**************@pc.dkwrote in message
news:Xn*********************************@207.46.2 48.16...
>>Hi all
From a C# program, I need to call an unmanaged (C++) dll. The dll fills an
array with variables and returns that to the C# program. When I have a
fixed sized array, this works fine by using the attribute
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)]
The calling program doesn't know the size of the array, so I would like the
dll to dynamically expand the array, and return the right sized array.
I have tried using the
[MarshalAs(UnmanagedType.ByValArray, SizeParamIndex=0)]
but I can't seem to get it to work.
Any suggestions?
Cheers
Bjarne Nielsen
Who allocated the array?
If the caller doesn't know the length of the array, then it's up to the callee to return
the
length and a pointer to the buffer (IntPtr in C#).
After the function returned, you'll have to unmarshal the buffer.
Willy.
Hi Willy
Thank you for your suggestion. It is the callee that allocates the buffer,
and I can return a length and a pointer to the array, but how do I
unmarshal the buffer in my C# code?
Bjarne Nielsen
Well you have to solve two problems here:
- Unmarshal the returned buffer.
- Free the buffer.
The first one depends on how you want to access the buffer (say an array of int's), do you
want to access the native array elements directly, or do you need a managed copy of the
array.
Following is a sample for the latter, the C++ takes a aparamter that is defined as a pointer
to an int[] and a the address of an int to store the length.
//C++, error checking omitted!
extern "C" void __declspec(dllexport) __stdcall Foo(int *d[], int &size)
{
size = 100;
int *arPtr = new int[size];
*d = arPtr;
for(int n = 0; n < size; n++)
*arPtr++ = n;
}
//C#
[DllImport("somedll"), SuppressUnmanagedCodeSecurity]
static extern void Foo(out IntPtr d, out int size);
....
IntPtr ptrArray = IntPtr.Zero;
int arraySize;
GetSimpleArray(out ptrArray, out arraySize);
int[] mngdArray = new int[arraySize];
try {
Marshal.Copy(ptrArray, mngdArray, 0, arraySize);
}
finally
{
// call function to delete the unmanaged array
}
foreach(int i in mngdArray )
Console.WriteLine(i);
....
Freeing the buffer depends on the allocator used, in above sample, the new operator is used
to allocate an int[], so you 'l have to declare a function that calls delete[] of the
pointer to the array. You need to make sure you call this function whenever you have done
with the marshaling.
extern "C" void __declspec(dllexport) __stdcall DeleteArray(void *d)
{
if(d)
delete[] d;
....
}
Willy.