Click here to Skip to main content
15,886,840 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 "DocView.h"
#include "Persist.h"
#include "OleCommandTarget.h"
 
#define MAX_ENTRS	10
void* pPrstDataDoc[MAX_ENTRS]={0,0,0,0,0,0,0,0,0,0};
void* pPrstFileFmt[MAX_ENTRS]={0,0,0,0,0,0,0,0,0,0};
void* pWnds[MAX_ENTRS]={0,0,0,0,0,0,0,0,0,0};
void* pOleTgt[MAX_ENTRS]={0,0,0,0,0,0,0,0,0,0};
COleCommandTargetSpy* pOCT[MAX_ENTRS]={0,0,0,0,0,0,0,0,0,0};

static BOOL IsRegistered( void* pObj, void* pArray[] )
{
	for( int i=0; i < MAX_ENTRS; i++ )
	{
		if ( pObj == pArray[i] )
		{
			return TRUE;
		}
		if ( pArray[i] == NULL )
		{
			pArray[i] = pObj;
			return FALSE;
		}
	}
	int* pPtr = NULL;
	*pPtr = 10; // generate exception if no space left in arrays
	return TRUE;
}
static int FindOleIndex(void* pObj)
{
	for( int i=0; i < MAX_ENTRS; i++ )
	{
		if ( pOleTgt[i] == pObj )
		{
			return i;
		}
		if ( pOleTgt[i] == NULL )
		{
			pOleTgt[i] = pObj;
			return i;
		}
	}
	int* pPtr = NULL;
	*pPtr = 10; // generate exception - the obj must exist in array
	return TRUE;
}

/*void* pIVsPersistDocData = NULL;
void* pIPersistFileFormat=NULL;
COleCommandTargetSpy* pOCT = NULL;*/

//=====================================================================
// Document View interface
//=====================================================================

START_IMPL_SPY(IUnknownDocView,3)

DECL_QI_FILTER( DocVIEW )

STDMETHODIMP DV_QueryInterface(void* pThat, REFIID rid,void** ppObj)
{
	HRESULT hr;

	CALL_METHOD3(0,pThat,rid,ppObj);
	GET_RET_VAL(hr);

	if ( FILTER_QI(hr, (GUID*)&rid, DocVIEW ) )
	{
		CDbg::Trace( "      (DocView) object=%p\n", *ppObj );
	}

	if ( rid == IID_IUnknown )
	{
		return hr;
	}

	if ( rid == IID_IPersistFileFormat )
	{
		if ( !IsRegistered(*ppObj,pPrstFileFmt) )
		{
			SET_SPY(IPersistFileFormat,*ppObj);
			CDbg::Trace( "<------------->IPersistFileFormat subclassed: obj=0x%p in (DocView)\n",*ppObj );
		}
		return S_OK;
	}
	if ( rid == IID_IMaybeWindowPane )
	{
		if ( !IsRegistered(*ppObj,pWnds) )
		{
			SET_SPY(IVsWindowPane,*ppObj);
			CDbg::Trace( "<------------->IVsWindowPane subclassed: obj=0x%p in (DocView)\n", *ppObj );
		}
		return S_OK;
	}
	if ( rid == IID_IVsPersistDocData )
	{
		if ( !IsRegistered(*ppObj,pPrstDataDoc) )
		{
			SET_SPY(IVsPersistDocData,*ppObj);
			CDbg::Trace( "<------------->IVsPersistDocData subclassed: obj=0x%p in (DocView)\n", *ppObj );
		}
		return S_OK;
	}
	if ( rid == IID_IOleCommandTarget )
	{
		int iIndx = FindOleIndex(*ppObj);
		if ( pOCT[iIndx] == NULL )
		{
			pOCT[iIndx] = new COleCommandTargetSpy();
			pOCT[iIndx]->Attach( *ppObj );
			CDbg::Trace( "<------------->IOleCommandTarget wrapped: orig=0x%p, new=0x%p in (View)\n", *ppObj, pOCT[iIndx] );
		}
		*ppObj = pOCT[iIndx];
		return S_OK;
	}
	*ppObj = NULL;
	return E_NOINTERFACE;
}

SPY_STUB_SILENT(1)//,"       DocView::")
SPY_STUB_SILENT(2)//,"       DocView::")

SPY_START_VTBL(3)
	SPY_ENTRY_(DV_QueryInterface,0)
	SPY_ENTRY(1)
	SPY_ENTRY(2)
SPY_END_VTBL()

END_IMPL_SPY()

