Click to See Complete Forum and Search --> : .NET to COM to Native string passing


sperlis
May 24th, 2009, 01:50 PM
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:


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


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,

boudino
May 25th, 2009, 05:50 AM
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.