Re: Problem with writing XML DOM tree to memory buffer.
Stuart Redmann wrote:
> Hello newsgroup,
> I tried to write a small test application that creates an XML DOM tree
> and writes the contents
> into a string buffer (saving to file works perfectly). Unfortunately, I
> seem to go wrong
> at some point (the text displayed contains only question marks :(
> Can anybody give me some suggestions?
[snipped erroneous code snippet]
In addition to Igor's reply, you don't need to call
`Detach()' om `CComBSTR' object; it will leak BSTR.
`CComBSTR' class already has `operator BSTR'. Also, calling
`GlobalLock' is not enough. You should also call
`GlobalSize' in order to determine actual size of written
data. Stream doesn't append terminating NUL automatically.
Here's working example (made with VC2005, though it should
be easily adaptable to VC6):
using namespace MSXML2;
int _tmain(int /*argc*/, _TCHAR* /*argv*/)
// Create the document and fill it with
// some example data.
IXMLDOMElementPtr spCurrentNode =
// Let the document persist into a memory stream.
HRESULT hr = ::CreateStreamOnHGlobal(
NULL, TRUE, &spStream);
hr = spDocument->save(vtStream);
I should have seen this one (tracing into the save call should have done the trick).
The big question is what IXMLDOMDocument is doing with the Boolean value I have
passed (I would have expected that the save method had fired an E_INVALIDARG).
HGLOBAL hMem = NULL;
hr = ::GetHGlobalFromStream(spStream, &hMem);
LPVOID lp = ::GlobalLock(hMem);
CStringA strXmlText((LPCSTR)lp, ::GlobalSize(hMem));
::MessageBoxA(NULL, strXmlText, "XML Text",
MB_OK | MB_ICONINFORMATION);
catch(const _com_error& e)
::MessageBox(NULL, e.ErrorMessage(), _T("Error"),
MB_OK | MB_ICONERROR);
Thank you very much (the same for Igor).