"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:Os**************@TK2MSFTNGP15.phx.gbl...
I have a managed DLL that I've used to expose a C++ class. One of the
functions in this class reads a line out of a file into a buffer of type
"unsigned char*". Since all the data is just straight byte data (that is,
I'm concerned with individual bits packed into the bytes), I'd like to put
that data into a byte array that's passed from the C# app to the DLL
function.
In the past, I've been able to marshal strings to character strings
without much trouble. But marshaling this byte array, which, for all
intensive purposes, holds exactly the same type and style of data as an
unsigned char array would in C++, has proven to be a ridiculously
difficult experience.
Anyone got any ideas?
Lee Crabtree
Not sure what problems you have with this, but it's rather simple.
In C#
//Following declaration tells the marshaler to pass the address of the
byte[] to C++ unmanaged function
[DllImport("unm.dll")]
private static extern double UnmFunc (byte[] val, int len);
// allocate array large enough to contain filedata
byte[] ar = new byte[xxx];
// pass the array reference and (optional) array length
UnmFunc(ar, ar.Length);
// upon return ar contains file data
In C++ you have two options, in both the first arg. is the address of a
buffer
1. address points to a unsigned char buffer with (optional) length len
__declspec(dllexport) void __stdcall func_c6 (unsigned char* d, int len);
2. address points to an unsigned char[] with length len
__declspec(dllexport) void __stdcall UnmFunc (unsigned char d[], int len);
Note: both 1 and 2 are threaten the same by the interop marshaler, only the
C code semantics differs.
Willy.