Marshalling byte** from C++ in C#

  • Thread starter Thread starter John Bravo
  • Start date Start date
J

John Bravo

Hello,

I have a function in C++ with the following definition

typedef HRESULT (WINAPI *PGetData)(OUT BYTE **pabc);

and am using P/Invoke method to call this method from C#.

I need the output parameter to be stored inside a string variable
containing byte value.

I have the following defined in my C# code:
public class ClassComm
{
[DllImport("mydll.dll", CharSet = CharSet.Auto, EntryPoint="#200")]
public static extern int GetDataX (
ref byte[] bytearr);
}
and calling it using:

byte[] bytearr = null;
ClassComm.GetDataX (ref bytearr);

However the length of bytearr returned is only 1 whereas when stepping
through the C++ code found the length to be 20.

What is the type of marshalling that needs to be used in this case?

Please help.

Thanks.
 
John,

Try it like this

[DllImport("mydll.dll", CharSet = CharSet.Auto, EntryPoint="#200")]
public static extern int GetDataX (
out IntPtr bytearr);

IntPtr p;
ClassComm.GetDataX (out p);
byte[] bytearr = new byte[20]; //assuming the length is always 20
Marshal.Copy(p, bytearr, 0, 20);
// free p here if needed



Mattias
 
Back
Top