Click here to Skip to main content
15,867,594 members
Articles / Desktop Programming / MFC

Resource ID Organiser Add-In for Visual C++ 5.0/6.0/.NET

Rate me:
Please Sign up or sign in to vote.
4.98/5 (71 votes)
10 Jan 2005CPOL25 min read 528.2K   12.1K   201  
An application/add-in to organise and renumber resource symbol IDs
/************************************************************************
 *
 *                 Resource ID Organiser Core Library
 *
 * (c) Copyright 2000-2002 by Anna-Jayne Metcalfe (resorg@annasplace.me.uk)
 *                         All rights reserved.
 *
 ************************************************************************
 *                                                                       
 *  Filename    : Vc7AutomationHelper.cpp
 *
 *  Description : CVc7AutomationHelper - Visual Studio.NET COM interface
 *                wrapper class
 *
 *  Compiler    : Microsoft Visual C++ 7.0
 *                                                                       
 *  Target                                                               
 *  Environment : Windows 2000/XP
 *
 *  NOTE:
 *
 *    This software is provided "as is" free for personal use. All
 *    title and copyrights in and to the software, including but not
 *    limited to any images, text, etc. incorporated into it, are
 *    owned by Anna-Jayne Metcalfe, except where acknowledged otherwise.
 *
 *    Your may freely to use this code in your own products, PROVIDED
 *    this notice is not removed or modified.
 *
 *
 *    Visit http://www.annasplace.me.uk/resorg for latest updates
 *
 ************************************************************************
 *
 *   MODIFICATION HISTORY:
 *
 *           This is a controlled document. See project configuration
 *           control tool for latest version and full version history.
 *
 *    $Archive: /Projects/AddIns/ResOrg/ResOrgCore/Vc7AutomationHelper.cpp $
 *   $Revision: 3 $
 *       $Date: 25/11/02 15:20 $
 *     $Author: Anna $
 *
 *    $History: Vc7AutomationHelper.cpp $
 * 
 * *****************  Version 3  *****************
 * User: Anna         Date: 25/11/02   Time: 15:20
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Changed website address in banner
 * 
 * *****************  Version 2  *****************
 * User: Anna         Date: 22/10/02   Time: 13:24
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Changed name/mail address (at last!)
 * 
 * *****************  Version 1  *****************
 * User: Andy         Date: 1/08/02    Time: 16:30
 * Created in $/Projects/AddIns/ResOrg/ResOrgCore
 * 
 * *****************  Version 2  *****************
 * User: Andy         Date: 10/06/02   Time: 17:16
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddInVc7
 * Got it working with VC.NET !!
 * 
 * *****************  Version 1  *****************
 * User: Andy         Date: 7/06/02    Time: 22:31
 * Created in $/Projects/AddIns/ResOrg/ResOrgAddInVc7
 *
 * $Nokeywords: $
 *
 ************************************************************************/

// Vc7AutomationHelper.cpp : implementation file
//

#include "StdAfx.h"

#if _MSC_VER >= 1300		// Visual C++.NET only

#include <comdef.h>

#include "ProjectFileBuffer.h"
#include "Vc7AutomationHelper.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


using namespace EnvDTE;

/////////////////////////////////////////////////////////////////////////////
// CVc7AutomationHelper

CVc7AutomationHelper::CVc7AutomationHelper(void)
{
}


CVc7AutomationHelper::~CVc7AutomationHelper(void)
{
	ASSERT(m_pDTE == NULL);
}


/////////////////////////////////////////////////////////////////////////////
// CVc7AutomationHelper operations

BOOL CVc7AutomationHelper::SetDTE(CComPtr<EnvDTE::_DTE> pDTE)
{
	if (m_pDTE != NULL)
	{
		m_pDTE = NULL;
	}

	if (pDTE != NULL)
	{
		m_pDTE = pDTE;
	}
	return (m_pDTE != NULL);
}


/////////////////////////////////////////////////////////////////////////////
// CVc7AutomationHelper Project operations

int CVc7AutomationHelper::GetProjectCount(void) const
{
	long lProjects = 0;

	_Solution* pSolution = NULL;

	if ( SUCCEEDED( m_pDTE->get_Solution(&pSolution) ) )
	{
		Projects* pProjects = NULL;

		if ( SUCCEEDED( pSolution->get_Projects(&pProjects) ) )
		{
			pProjects->get_Count(&lProjects);

			pProjects->Release();
		}
		pSolution->Release();
	}

	return (int)lProjects;
}


