Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C++

Customized Visual Studio .NET package: your fully integrated document window inside the IDE

Rate me:
Please Sign up or sign in to vote.
4.72/5 (20 votes)
20 Dec 200313 min read 63.7K   1.1K   39  
Create a fully integrated document window inside the Visual Studio IDE.
#include "StdAfx.h"
#include "serviceprovider.h"
#include "VsRegisterEditors.h"
/*
START_IMPL_SPY(IServiceProvider,2)

STDMETHODIMP SP_QueryInterface( void* pThis, REFIID refiid, LPVOID* ppv )
{
	HRESULT hr;
	CALL_METHOD3(0,pThis,refiid,ppv)//QueryInterface( refiid, ppv );
	GET_RET_VAL(hr);
	CDbg::Trace( "   IServiceProvider::QueryInterface returned %X, ptr=%p, REFIID=", hr, *ppv );
	CDbg::TraceGuid( refiid );
	return hr;
}

STDMETHODIMP_(ULONG) SP_AddRef(void* pThat)
{
	ULONG uRC;
	CALL_METHOD1(1,pThat)//AddRef();
	GET_RET_VAL(uRC);
	CDbg::Trace( "   IServiceProvider::AddRef returned %u\n", uRC );
	return uRC;
}

STDMETHODIMP_(ULONG) SP_Release(void* pThat)
{
	ULONG uRC;
	CALL_METHOD1(2,pThat)//Release();
	GET_RET_VAL(uRC);
	CDbg::Trace( "   IServiceProvider::Release returned %u\n", uRC );
	return uRC;
}

STDMETHODIMP SP_QueryService( void* pThat, REFGUID guidService, REFIID riid, void** ppv)
{
	HRESULT hr;
	CALL_METHOD4(3,pThat,guidService,riid,ppv)//QueryService( guidService, riid, ppv );
	GET_RET_VAL(hr);
	CDbg::Trace( "   IServiceProvider::QueryService returned %X, GUID Serv=\n", hr );
	CDbg::TraceGuid( guidService, FALSE );
	CDbg::Trace( " RefID=" );
	CDbg::TraceGuid( riid );
	return hr;
}

SPY_START_VTBL(4)
	SPY_ENTRY_(SP_QueryInterface,0)
	SPY_ENTRY_(SP_AddRef,1)
	SPY_ENTRY_(SP_Release,2)
	SPY_ENTRY_(SP_QueryService,3)
SPY_END_VTBL()

END_IMPL_SPY()

*/


CServiceProvider::CServiceProvider(void)
{
	m_pSP = NULL;
	m_pRE = NULL;
}

CServiceProvider::~CServiceProvider(void)
{
}

BOOL CServiceProvider::Attach( IServiceProvider* pSP )
{
	if ( m_pSP == NULL )
	{
		m_pSP = pSP;
		return TRUE;
	}
	return FALSE;
}

// IUnknown
STDMETHODIMP CServiceProvider::QueryInterface( REFIID refiid, LPVOID* ppv )
{
	if ( m_pSP )
	{
		CDbg::Trace( "   IServiceProvider::QueryInterface( REFIID=" );
		CDbg::TraceGuid( refiid );
		CDbg::TraceC( ", [out] ppv=0x%p )\n", ppv );
		
		HRESULT hr = m_pSP->QueryInterface( refiid, ppv );
		CDbg::Trace( "   IServiceProvider::QueryInterface( ,[out] *ppv=0x%p ) = %X\n", *ppv, hr );
		return hr;
	}
	return E_INVALIDARG;
}

STDMETHODIMP_(ULONG) CServiceProvider::AddRef(void)
{
	if ( m_pSP )
	{
		ULONG uRC = m_pSP->AddRef();
		//CDbg::Trace( "   CServiceProvider::AddRef returned %u\n", uRC );
		return uRC;
	}
	return 0;
}

STDMETHODIMP_(ULONG) CServiceProvider::Release(void)
{
	if ( m_pSP )
	{
		ULONG uRC = m_pSP->Release();
		//CDbg::Trace( "   CServiceProvider::Release returned %u\n", uRC );
		if ( 0 == uRC )
		{
			m_pSP = NULL;
		}
		return uRC;
	}
	return 0;
}

STDMETHODIMP CServiceProvider::QueryService( REFGUID guidService, REFIID riid, void** ppv)
{
	if ( m_pSP )
	{
		CDbg::Trace( "   IServiceProvider::QueryService( guidSvc=" );
		CDbg::TraceGuid( guidService );
		CDbg::TraceC( ", riid=" );
		CDbg::TraceGuid( riid );
		CDbg::TraceC( ", [out]ppv=0x%p )\n", ppv );

		HRESULT hr = m_pSP->QueryService( guidService, riid, ppv );
		
		if ( riid == IID_IVsRegisterEditors )
		{
			if ( (m_pRE != NULL) && (m_pRE->m_pRealObject == *ppv) )
			{
				*ppv = m_pRE;
			}
			else
			{
				m_pRE = new CVsRegisterEditors;
				if ( m_pRE )
				{
					if ( m_pRE->Attach( (IVsRegisterEditors*)(*ppv) ) )
					{
						*ppv = (void*)m_pRE;
					}
				}
				CDbg::Trace( "<------------->IVsRegisterEditors wrapped: orig=0x%p, new=0x%p\n", m_pRE->m_pRealObject, m_pRE );
			}
		}
		CDbg::Trace( "   IServiceProvider::QueryService(,,[out]*ppv=0x%p ) = %X\n", *ppv, hr );
		return hr;
	}
	return E_INVALIDARG;
}

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
United States United States
For the last 7 years I have developed software in real-time/embedded and MS-Windows environments for military and civil markets. I hold B.Sc. degree in computer engineering. Living in Ontario, Canada, I like hiking and traveling in general. Currently, I'm looking for employment opportunity inside the GTA.

Comments and Discussions