Search code examples
c++msxml

Can IXMLDOMElementPtr pElement->text = "..." fail?


Pseudo code is as the following:

IXMLDOMElementPtr pElement;
pElement = pXMLDom->createElement("...");
pElement->text = "..."; // My question is can this step fail (such as because of memory restrain)?

More, please help me to understand the "=" operator's internal work principle! I am familiar with C, but a newbie of C++/Java.

Thanks!


Solution

  • The "=" operator's internal work principle is based on specific Microsoft C++ extensions, and as nothing to do with the legacy C++ operator overloading.

    See property (C++)

    When the compiler sees a data member declared with this attribute on the right of a member-selection operator ("." or "->"), it converts the operation to a get or put function, depending on whether such an expression is an l-value or an r-value.

    When you use #import to import the COM definitions from a DLL (as a MSXML?.DLL), a TLH file (a C++ header) is created, which reflects the content of the Type Library associated with the DLL.

    In that TLH you will find a declaration for a struct IXMLDOMNode, inheriting from IDispatch, with the lines:

    __declspec(property(get=Gettext,put=Puttext))
        _bstr_t text;
    

    The Gettext and Puttext methods are defined in a TLI file (inlined functions), generated during the import.

    The Puttext methods is:

    inline void IXMLElement::Puttext ( _bstr_t p ) {
        HRESULT _hr = put_text(p);
        if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    }
    

    The put_text method is a raw call through the interface pointer, and, as always for that kind of calls, returns a HRESULT. So, Yes, in theory, this step CAN fail.