int CVc7AutomationHelper::GetProjectFullNames(CStringArray& rarrayNames) const
{
	rarrayNames.RemoveAll();

	CVc7ProjectArray arrayProjects;
	GetProjects(arrayProjects);

	for (int n = 0; n < arrayProjects.GetSize(); n++)
	{
		Project* pProject = arrayProjects[n];

		if (NULL != pProject)
		{
			rarrayNames.Add( GetFullName(pProject) );

			pProject->Release();
		}
	}
	return (int)rarrayNames.GetSize();
}


int CVc7AutomationHelper::GetProjectNames(CStringArray& rarrayNames) const
{
	rarrayNames.RemoveAll();

	CVc7ProjectArray arrayProjects;
	GetProjects(arrayProjects);

	for (int n = 0; n < arrayProjects.GetSize(); n++)
	{
		Project* pProject = arrayProjects[n];

		if (NULL != pProject)
		{
			rarrayNames.Add( GetName(pProject) );

			pProject->Release();
		}
	}
	return (int)rarrayNames.GetSize();
}


CString CVc7AutomationHelper::GetSolutionPathName(void) const
{
	USES_CONVERSION;

	CString sPathName;

	_Solution* pSolution = NULL;

	if ( SUCCEEDED( m_pDTE->get_Solution(&pSolution) ) )
	{
		CComBSTR bsFullName;
		if ( SUCCEEDED( pSolution->get_FullName(&bsFullName.m_str ) ) )
		{
			sPathName = OLE2T(bsFullName.m_str);
		}
		pSolution->Release();
	}

	return sPathName;
}


int CVc7AutomationHelper::GetProjects(CVc7ProjectArray& rarrayProjects) const
{
	_Solution* pSolution = NULL;

	if ( SUCCEEDED( m_pDTE->get_Solution(&pSolution) ) )
	{
		Projects* pProjects = NULL;

		if ( SUCCEEDED( pSolution->get_Projects(&pProjects) ) )
		{
			long lProjects = 0;
			pProjects->get_Count(&lProjects);

			for (int n = 1; n <= (int)lProjects; n++)	// This bit me the first time. The index is 1 based. BLOODY VB PROGRAMMERS!
			{
				Project* pProject = NULL;
				_variant_t Index( (long)n);

				pProjects->Item(Index, &pProject);

				if (NULL != pProject)
				{
					rarrayProjects.Add(pProject);
				}
			}
			pProjects->Release();
		}
		pSolution->Release();
	}

	return (int)rarrayProjects.GetSize();
}


Project* CVc7AutomationHelper::GetProject(const CString& sName) const
{
	USES_CONVERSION;

	_Solution* pSolution	= NULL;
	Projects* pProjects		= NULL;
	Project* pProject		= NULL;

	if ( SUCCEEDED( m_pDTE->get_Solution(&pSolution) ) )
	{
		if ( SUCCEEDED( pSolution->get_Projects(&pProjects) ) )
		{
			long lProjects = 0;
			pProjects->get_Count(&lProjects);

			for (int n = 1; n <= (int)lProjects; n++)	// This bit me the first time. The index is 1 based. BLOODY VB PROGRAMMERS!
			{
				_variant_t Index( (long)n);
				pProjects->Item(Index, &pProject);

				if (NULL != pProject)
				{
					if (0 == sName.CompareNoCase( GetName(pProject) ) )
					{
						break;
					}
					else
					{
						pProject->Release();

						pProject = NULL;
					}
				}
			}

			pProjects->Release();
		}
		pSolution->Release();
	}
	return pProject;
}

/*
Project* CVc7AutomationHelper::GetActiveProject(void) const
{
	Project* pActiveProject = NULL;

	m_pDTE->get_ActiveProject( (LPDISPATCH*)&pActiveProject);

	return pActiveProject;
}


BOOL CVc7AutomationHelper::SetActiveProject(Project* pActiveProject)
{
	BOOL bResult = FALSE;

	_Solution* pSolution	= NULL;
	Projects* pProjects		= NULL;

	if ( SUCCEEDED( m_pDTE->get_Solution( &pSolution) ) )
	{
		if ( SUCCEEDED( pSolution->get_Projects( (LPDISPATCH*)&pProjects) ) )
		{
			if ( SUCCEEDED( m_pDTE->put_ActiveProject( (LPDISPATCH)pActiveProject) ) )
			{
				bResult = TRUE;
			}
			pProjects->Release();
		}
		pSolution->Release();
	}

	return bResult;
}
*/

BOOL CVc7AutomationHelper::LoadProject(Project* pProject)
{
	UNREFERENCED_PARAMETER(pProject);

	BOOL bResult = FALSE;

	return bResult;
}


