469,924 Members | 1,419 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,924 developers. It's quick & easy.

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

// 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[i] = records[i].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.MarshalDirectiveEx ception'
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?
Nov 15 '05 #1
1 6920
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.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Eric Hendriks" <er*********@raketnet.nl> wrote in message
news:5b*************************@posting.google.co m...
// 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[i] = records[i].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.MarshalDirectiveEx ception'
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?

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by William Stacey | last post: by
5 posts views Thread by Daniel Brown | last post: by
7 posts views Thread by repekto.argosoft | last post: by
1 post views Thread by eusebiu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.