For ex. i have c# COM object with such function:
int GetString([In, Out, MarshalAs(UnmanagedType.LPStr)]ref string str)
then i’m calling int from c++ (in c++ COM object is used via #import keyword), in generated wrapper method is declared as:
GetString(LPSTR * str, long * retVal)
i’m calling it this way:
char myStr[40];
LPSTR buf = (LPSTR)myStr;
LPSTR pBuf = &buf;
pComObject->GetString(pBuf);
what is strange:
1) myStr is not filled, and buf value is changed (it doesnt the same with myStr after the call) so, i suppose that c# marshaller allocates new block of memory
2) if c# allocates memory so should i call free(buf); or not?
Yes, the client should release the memory.
In this case, one should use the CoTaskMemFree function to free the received buffer. Since the buffer may not be allocated on the client’s heap. CoTaskMemFree will call an appropriate deleter.