OUT parameters can be of standard types like char, int, long, float etc. These are not allocated by COM server. Client passes the address of these variables and server puts the return value and then it returns to client. Client can use these values.

This is not same for the BSTR, Strings, arrays or other type of dynamically allocate buffers. Client passes the address of a pointer. COM server allocates the buffer and fills the return values. It then writes the address value to the parameter which client has provided. Now client can read this buffer and finally this buffer should be deallocated. Client has the control to this pointer so client can decide when to free up this buffer. It is the responsibility of the client to properly deallocate the buffer.

BSTR* strSample; /* BSTR * or OLECHAR ** */
for (int i = 0; i < 5; i++)
  strSample = NULL;
  hr = ReturnBSTR(i, &strSample);
  if(hr == S_OK)
    wprintf(“String %d is %s\n”, i + 1, strSample);
    /* Now free this sting, else memory leak */
    SysFreeString (strSample);

  } else {
    wprintf(“ReturnBSTR return %x\n”, hr);

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.