PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

wm5 msxml c++ COM?

Hello to all.

I need to handle creating and loading/parsing of xml documents. I'm using c++ and other languages don't seem to be an option at this point.

Currently I've implemented this using msxml, but I must admit, this COM business seems whacko (I'm fairly new to c++ and this is my first experience with COM).

I would like to know if there is any good alternatives for handling xml than this COM business. If not, then any help with the situation noted below would be greatly appreciated.

I've used the following to get running.

Code:

namespace MSXML
{
#include <msxml.h>
}

.....

//need to define this to stop the warning messages
#define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA


I'm almost certain that I had to do something else to get rid of a "SDK doesn't support DCOM" or something like that, but I can't remember.

I'm using a private class member to hold the pointer to the xml doc.

CComPtr<MSXML::IXMLDOMDocument2> pDocument;

I use the following to initialize com and create the instance.
Code:

//Project::Open()
hResult = ::CoInitializeEx(NULL, NULL);
CoCreateInstance(MSXML::CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, MSXML::IID_IXMLDOMDocument2, (void**)&project->pDocument);
...


I have a two step process where I will open a project (above), and then (in a later thread) initialize it (below).

Code:

//Project::Init()
this->pDocument->selectNodes(BSTR(_T("//object")), &resultList);
...


The init code above is the first interaction with the document in this new thread. At this point the program hangs and after a couple seconds crashes. My feeling is that the COM context is getting messed up between threads because if I call Both "OPen" and "Init" in the same thread the code (seemingly) works.

I'm an experienced programmer but quite novice in c++ so any information is helpful and appreciated.

Thanks.
Just use TinyXML. I've never built it for CE, but I do use it in some desktop software. Looking at the source, it's well written and its string arrays support varying character sizes, so it should work for Unicode too.

Dan East
hey thanks for the suggestion. I've done a basic test of loading my project files and it works! :) :) :) If only I knew this two days ago. Oh well, at least I know that I never want to see COM again ;)

It doesn't seem to support TCHAR though. The readme states that it doesn't support a number of wide char types and only support UTF-8. It's not a big deal for me at the moment and I'm getting around it using the CAtlString class for conversion.

From the readme:
"UTF-8 is not a double byte format - but it is a standard encoding of Unicode!
TinyXML does not use or directly support wchar, TCHAR, or Microsoft's _UNICODE at this time.
It is common to see the term "Unicode" improperly refer to UTF-16, a wide byte encoding
of unicode. This is a source of confusion."

As a side note, I had to comment out the following for tinyxml to compile:

Code:


// Deprecated library function hell. Compilers want to use the
// new safe versions. This probably doesn't fully address the problem,
// but it gets closer. There are too many compilers for me to fully
// test. If you get compilation troubles, undefine TIXML_SAFE
#define TIXML_SAFE

#ifdef TIXML_SAFE
#if defined(_MSC_VER) && (_MSC_VER >= 1400 )
// Microsoft visual studio, version 2005 and higher.
//#define TIXML_SNPRINTF _snprintf_s
//#define TIXML_SNSCANF _snscanf_s



Maybe there's a better way to deal with it, but for now it works. :) Maybe I should remove the TIXML_SAFE define instead.

Anyway thanks again for the suggestion. It's so great to get rid of that COM nightmare!
UTF-8 is, IMO, a pain in the butt. The reason I say that is because each character can be a different length, depending on what it encodes. ASCII values 1-127 map exactly to UTF-8, and they take one byte each. If the most significant bit is set (values 128 through 255) then that indicates the character is multibyte, and can be from 2 to 4 bytes in length.

So you have to ask yourself do you need to support actual Unicode XML documents (and by Unicode I mean Windows CE's unicode, which is UTF-16)? If so, then you will have to convert from UTF-16 to UTF-8 and vice versa.

If you can restrict yourself to 1-127 ASCII, then just convert your string from UTF-16 to ASCII (WideCharToMultiByte, MultiByteToWideChar), which is probably what CAtlString does.

Dan East
Yeah thanks Dan. I'm perfectly fine with the limitation. The software I'm developing is a tool just for myself (at this point anyway) and I only speak english. ;)

Between your help and the recently discovered CAtlString, I've cut my code down to a third of what it was, and I'm sure it's at least 10 times less buggy. TinyXml worked fantastic btw.

Thanks for your help. It's really appreciated.

Graeme