Returning Byte[] from C++ to C#

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
 
E

eran.sandler

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top