The following function prototype uses a single-counted character array that is passed both ways: from client to server and from server to client:
#define STRSIZE 500 //maximum string length
void Analyze(
[in, out, length_is(*pcbSize), size_is(STRSIZE)] char achInOut[],
[in, out] long *pcbSize);
As an in parameter, achInOut must point to valid storage on the client side. The developer allocates memory associated with the array on the client side before making the remote procedure call.
The stubs use the size_is parameter STRSIZE to allocate memory on the server and then use the length_is parameter pcbSize to transmit the array elements into this memory. The developer must make sure the client code sets the length_is variable before calling the remote procedure:
/* client */
char achInOut[STRSIZE];
long cbSize;
...
gets(achInOut); // get patient input
cbSize = strlen(achInOut) + 1; // transmit '\0' too
Analyze(achInOut, &cbSize);
In the previous example, the character array achInOut is also used as an out parameter. In C, the name of the array is equivalent to the use of a pointer. By default, all pointers are reference pointers — they do not change in value and they point to the same area of memory on the client before and after the call. All memory accessed by the remote procedure must fit the size specified on the client before the call or the stubs will generate an exception.
Before returning, the Analyze function on the server must reset the pcbSize variable to indicate the number of elements that the server will transmit to the client as shown:
/* server */
Analyze(char * str, long * pcbSize)
{
...
*pcbSize = strlen(str) + 1; // transmit '\0' too
return;
}
Instead of using a single string for both input and output, you may find it more efficient and flexible to use separate parameters.