BOOL CVc7AutomationHelper::UnloadProject(Project* pProject)
{
	UNREFERENCED_PARAMETER(pProject);

	BOOL bResult = FALSE;

	return bResult;
}



CString CVc7AutomationHelper::GetName(Project* pProject)
{
	USES_CONVERSION;

	CString sName;
	if (NULL != pProject)
	{
		CComBSTR bsName;
		if ( SUCCEEDED( pProject->get_Name(&bsName.m_str ) ) )
		{
			sName = OLE2T(bsName.m_str);
		}
	}
	return sName;
}


CString CVc7AutomationHelper::GetFullName(Project* pProject)
{
	USES_CONVERSION;

	CString sFullName;
	if (NULL != pProject)
	{
		CComBSTR bsFullName;
		if ( SUCCEEDED( pProject->get_FullName(&bsFullName.m_str ) ) )
		{
			sFullName = OLE2T(bsFullName.m_str);
		}
	}
	return sFullName;
}

/////////////////////////////////////////////////////////////////////////////
// CVc7AutomationHelper Document operations

int CVc7AutomationHelper::GetDocumentCount(void) const
{
	long nDocuments = 0;

	Documents* pDocuments = NULL;

	if ( SUCCEEDED( m_pDTE->get_Documents(&pDocuments) ) )
	{
			
		pDocuments->get_Count(&nDocuments);

		pDocuments->Release();
	}
	return (int)nDocuments;
}


int CVc7AutomationHelper::GetDocumentNames(CStringArray& rarrayNames) const
{
	rarrayNames.RemoveAll();

	CVc7DocumentArray arrayDocuments;
	GetDocuments(arrayDocuments);

	for (int n = 0; n < arrayDocuments.GetSize(); n++)
	{
		Document* pDocument = arrayDocuments[n];
		if (NULL != pDocument)
		{
			rarrayNames.Add( GetFullName(pDocument) );

			pDocument->Release();
		}
	}
	return (int)rarrayNames.GetSize();
}


int CVc7AutomationHelper::GetDocuments(CVc7DocumentArray& rarrayDocuments) const
{
	USES_CONVERSION;

	rarrayDocuments.RemoveAll();

	Documents* pDocuments = NULL;

	if ( SUCCEEDED( m_pDTE->get_Documents(&pDocuments) ) )
	{
		int nDocuments = GetDocumentCount();
			
		for (int n = 1; n <= nDocuments; n++)	// This bit me the first time. The index is 1 based. BLOODY VB PROGRAMMERS!
		{
			Document* pDocument = NULL;
			_variant_t Index( (long)n);

			pDocuments->Item(Index, &pDocument);

			if (NULL != pDocument)
			{
				rarrayDocuments.Add(pDocument);
			}
		}
		pDocuments->Release();
	}
	return (int)rarrayDocuments.GetSize();
}


Document* CVc7AutomationHelper::GetDocument(const CString& sFullName) const
{
	USES_CONVERSION;

	Documents* pDocuments	= NULL;
	Document* pDocument		= NULL;

	if ( SUCCEEDED( m_pDTE->get_Documents(&pDocuments) ) )
	{
		long lDocuments = 0;
		pDocuments->get_Count(&lDocuments);

		for (int n = 1; n <= (int)lDocuments; n++)	// This bit me the first time. The index is 1 based. BLOODY VB PROGRAMMERS!
		{
			_variant_t Index( (long)n);
			pDocuments->Item(Index, &pDocument);

			if (NULL != pDocument)
			{
				if (0 == sFullName.CompareNoCase( GetFullName(pDocument) ) )
				{
					break;
				}
				else
				{
					pDocument->Release();
				}
			}
		}
		pDocuments->Release();
	}
	return pDocument;
}


CString CVc7AutomationHelper::GetName(Document* pDocument)
{
	USES_CONVERSION;

	CString sName;
	if (NULL != pDocument)
	{
		CComBSTR bsName;
		if ( SUCCEEDED( pDocument->get_Name(&bsName.m_str ) ) )
		{
			sName = OLE2T(bsName.m_str);
		}
	}
	return sName;
}


CString CVc7AutomationHelper::GetFullName(Document* pDocument)
{
	USES_CONVERSION;

	CString sFullName;
	if (NULL != pDocument)
	{
		CComBSTR bsFullName;
		if ( SUCCEEDED( pDocument->get_FullName(&bsFullName.m_str ) ) )
		{
			sFullName = OLE2T(bsFullName.m_str);
		}
	}
	return sFullName;
}


