473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 7196
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: William Stacey | last post by:
Using the following struct def, how can I tell (using reflection) if "ba" has the marshal attribute and get the "ByValArray" and maybe even the size? In the bigger picture, given a struct (or a...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
5
by: Daniel Brown | last post by:
I am coding a managed C# wrapper for an unmanaged C DLL and I am unable to marshal a structure that contains an array of structures. When executed, the following code throws an ArgumentException...
0
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
1
by: nicewenyan | last post by:
I want to pass a managed c# byte (8 bit) array into a unmanaged c++ function: extern "C" void AddData(unsigned int* data); I use P/Invoke on managed side to do the marshaling as following: ...
7
by: repekto.argosoft | last post by:
Hi, I'm working with Marshalling. I got an idea to pass an object to the Native side as a byte array i.e. as void* in terms of C. For example: I have a .NET structure Point that contains...
1
by: eusebiu | last post by:
Hello.... I have an MFC ActiveX and creates a BYTE* buffer(as a CDialog Class member) and I've wrote a function that gets the pointer to that buffer and it's size: void...
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.