Search code examples
c++cpointersrpcmidl

Windows C - Pass pointer by reference to dynamic allocated memory of unsigned char with MIDL Protocol


I'm using MIDL protocol(RPC) and i trying to pass pointer by reference to an allocated memory of unsigned char. but only the first attribute of the array filled with the correct value.

MIDL CODE:

 // File FPGA_RPC_MIDL.idl
 [
    // A unique identifier that distinguishes this interface from other interfaces.
uuid(00000001-EAF3-4A7A-A0F2-BCE4C30DA77E),

// This is version 1.0 of this interface.
version(1.0)
 ]

interface FPGA_RPC_MIDL // The interface is named FPGA_RPC_MIDL
{

    int get_Message([ref, out]  unsigned char* message_out);

}

The server code:

int get_Message(
/* [ref][out] */ unsigned char *message_out)
{
   message_out[0] = 0x25;
   message_out[1] = 0x26;
   message_out[2] = 0x27;
   return 0;'
}

The client code:

int main
{
      message_out = (unsigned char *)malloc(sizeof(unsigned char)*3);
      get_Message(message_out);
      printf("%x, %x, %x",message_out[0],message_out[1],message_out[2])
}

output:

25,0,0

How can i pass by reference all the array?


Solution

  • [ref, out] is the wrong set of attributes to use in this situation. You are telling MIDL that get_Message() returns a single character by reference as an output value, and so that is how your data is getting marshalled, but that is not what your code wants. It wants to fill a multi-character array instead, so you have to marshal it accordingly.

    Try this:

    int get_Message([in, out, size_is(3)] unsigned char message_out[]);
    

    Or simply:

    int get_Message(unsigned char message_out[3]);
    

    Refer to MSDN for more details:

    MIDL Arrays