BOOL CVc7AutomationHelper::CloseDocument(const CString& sFileName)
{
	BOOL bResult = FALSE;

	Document* pDocument = GetDocument(sFileName);

	if (NULL != pDocument)
	{
		// RC file is open - close it (save first if need be)
		VARIANT_BOOL bvSaved = VARIANT_TRUE;
		pDocument->get_Saved(&bvSaved);

		if (VARIANT_TRUE == bvSaved)
		{
			bResult = TRUE;
		}
		else
		{
			if ( !SUCCEEDED( pDocument->Close(vsSaveChangesYes) ) )
			{
				bResult = FALSE;
			}
		}
		pDocument->Release();
	}
	return bResult;
}


/////////////////////////////////////////////////////////////////////////////
// CVc7AutomationHelper Resource File operations

CString CVc7AutomationHelper::GetResourceFileName(const CString& sProject) const
{
	USES_CONVERSION;

	CString sResourceFile;

	Project* pProject = GetProject(sProject);

	if (NULL != pProject)
	{
		CString sFullName = GetFullName(pProject);

		CProjectFileBuffer prj;
		if (!sFullName.IsEmpty())
		{
			// Parse the project file to get the RC file
			CStdioFile f;
			CFileException e;

			if (f.Open(	sFullName,
						CFile::modeRead |
						CFile::shareDenyWrite,
						&e) )
			{
				// File opened OK
				//
				// Now create an archive, and an empty text buffer
				CArchive ar(&f, CArchive::load);

				prj.Serialize(ar);

				try
				{
					ar.Flush();
					ar.Close();
					f.Flush();
					f.Close();
				}
				catch (CException* e)
				{
					TCHAR szCause[255];
					e->GetErrorMessage(szCause, 255);
					CString sMsg;
					sMsg.Format( _T("Error closing project file\n\nDetails: %s\n"), szCause);

					TRACE1("%s\n", sMsg);
					AfxMessageBox(sMsg, MB_OK | MB_ICONSTOP);

					e->Delete();
				}
			}
			CStringArray arrayResourceFiles;
			prj.GetFiles(arrayResourceFiles, _T(".rc"));
			if (arrayResourceFiles.GetSize() > 0)
			{
				sResourceFile = arrayResourceFiles[0];
			}
		}
		pProject->Release();
	}
	return sResourceFile;
}


// Closes RC file in the IDE to avoid fuck-ups when symbols are renumbered
BOOL CVc7AutomationHelper::CloseResourceFile(const CString& sResourceFileName)
{
	BOOL bResult = CloseDocument(sResourceFileName);
	if (bResult)
	{
		// TO DO: Delete the .aps file as well
		bResult = TRUE;
	}

	// Try to delete the .aps file anyway
	CNGSplitPath split(sResourceFileName);

	CString sApsFileName =	split.GetDrive() + 
							split.GetDirectory() +
							split.GetFileName() +
							_T(".aps");


	if (::FileExists(sApsFileName) )
	{
		if (!::DeleteFile(sApsFileName))
		{
			DWORD dwError = GetLastError();

			TRACE2("Failed to delete %s: code = %u", sApsFileName, dwError);
		}
	}
	return bResult;
}

/////////////////////////////////////////////////////////////////////////////
// CVc7AutomationHelper implementation

#endif	//#if _MSC_VER >= 1300

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Riverblade Limited
United Kingdom United Kingdom
I haven't always written software for a living. When I graduated from Surrey University in 1989, it was with an Electronic Engineering degree, but unfortunately that never really gave me the opportunity to do anything particularly interesting (with the possible exception of designing Darth Vader's Codpiece * for the UK Army in 1990).
    * Also known as the Standard Army Bootswitch. But that's another story...
Since the opportunity arose to lead a software team developing C++ software for Avionic Test Systems in 1996, I've not looked back. More recently I've been involved in the development of subsea acoustic navigation systems, digital TV broadcast systems, port security/tracking systems, and most recently software development tools with my own company, Riverblade Ltd.

One of my personal specialities is IDE plug-in development. ResOrg was my first attempt at a plug-in, but my day to day work is with Visual Lint, an interactive code analysis tool environment with works within the Visual Studio and Eclipse IDEs or on build servers.

I love lots of things, but particularly music, photography and anything connected with history or engineering. I despise ignorant, intolerant and obstructive people - and it shows...I can be a bolshy cow if you wind me up the wrong way...Laugh | :laugh:

I'm currently based 15 minutes walk from the beach in Bournemouth on the south coast of England. Since I moved here I've grown to love the place - even if it is full of grockles in Summer!

Comments and Discussions