CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    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.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  3. #3
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    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

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  5. #5
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    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???

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  7. #7
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    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...

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: passing string from RPC client to server

    Code:
    	*size = strXmlRawData.GetLength() + 1;
    	
    	*szXMLString =new unsigned char[*size];
    	
    	sprintf((char*)*szXMLString,"&#37;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
  •  





Click Here to Expand Forum to Full Width

Featured