CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2002
    Posts
    78

    .NET to COM to Native string passing

    Hi,

    I have a problem which I can't figure out. I have code in C# which calls a native function via COM interface.
    I have an object which I serialize to XML, then pass the string to the native C++ code which I use mxsml (4) to handle.

    The flow is:

    Code:
    //--- C#
    MemoryStream memStream = new MemoryStream();
    myObject.ExportToXml(memStream);
    
    /* Inside there is something like this:
          {
             XmlTextWriter xmlWriter = new XmlTextWriter(memStream, null);
             // here I build the XML
          }
    */
    
    string xmlString = Encoding.UTF8.GetString(memStream.ToArray());
    
        //--- COM C++
        myComObject.passXmlOn(xmlString); // MyComObject::passXmlOn(BSTR)
        // here the native object is accessed and handleXml is called
    
            //--- Native C++
            myNativeObj.handleXml(xmlString); // MyNativeObj::handleXml(wstring)
                // here MSXML2::DOMDocument40::loadXml(xmlString) is called
    At this point I try to create an MSXML object, and it fails.

    However, when I write the XML to file (I pass FileStream to my ExportToXml) and then I create the MSXML in the native code from the same file, everything works fine.

    Code:
    FileStream fileStream = new FileStream(someFileLocation);
    myObject.ExportToXml(fileStream);
    
    
        //--- COM C++
        myComObject.readXml(); 
        // here the native object is accessed and handleXml is called
    
            //--- Native C++
            myNativeObj.handleXml(); // MyNativeObj::handleXml()
                // here MSXML2::DOMDocument40::load(someFileLocation) is called
    Any ideas?

    Thanks,
    sperlis

    "Let's code quickly now, so we'll have time to debug later"

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: .NET to COM to Native string passing

    For case nobody more competen won't answer, I would think the string passed to COM library should be treated by Marshal class. Try to look at Marshal.StringToBSTR() method.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

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