C# – COM event with binary data in arguments

ccomevents

I am trying to develop a COM object using C++ and ATL to be used by both C++ and C# Windows Mobile clients. The COM object wraps up all of the logic to connect to our server and send/receive data using our proprietary protocol. I am having some difficulty coming up with an OnReceive event that works correctly with C# and C++.

I have defined the event function like this:

HRESULT OnReceive(BYTE* pBuffer, LONG lSize);

But when I look at the function in C# or Object Browser, it comes out as:

OnReceive(ref byte pBuffer, int lSize);

How would I treat "ref byte" as a pointer in C#? How can I pass binary data to OnReceive and allow both C++ and C# clients to access the binary data?

Best Answer

You basically have two options: use a SAFEARRAY of BYTEs or stuff the data into a BSTR. The latter, although ugly, used to be the default hack to pass binary data to VB6 components. Although I have never tried it, I guess it should work for .Net, too.