Click here to Skip to main content
14,209,092 members
Click here to Skip to main content
Posted 20 Dec 2003

Stats

59.6K views
1.1K downloads
39 bookmarked

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

,
Rate this:
4.72 (20 votes)
Please Sign up or sign in to vote.
4.72 (20 votes)
20 Dec 2003        
Create a fully integrated document window inside the Visual Studio IDE.
#include "StdAfx.h"
#include "vsdllif.h"
#include "ClassFactory.h"

#define FAILED_TO_LOAD	((HMODULE)0x01)


CVsDllIf::CVsDllIf(void)
{
	m_hDll = NULL;
	m_bInitFailed = FALSE;
	m_pfnDllCanUnloadNow = NULL;
	m_pfnDllGetClassObject = NULL;
	m_pfnDllRegisterServer = NULL;
	m_pfnDllUnregisterServer = NULL;
	m_pfnVSDllRegisterServer = NULL;
	m_pfnVSDllUnregisterServer = NULL;
}


CVsDllIf::~CVsDllIf(void)
{
	Uninitialize();
}


HMODULE CVsDllIf::LoadTargetDll()
{
	WCHAR wszModulePath[_MAX_PATH];
	DWORD dwChrCnt = ::GetModuleFileNameW( m_hDllThis, wszModulePath, _MAX_PATH );
	if ( dwChrCnt > 0 )
	{
		for( int i = dwChrCnt-1; i >=0; i -- ) // find last '\' in string
		{										// strrchr() can be used instead of
			if ( wszModulePath[i] == L'\\' )	// the loop
			{
				break;
			}
		}
		wcscpy( &(wszModulePath[i+1]), L"orig-bined.dll" );
		return ::LoadLibraryW( wszModulePath );	
	}
	return NULL;
}

BOOL CVsDllIf::Initialize(void)
{
	if ( FAILED_TO_LOAD == m_hDll )
	{
		return FALSE;
	}
	
	if ( NULL != m_hDll )
	{
		return TRUE;
	}
	
	m_hDll = LoadTargetDll();

	if ( m_hDll )
	{
		m_pfnDllCanUnloadNow = (TFnDllCanUnloadNow)GetProcAddress( m_hDll, "DllCanUnloadNow" );
		m_pfnDllGetClassObject = (TFnDllGetClassObject)GetProcAddress( m_hDll, "DllGetClassObject" );
		m_pfnDllRegisterServer = (TFnDllRegisterServer)GetProcAddress( m_hDll, "DllRegisterServer" );
		m_pfnDllUnregisterServer = (TFnDllUnregisterServer)GetProcAddress( m_hDll, "DllUnregisterServer" );
		m_pfnVSDllRegisterServer = (TFnVSDllRegisterServer)GetProcAddress( m_hDll, "VSDllRegisterServer" );
		m_pfnVSDllUnregisterServer = (TFnVSDllUnregisterServer)GetProcAddress( m_hDll, "VSDllUnregisterServer" );
		if ( m_pfnDllCanUnloadNow && m_pfnDllGetClassObject && m_pfnDllRegisterServer &&
			m_pfnDllUnregisterServer && m_pfnVSDllRegisterServer &&  m_pfnVSDllUnregisterServer )
		{
			CDbg::Trace( "Target dll loaded\n" );
			return TRUE;
		}
		Uninitialize();
		m_hDll = FAILED_TO_LOAD;
	}
	CDbg::Trace( "Failed to load original DLL\n" );
	return FALSE;
}

void CVsDllIf::Uninitialize(void)
{
	if ( m_hDll )
	{
		FreeLibrary(m_hDll);
		m_hDll = NULL;
		m_pfnDllCanUnloadNow = NULL;
		m_pfnDllGetClassObject = NULL;
		m_pfnDllRegisterServer = NULL;
		m_pfnDllUnregisterServer = NULL;
		m_pfnVSDllRegisterServer = NULL;
		m_pfnVSDllUnregisterServer = NULL;
	}
}

HRESULT CVsDllIf::CanUnload(void)
{
	if ( !Initialize() )
	{
		return E_INVALIDARG;
	}
	CDbg::Trace( "CVsDllIf::CanUnload( )\n" );
	HRESULT hr = (*m_pfnDllCanUnloadNow)();
	CDbg::Trace( "CVsDllIf::CanUnload( ) = %X\n", hr );
	return hr;
}

HRESULT CVsDllIf::GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
	if ( !Initialize() )
	{
		return E_INVALIDARG;
	}
	CDbg::Trace( "CVsDllIf::GetClassObject( rclsid=" );
	CDbg::TraceGuid( rclsid );
	CDbg::TraceC( ", riid=" );
	CDbg::TraceGuid( riid );
	CDbg::TraceC( ", [out]ppv )\n" );
	HRESULT hr = (*m_pfnDllGetClassObject)( rclsid, riid, ppv);
	CDbg::Trace( "CVsDllIf::GetClassObject(,,[out](CClassFactory)ppv=0x%p) = %X\n", *ppv, hr );
	if ( riid == IID_IClassFactory )
	{
		//SET_SPY(IClassFactory, *ppv );
		CClassFactory* pCF = new CClassFactory();
		pCF->Attach( (IClassFactory*)*ppv );
		*ppv = pCF;
		CDbg::Trace( "<-------------> IClassFactory subclassed: obj=0x%p\n", *ppv );
	}
	return hr;
}

HRESULT CVsDllIf::RegisterServer(void)
{
	if ( !Initialize() )
	{
		return E_INVALIDARG;
	}
	CDbg::Trace( "CVsDllIf::RegisterServer( )\n" );
	HRESULT hr = (*m_pfnDllRegisterServer)();
	CDbg::Trace( "CVsDllIf::RegisterServer( ) = %X\n", hr );
	return hr;
}

HRESULT CVsDllIf::UnregisterServer(void)
{
	if ( !Initialize() )
	{
		return E_INVALIDARG;
	}
	CDbg::Trace( "CVsDllIf::UnregisterServer( )\n" );
	HRESULT hr = (*m_pfnDllUnregisterServer)();
	CDbg::Trace( "CVsDllIf::UnregisterServer = %X\n", hr );
	return hr;

}

HRESULT CVsDllIf::VSRegisterServer(void)
{
	if ( !Initialize() )
	{
		return E_INVALIDARG;
	}
	CDbg::Trace("CVsDllIf::VSRegisterServer( )\n" );
	HRESULT hr = (*m_pfnVSDllRegisterServer)();
	CDbg::Trace( "CVsDllIf::VSRegisterServer( ) = %X\n", hr );
	return hr;

}

HRESULT CVsDllIf::VSUnregisterServer(void)
{
	if ( !Initialize() )
	{
		return E_INVALIDARG;
	}
	CDbg::Trace("CVsDllIf::VSUnregisterServer()\n");
	HRESULT hr = (*m_pfnVSDllUnregisterServer)();
	CDbg::Trace( "CVsDllIf::VSUnregisterServer() = %X\n", hr );
	return 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

Share

About the Author

Michael Sheinin
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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190617.3 | Last Updated 21 Dec 2003
Article Copyright 2003 by Michael Sheinin
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid