Returning Byte[] from C++ to C#

  • Thread starter Thread starter capsoncc
  • Start date Start date
C

capsoncc

I am creating a method that I need to return an array of Bytes. How do
I return a Byte array in C++ as an output variable?

In C# I can do this

Calling application (C#)
{
byte[] attByteArray=null;
test.GetAttachmentBinary(attachment, ref attByteArray);
}

C# Method that would return my Byte Array information.
public void GetAttachmentBinary(MAPICDO::Attachment pAttachment, ref
byte[] AttachmentData)
{
....
}

Here is my C++ Method

void LiveMailAttachments::GetAttachmentBinary(MAPICDO::Attachment*
pAttachment, ?????? b)
{
...
Byte __pin* p = &attachmentArray[0];
hr = streamUnk->Read(p, attSize, (ULONG*)&attSize);

//WHAT and how do I need declare my variable b as in order to return
it to c# as a Byte[]? I want to return the value read intp p from my
IStream

}

How do I do the same in C++? I want to have my calling Application be
the same C# application. But I want my GetAttachmentBinary method to be
in Managed C++. How Would I define my C++ method so that it would
return the byte[]?

Thanks in advance
Chris Capson
 
I'm not sure what you are trying to do here.

If your C++ code in COM then you shoud either pass in a SafeArray (it
will automatically will get translated into System.Array when adding a
reference to the COM object in C#).

If your C++ code in partially managed (using C++ CLI) you can pass a
..NET byte array, pin it and fill it with the information.

The 3rd option is to pass back a point from your function to a block of
memory and use some unsafe C# code with the Marshal.ReadByte API to
read the buffer byte by byte.

Hope this helps,
Eran
 
Back
Top