I am implementing a Windows system service, which acts as RPC server, and a corresponding client, both in C++. I am using plain Windows RPC functionality.
Passing strings from the RPC client to the server is easy. Just declare the function parameter in the IDL file like this:
[in, string] wchar_t* myString
MIDL will take care of the memory-allocation magic. Works like a treat.
Returning a modified client string is also easy:
[in, out, string] wchar_t* myString
That requires me to properly size the string on the client side, though.
Problem:
I need to return strings from the server to the client. I do not know on the client how large they are going to be, so memory allocation on the client is not an option.
I could allocate a very large amount of memory, say 10K, an amount large enough for every string the server could possibly return. But that is a huge waste of resources (memory, network), and I still cannot know for certain that the server never needs to return a larger string.
What I tried:
Amonst many other things I tried the technique used in Microsoft's strout sample. It worked when calling the RPC function for the first time, but crashed the server when called for the second time.