How to marshal a managed byte[][] to unmanaged byte **???

E

Eric Hendriks

// In an unmanaged DLL the following function must be called:
// int VFGeneralize(const BYTE * const * features);
// "features" parameter is supposed to be an array of byte arrays.
// function is Marshaled as follows:
[DllImport("duh.dll", EntryPoint="VFGeneralize", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
static extern int VFGeneralize(byte[][] features);


In C# I have the following:
// Allocate memory to store "Count" references to byte arrays
byte[][] src = new byte[Count][];

// Point the references to existing byte arrays
for( int i = 0; i < Count; i++ )
{
// records is an array of structs. in each record Featurebuffer
points to
// an array of bytes.
src = records.FeatureBuffer;
}

// Try to call the DLL with:
VFGeneralize(src);

The above compiles Ok, but when calling the VFGeneralize function I
get the exception:
An unhandled exception of
type 'System.Runtime.InteropServices.MarshalDirectiveException'
occurred in veriwrapper.dll

Additional information: Can not marshal parameter #2: Invalid
managed/unmanaged type combination (Arrays can only be marshaled as
Array or SafeArray).

How to solve this?
 
N

Nicholas Paldino [.NET/C# MVP]

Eric,

I think that you will have to marshal this yourself. First, you will
have to allocate a block of memory that will hold each of the sub-arrays.
The size of this will be the size of the IntPtr structure multiplied by the
number of sub-arrays that there are. Once you have that, you will have to
cycle through each of the arrays and allocate space for the elements in that
array, and manually marshal each element into those arrays. Finally, you
would have to change the declaration of the method in C# so that it takes an
IntPtr argument.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top