Question

I have a COM interface defined in the file IPlan.idl that defines a property for an unsigned char* buffer.

[
    object,
    uuid(...),
    dual,
    helpstring("..."),
    pointer_default(unique)
]
interface IPlan : IDispatch
{
    ...
    [id(28), helpstring("method SetByte")
    HRESULT SetByte([in] long index, [in] unsigned char buffer);

    [id(29), helpstring("method GetByte")
    HRESULT GetByte([in] long index, [out, retval] unsigned char * pBuffer);

    [id(30), propget, helpstring("property Buffer")]
    HRESULT Buffer([out, retval] unsigned char** pBuffer);
    [id(30), propput, helpstring("property Buffer")]
    HRESULT Buffer([in] unsigned char* newBuffer);
    ...
}

My implementation class, MyPlan, is defined in C#. This class talks to a C++ COM class, CProcessor. CProcessor calculates some data into a unsigned char* buffer. This data is loaded into a MyPlan object once all the data in CProcessor has been stored in its buffer.

The metadata for IPlan defines the following function signatures:

public interface IPlan
{
    ...
    byte GetByte(int index);
    void SetByte(int index, byte buffer);   
    IntPtr get_Buffer();
    void set_Buffer(ref byte pBuffer);
    ...
}

I am able to use GetByte and SetByte from CProcessor to access and modify a byte[] variable in MyPlan. I am also able to use get_Buffer() by marshalling the byte[] variable.

My question is how can I utilize the

set_Buffer(ref byte pBuffer)

function? When I call it in CProcessor, pBuffer only contains the first byte of the buffer (like a call to SetByte(0, data)). I assume that I need to marshal the setter just as I did in the getter but my searches have come up empty. I have tried

set_Buffer([MarshalAs(UnmanagedType.LPStr)] ref byte pBuffer);

but that doesn't seem to do anything to pass in the entire array.

MarshalAs(UnmanagedType.LPStr) - how does this convert utf-8 strings to char*

Problem using dll in c#

Was it helpful?

Solution

I was able to use

[id(1), propget, helpstring("Buffer")]
HRESULT Buffer([out, retval] VARIANT* pBuffer);
[id(1), propput, helpstring("Buffer")]
HRESULT Buffer([in] VARIANT pBuffer);

to pass an unsigned char array from C++ to C#. The C# just requires a cast to byte[]

this.buffer = (byte[])myPlan.Buffer;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top