Click here to Skip to main content
15,880,543 members
Articles / Desktop Programming / MFC

C++ Wrapper classes for the COM interfaces of Microsoft XML parser (MSXML)

Rate me:
Please Sign up or sign in to vote.
4.46/5 (32 votes)
1 Jan 20029 min read 590.7K   6.9K   114  
The provided MFC extension DLL provides easy to use wrapper classes for all COM-interfaces of Microsoft's DOM-/SAX implementation.
/********************************************************************
*
* Copyright (C) 2001 Sven Wiegand
* 
* This file is free software; you can redistribute it and/or
* modify, but leave the headers intact and do not remove any 
* copyrights from the source.
*
* If you have further questions, suggestions or bug fixes, please
* let me know
*
*    sven.wiegand@web.de
*
********************************************************************/

#include "stdafx.h"
#include "InterfaceWrapper.h"

//-------------------------------------------------------------------
// class CInterfaceImplementationWrapper
//-------------------------------------------------------------------

CInterfaceImplementationWrapper::CInterfaceImplementationWrapper()
:	m_ulRef(0)
{}


CInterfaceImplementationWrapper::~CInterfaceImplementationWrapper()
{}


ULONG CInterfaceImplementationWrapper::AddRef()
{
	return InterlockedIncrement((long*)&m_ulRef);
}


ULONG CInterfaceImplementationWrapper::Release()
{
	ULONG	ulRef = InterlockedDecrement((long*)&m_ulRef);
	if (ulRef == 0)
		delete this;

	return ulRef;
}


HRESULT CInterfaceImplementationWrapper::QueryInterface(REFIID riid, void **ppv)
{
	if (*ppv == NULL)
		return E_FAIL;

	if (riid == IID_IUnknown)
		*ppv = dynamic_cast<IUnknown*>(this);
	else
		*ppv = NULL;

	if (*ppv == NULL)
		return E_NOINTERFACE;

	AddRef();
	return S_OK;
}


void CInterfaceImplementationWrapper::MethodPrologue()
{
}


//-------------------------------------------------------------------
// class CDispatchInterfaceImplementationWrapper
//-------------------------------------------------------------------

CDispatchInterfaceImplementationWrapper::CDispatchInterfaceImplementationWrapper(REFGUID rguidTypeLibrary, USHORT usVerMajor, USHORT usVerMinor, REFGUID rguidInterface)
:	CInterfaceImplementationWrapper()
{
	ITypeLib	*ptl = 0;
	HRESULT hr = LoadRegTypeLib(rguidTypeLibrary, usVerMajor, usVerMinor, 0, &ptl);
	ASSERT(SUCCEEDED(hr));
	if (!SUCCEEDED(hr))
		AfxThrowComException(hr);

	hr = ptl->GetTypeInfoOfGuid(rguidInterface, &m_pTypeInfo);
	ptl->Release();
	ASSERT(SUCCEEDED(hr));
	if (!SUCCEEDED(hr))
		AfxThrowComException(hr);
}


CDispatchInterfaceImplementationWrapper::~CDispatchInterfaceImplementationWrapper()
{
	m_pTypeInfo->Release();
}


HRESULT CDispatchInterfaceImplementationWrapper::GetTypeInfoCount(UINT *pctinfo)
{
	ASSERT(pctinfo);
	*pctinfo = 1;
	return S_OK;
}


HRESULT CDispatchInterfaceImplementationWrapper::GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
{
	ASSERT(iTInfo == 0 && ppTInfo != 0);
	(*ppTInfo = m_pTypeInfo)->AddRef();
	return S_OK;
}


HRESULT CDispatchInterfaceImplementationWrapper::GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgid)
{
	ASSERT(riid == IID_NULL);
	return m_pTypeInfo->GetIDsOfNames(rgszNames, cNames, rgid);
}


HRESULT CDispatchInterfaceImplementationWrapper::Invoke(DISPID id, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
	ASSERT(riid == IID_NULL);
	return m_pTypeInfo->Invoke((PVOID)GetInterface(), id, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}


//-------------------------------------------------------------------
// class CComException
//-------------------------------------------------------------------

CComException::CComException(HRESULT hr)
:	CException(TRUE),
	m_hr(hr)
{}


void CComException::Throw(HRESULT hr)
{
	CComException	*pE = new CComException(hr);
	if (!pE)
		return;

	throw pE;
}


//-------------------------------------------------------------------
// globals
//-------------------------------------------------------------------

void AfxThrowComException(HRESULT hr)
{
	CComException::Throw(hr);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
Sven Wiegand (1976), IT professional living in Berlin (Germany), develops open source software in his free time. His most successfull project is the LaTeX IDE TeXnicCenter which is distributed under the terms of the GNU-GPL and has more than 100,000 users all about the world.

"The picture shows me with my racing bike on the top of the Roque de los Muchachos (2426m) - the highest point of the canarian island La Palma."

Comments and Discussions