-
February 4th, 2011, 01:21 AM
#1
passing string from RPC client to server
Hi,
I need to return the string data from the RPC function that i am calling. String will be very dynamic so i am interested in using stl string inside the idl.
I tried dynamic arrays but i am unable to persist the pointer from heap corruption... e.g. here is my idl code
Code:
interface myinterface
{
void RPC_GetGroupStatusEx([out] char ** szXMLString);
}
server implemented code
Code:
void RPC_GetGroupStatusEx(unsigned char ** szXMLString)
{
CString strXmlRawData = GetSomeXmlData(); //it return xml string
unsigned char* dyn = new unsigned char[strXmlRawData.GetLength()];
sprintf((char*)dyn,"%s", strXmlRawData.GetBuffer());
*szXMLString =dyn;
}
client caller code
Code:
unsigned char *ms = NULL;
//i have tried even initialized pointer by sending it to the server but it also gave me heap corruption , also i have used address of pointer but same result
RpcTryExcept
{
RPC_GetGroupStatusEx(&ms);
}
RpcExcept(1)
{
int a = RpcExceptionCode();
std::cerr << "Runtime reported exception " << RpcExceptionCode()
<< std::endl;
}
RpcEndExcept
it always crash at the server side code after leaving that function , one more thing is RPC server is on the different machine. Every other functions with data types works fine except string.
Please guide me what i am doing wrong in the code? How can i use stl string inside the idl file ?
Regards
Last edited by Wolvorine; February 4th, 2011 at 01:24 AM.
-
February 4th, 2011, 05:08 AM
#2
Re: passing string from RPC client to server
Something like this (please refer to IDL documentation)
Code:
interface myinterface
{
void RPC_GetGroupStatusEx(UINT nSize, [ out, size_is(nSize)] char ** szXMLString);
}
Last edited by Igor Vartanov; February 4th, 2011 at 05:13 AM.
Best regards,
Igor
-
February 4th, 2011, 07:11 AM
#3
Re: passing string from RPC client to server
hi Igor Vartanov,
i have tried this example after your recommendation also from the documentation which is written http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
HRESULT Proc7(
[out] long * pSize,
[out, size_is( , *pSize)] my_type ** ppMyType); /* Specifies a pointer
to a sized pointer,
which points to a block
of my_types, whose size is
unknown when the stub
calls the server. */
But at the end of my function inside server i see this error
Code:
HEAP CORRUPTION DETECTED: after Normal block (#4165) at 0x0081F710.
CRT detected that the application wrote to memory after end of heap buffer.
my new idl file is now as
Code:
interface myinterface
{
void RPC_GetGroupStatusEx(([out] long * pSize,[out, size_is( , *pSize)] char ** szXMLString););
}
server code
Code:
void RPC_GetGroupStatusEx (long *size,unsigned char ** szXMLString)
{
CString strXmlRawData = GetSomeXmlData(); //it return xml string
unsigned char* dyn = new unsigned char[strXmlRawData.GetLength()];
sprintf((char*)dyn,"%s", strXmlRawData.GetBuffer());
*pSize = strXmlRawData.GetLength()
*szXMLString =dyn;
}
Also i have used another way as below
Code:
void RPC_GetGroupStatusEx (long *size,unsigned char ** szXMLString)
{
CString strXmlRawData = GetSomeXmlData(); //it return xml string
*size = strXmlRawData.GetLength();
*szXMLString =new unsigned char[*size];
sprintf((char*)*szXMLString,"%s",strXmlRawData.GetBuffer());
}
both of the style of server function gives heap error , first one raised right after the end of the function while the second one gives in after the end of function and then in middle of another statement.
any idea? please help
-
February 4th, 2011, 07:38 AM
#4
Re: passing string from RPC client to server
Is your build ANSI or UNICODE? Your code seems to assume it'd be incorrect in the UNICODE build!
Victor Nijegorodov
-
February 5th, 2011, 01:39 AM
#5
Re: passing string from RPC client to server
my server and client both are unicode but my interface is non-unicode and i am communicating things with using char instead of wchar. So basically when i call the rpc function i provides non-unicode character pointers.
What do you think???
-
February 5th, 2011, 03:28 AM
#6
Re: passing string from RPC client to server
I think that the length of CString text in bytes (aka unsigned chars) is GetLength() * sizeof(TCHAR), not just the GetLength()
Victor Nijegorodov
-
February 5th, 2011, 03:47 AM
#7
Re: passing string from RPC client to server
Code:
USES_CONVERSION;
CStringA strXmlRawData = W2A(objStatusXml.GetRawData().GetBuffer());
*size = strXmlRawData.GetLength();
*szXMLString =new unsigned char[*size];
sprintf((char*)*szXMLString,"%s",strXmlRawData.GetBuffer());
above is my code CStringA is asci version explicitly initialized but still it gives error with both of the styles i previously mentioned...
-
February 5th, 2011, 03:30 PM
#8
Re: passing string from RPC client to server
Code:
*size = strXmlRawData.GetLength() + 1;
*szXMLString =new unsigned char[*size];
sprintf((char*)*szXMLString,"%s",strXmlRawData.GetBuffer());
Your buffer allocation seems missing space for a zero terminator. Besides, I'm not sure about the way you allocate that (actually, I'm sure new doesn't belong here). You definitely need some reading about RPC out parameters, what side and how allocates that.
Last edited by Igor Vartanov; February 5th, 2011 at 03:40 PM.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|