DOM Document without a Site Object (C++ Code Listing)

 

This topic contains the source code listing for the DOMWithoutSiteObj project, which implements an XML DOM document that does not contain a site object. Because of this, the default Internet Security Manager is used to control access to resources, here the local file Sample.xml.

Description: DOM Document without a Site Object

The C/C++ source code contained in the files listed below performs the following actions:

  1. Creates an IXMLDomDocument object, but does not set the site object on the IXMLDomDocument object.

  2. Calls the load method on the IXMLDomDocument object, specifying the path to the sample.xml.

Because it uses the default Internet Security Manager, the resulting program should load local XML files successfully. Typically, because of security concerns, this is not the desired outcome. Instead, we want to block loading of local files. The example following this one shows how to set security options and the site object on a DOM document.

Note

The following C/C++ code uses smart pointers, which are represented by the _COM_SMARTPTR_TYPEDEF macro and the _com_ptr_t type.

Code Listing: DOMWithoutSiteObj.cpp

#include "stdafx.h"  
#include <comdef.h>  
#include <string>  
using namespace std;  
#include <msxml6.h>  
#include "msxml2.h"  
  
#pragma comment( lib, "msxml6.lib")  
  
_COM_SMARTPTR_TYPEDEF(IXMLDOMDocument2, __uuidof(IXMLDOMDocument2));  
_COM_SMARTPTR_TYPEDEF(IXMLDOMParseError, __uuidof(IXMLDOMParseError));  
  
// CLSIDs for MSXML objects (varies with MSXML version).  
#define DOMCLSID CLSID_DOMDocument60  
  
// Helper function that puts output in stdout.  
void dprintf(char* format, ...)  
{  
    va_list args;  
    int len;  
    char* buffer;  
  
    va_start(args, format);  
    len = _vscprintf(format, args) // _vscprintf does not count  
                               + 1; // terminating '\0'  
    buffer = (char*)malloc(len * sizeof(char));  
    vsprintf_s(buffer, len, format, args);  
    puts(buffer);  
    free(buffer);  
}  
  
wstring GetErrorMessage(IUnknownPtr pUnk, REFIID refiid)  
{  
    HRESULT hr;  
    wstring result;  
    IErrorInfoPtr pErrorInfo = NULL;  
    hr = GetErrorInfo(0, &pErrorInfo);  
    if (hr != S_OK)  
    {  
        return result;  
    }  
  
    BSTR bstrMessage;  
    hr = pErrorInfo->GetDescription(&bstrMessage);  
  
    _bstr_t bsMessage(bstrMessage, true);  
  
    result = (LPCWSTR)bsMessage;  
  
    return result;  
}  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    IXMLDOMDocument *pXMLDom=NULL;  
    IXMLDOMParseError *pXMLErr=NULL;  
    BSTR bstr = NULL;  
    HRESULT hr;  
  
    CoInitialize(NULL);  
  
    // Create a DOM object.  
    IXMLDOMDocument2 *pDOMDocument = NULL;  
    hr = CoCreateInstance(DOMCLSID, NULL, CLSCTX_INPROC, IID_IXMLDOMDocument2, (LPVOID *)&pDOMDocument);  
    if (FAILED(hr))  
    {  
        wstring msg = GetErrorMessage(pDOMDocument, IID_IUnknown);  
        dprintf("%s (%s, %d)\n", (LPCSTR)msg.c_str(), __FILE__, __LINE__);  
        goto clean;  
    }  
  
    // Load the file.  
    VARIANT_BOOL bSuccess;  
    pDOMDocument->put_async(VARIANT_FALSE);  
    pDOMDocument->put_resolveExternals( VARIANT_TRUE );  
    pDOMDocument->put_validateOnParse(VARIANT_FALSE );  
  
    dprintf("Calling load on sample.xml.\r\n");  
  
    pDOMDocument->load(variant_t("sample.xml"), &bSuccess);  
    if (bSuccess!=VARIANT_TRUE)   
    {  
        hr = pDOMDocument->get_parseError(&pXMLErr);  
        if (FAILED(hr))  
        {  
            wstring msg = GetErrorMessage(pDOMDocument, IID_IUnknown);  
            dprintf("%s (%s, %d)\n", (LPCSTR)msg.c_str(), __FILE__, __LINE__);  
            goto clean;  
        }  
        hr = pXMLErr->get_reason(&bstr);  
        if (FAILED(hr))  
        {  
            wstring msg = GetErrorMessage(pXMLErr, IID_IUnknown);  
            dprintf("%s (%s, %d)\n", (LPCSTR)msg.c_str(), __FILE__, __LINE__);  
            goto clean;  
        }  
        dprintf("Failed to load DOM from sample.xml. %S\n", bstr);  
        goto clean;  
    }  
  
    hr = pDOMDocument->get_xml(&bstr);  
    if (FAILED(hr))  
    {  
        wstring msg = GetErrorMessage(pDOMDocument, IID_IUnknown);  
        dprintf("%s (%s, %d)\n", (LPCSTR)msg.c_str(), __FILE__, __LINE__);  
        goto clean;  
    }  
    dprintf("XML DOM loaded from sample.xml:\n%S\n", bstr);  
  
clean:  
    if (bstr) SysFreeString(bstr);  
  
    CoUninitialize();  
    return 0;  
}  
  

Code Listing: Sample.xml

<sample>  
</sample>  
  

See Also

Building a Custom Internet Security Manager
Custom Internet Security Manager Examples
DOM Document with a Site Object (C++ Code Listing)