//=====================================================================
// Document Data interface
//=====================================================================
START_IMPL_SPY(IUnknownDocData,3)

DECL_QI_FILTER(DocDATA)

STDMETHODIMP DD_QueryInterface(void* pThat, REFIID rid,void** ppObj)
{
	HRESULT hr;
	CALL_METHOD3(0,pThat,rid,ppObj);
	GET_RET_VAL(hr);
	
	if ( FILTER_QI(hr, (GUID*)&rid, DocDATA ) )
	{
		CDbg::Trace( "      (DocData) object=%p\n", *ppObj );
	}

	if ( rid == IID_IUnknown )
	{
		return hr;
	}
	if ( rid == IID_IPersistFileFormat )
	{
		if ( !IsRegistered(*ppObj,pPrstFileFmt) )
		{
			SET_SPY(IPersistFileFormat,*ppObj);
			CDbg::Trace( "<------------->IPersistFileFormat subclassed: obj=0x%p in (DocData)\n",*ppObj );
		}
		return S_OK;
	}
	if ( rid == IID_IMaybeWindowPane )
	{
		if ( !IsRegistered(*ppObj,pWnds) )
		{
			SET_SPY(IVsWindowPane,*ppObj);
			CDbg::Trace( "<------------->IVsWindowPane subclassed: obj=0x%p in (DocData)\n", *ppObj );
		}
		return S_OK;
	}
	if ( rid == IID_IVsPersistDocData )
	{
		if ( !IsRegistered(*ppObj,pPrstDataDoc) )
		{
			SET_SPY(IVsPersistDocData,*ppObj);
			CDbg::Trace( "<------------->IVsPersistDocData subclassed: obj=0x%p in (DocData)\n", *ppObj );
		}
		return S_OK;
	}
	if ( rid == IID_IOleCommandTarget )
	{
		int iIndx = FindOleIndex(*ppObj);
		if ( pOCT[iIndx] == NULL )
		{
			pOCT[iIndx] = new COleCommandTargetSpy();
			pOCT[iIndx]->Attach( *ppObj );
			CDbg::Trace( "<------------->IOleCommandTarget wrapped: orig=0x%p, new=0x%p in (DocData)\n", *ppObj, pOCT[iIndx] );
		}
		*ppObj = pOCT[iIndx];
		return S_OK;
	}
	*ppObj = NULL;
	return E_NOINTERFACE;
}

SPY_STUB_SILENT(1)
SPY_STUB_SILENT(2)

SPY_START_VTBL(3)
	SPY_ENTRY_(DD_QueryInterface,0)
	SPY_ENTRY(1)
	SPY_ENTRY(2)
SPY_END_VTBL()

END_IMPL_SPY()

//==========================================================
START_IMPL_SPY(IVsWindowPane,10)

DECL_QI_FILTER(IWindowPane)

//SPY_STUB_(0,"        IVsWindowPane::")
STDMETHODIMP WP_QueryInterface(void* pThat, REFIID rid,void** ppObj)
{
	HRESULT hr;
	CALL_METHOD3(0,pThat,rid,ppObj);
	GET_RET_VAL(hr);
	if ( FILTER_QI(hr, (GUID*)&rid, IWindowPane ) )
	{
		CDbg::Trace( "      (IWindowPane) object=%p\n", *ppObj );
	}

	if ( rid == IID_IUnknown )
	{
		return hr;
	}
	if ( rid == IID_IPersistFileFormat )
	{
		if ( !IsRegistered(*ppObj,pPrstFileFmt) )
		{
			SET_SPY(IPersistFileFormat,*ppObj);
			CDbg::Trace( "<------------->IPersistFileFormat subclassed: obj=0x%p in (Window)\n",*ppObj );
		}
		return S_OK;
	}
	if ( rid == IID_IVsPersistDocData )
	{
		if ( !IsRegistered(*ppObj,pPrstDataDoc) )
		{
			SET_SPY(IVsPersistDocData,*ppObj);
			CDbg::Trace( "<------------->IVsPersistDocData subclassed: obj=0x%p in (Window)\n", *ppObj );
		}
		return S_OK;
	}
	if ( rid == IID_IOleCommandTarget )
	{
		int iIndx = FindOleIndex(*ppObj);
		if ( pOCT[iIndx] == NULL )
		{
			pOCT[iIndx] = new COleCommandTargetSpy();
			pOCT[iIndx]->Attach( *ppObj );
			CDbg::Trace( "<------------->IOleCommandTarget wrapped: orig=0x%p, new=0x%p in (Window)\n", *ppObj, pOCT[iIndx] );
		}
		*ppObj = pOCT[iIndx];
		return S_OK;
	}
	*ppObj = NULL;
	return E_NOINTERFACE;
}


SPY_STUB_SILENT(1)//,"        IVsWindowPane::")
SPY_STUB_SILENT(2)//,"        IVsWindowPane::")
//SPY_STUB_(3,"        IVsWindowPane::")
STDMETHODIMP WP_Stub3( void* pThat, DWORD p1 )
{
	CDbg::Trace("       IVsWindowPane::Stub3(0x%p, p1=0x%X )\n", pThat, p1 );
	HRESULT hr;
	CALL_METHOD2(3,pThat,p1);
	GET_RET_VAL(hr);
	CDbg::Trace("       IVsWindowPane::Stub3(, ) = %x\n", hr );
	return hr;
}

//SPY_STUB_(4,"        IVsWindowPane::")
STDMETHODIMP WP_CreatePaneWindow(void* pThat, HWND hwndParent, int iX, int iY, int iCX, int iCY, HWND* phwnd )
{
	CDbg::Trace("       IVsWindowPane::CreatePaneWindow(0x%p,hpar=0x%p,\n", pThat, hwndParent );
	CDbg::Trace("                                     x=%d, y=%d, cx=%d, cy=%d,[out]phwnd=0x%p )\n", iX, iY, iCX, iCY, phwnd);

	HRESULT hr;
	__asm{ push phwnd }
	__asm{ push iCY }
	__asm{ push iCX }
	CALL_METHOD4(4,pThat,hwndParent,iX,iY);
	GET_RET_VAL(hr);
	CDbg::Trace("       IVsWindowPane::CreatePaneWindow(,,,,,[out]*phwnd=0x%p ) = %x\n", *phwnd, hr);
	return hr;
}

//SPY_STUB_(5,"        IVsWindowPane::")
STDMETHODIMP WP_Stub5(void* pThis, void* p1 )
{
	CDbg::Trace("       IVsWindowPane::Stub5(0x%p, p1=0x%p )-empty=E_NOTIMPL\n", pThis, p1 );
	return E_NOTIMPL;
}

//SPY_STUB_(6,"        IVsWindowPane::")
STDMETHODIMP WP_Close(void* pThat)
{
	CDbg::Trace("       IVsWindowPane::Close(0x%p)\n", pThat );
	HRESULT hr;
	CALL_METHOD1(6,pThat);
	GET_RET_VAL(hr);
	CDbg::Trace("       IVsWindowPane::Close( ) = %x\n", hr );
	return hr;
}
//SPY_STUB_(7,"        IVsWindowPane::")
STDMETHODIMP WP_Stub7(void* pThis, void* p1 )
{
	CDbg::Trace("       IVsWindowPane::Stub7(0x%p, p1=0x%p )-empty=E_NOTIMPL\n", pThis, p1 );
	return E_NOTIMPL;
}
//SPY_STUB_(8,"        IVsWindowPane::")
STDMETHODIMP WP_Stub8(void* pThis, void* p1 )
{
	CDbg::Trace("       IVsWindowPane::Stub8(0x%p, p1=0x%p )-empty=E_NOTIMPL\n", pThis, p1 );
	return E_NOTIMPL;
}

//SPY_STUB_SILENT(9)//,"        IVsWindowPane::")
STDMETHODIMP WP_Stub9(void* pThis, void* p1 )
{
	static int iCnt = 0;
	if ( iCnt == 0 )
	{//print once:
		CDbg::Trace("       IVsWindowPane::Stub9(0x%p, p1=0x%p )-empty=E_NOTIMPL\n", pThis, p1 );
		iCnt = 100;
	}
	return E_NOTIMPL;
}


SPY_START_VTBL(10)
	//SPY_ENTRY(0)
	SPY_ENTRY_(WP_QueryInterface,0)
	SPY_ENTRY(1)
	SPY_ENTRY(2)
	SPY_ENTRY_(WP_Stub3,3)
	SPY_ENTRY_(WP_CreatePaneWindow,4)
	SPY_ENTRY_(WP_Stub5,5)
	SPY_ENTRY_(WP_Close,6)
	SPY_ENTRY_(WP_Stub7,7)
	SPY_ENTRY_(WP_Stub8,8)
	SPY_ENTRY_(WP_Stub9,9)
SPY_END_VTBL()
END_IMPL_SPY()

//==========================================================

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