Click here to Skip to main content
15,861,168 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 527.5K   12.1K   201  
An application/add-in to organise and renumber resource symbol IDs
/************************************************************************
 *
 *                 Resource ID Organiser Core Library
 *
 * (c) Copyright 2000-2003 by Anna-Jayne Metcalfe (resorg@annasplace.me.uk)
 *                         All rights reserved.
 *
 ************************************************************************
 *                                                                       
 *  Filename    : Vc6AutomationHelper.cpp
 *
 *  Description : CVc6AutomationHelper - Visual Studio COM interface wrapper class
 *
 *  Compiler    : Microsoft Visual C++ 6.0, Service Pack 3 or later
 *                                                                       
 *  Target                                                               
 *  Environment : Windows 98/NT/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/Vc6AutomationHelper.cpp $
 *   $Revision: 9 $
 *       $Date: 15/04/03 20:32 $
 *     $Author: Anna $
 *
 *    $History: Vc6AutomationHelper.cpp $
 * 
 * *****************  Version 9  *****************
 * User: Anna         Date: 15/04/03   Time: 20:32
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * 1.  Removed unnecessary file guards (#pragma once works well enough)
 * 2.  Updated file banners
 * 
 * *****************  Version 8  *****************
 * User: Anna         Date: 25/11/02   Time: 15:20
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Changed website address in banner
 * 
 * *****************  Version 7  *****************
 * User: Anna         Date: 22/10/02   Time: 13:24
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Changed name/mail address (at last!)
 * 
 * *****************  Version 6  *****************
 * User: Andy         Date: 2/08/02    Time: 17:01
 * Updated in $/Projects/AddIns/ResOrg/ResOrgCore
 * Renamed class from CWorkspaceHelper to CVc6AutomationHelper, and moved
 * it to the ResOrgCore module
 * 
 * *****************  Version 4  *****************
 * User: Andy         Date: 1/24/02    Time: 10:17p
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Comment changes only
 * 
 * *****************  Version 3  *****************
 * User: Andy         Date: 2/03/01    Time: 13:14
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * Removed CVc6AutomationHelper::GetSymbolFileName() [now a global function in
 * ResOrgUtils]
 * 
 * *****************  Version 2  *****************
 * User: Andy         Date: 29/11/00   Time: 18:37
 * Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
 * 1.  Moved Office2KDlg code to its own DLL
 * 2.  Added file banners
 *
 * $Nokeywords: $
 *
 ************************************************************************/


#include "StdAfx.h"
#include <comdef.h>
#include "ProjectFileBuffer.h"

#include "Vc6AutomationHelper.h"


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


/////////////////////////////////////////////////////////////////////////////
// CVc6AutomationHelper

CVc6AutomationHelper::CVc6AutomationHelper(IApplication* pApplication /*= NULL*/)
{
	m_pApplication		= NULL;

	SetApplication(pApplication);
}


CVc6AutomationHelper::~CVc6AutomationHelper(void)
{
	if (NULL != m_pApplication)
	{
		SetApplication(NULL);
	}
}


/////////////////////////////////////////////////////////////////////////////
// CVc6AutomationHelper operations

BOOL CVc6AutomationHelper::SetApplication(IApplication* pApplication)
{
	if (NULL != m_pApplication)
	{
		m_pApplication->Release();

		m_pApplication = NULL;
	}

	if (pApplication != NULL)
	{
		m_pApplication = pApplication;

		m_pApplication->AddRef();
	}
	return (m_pApplication != NULL);
}


/////////////////////////////////////////////////////////////////////////////
// CVc6AutomationHelper Project operations

int CVc6AutomationHelper::GetProjectCount(void) const
{
	long nProjects = 0;

	IProjects* pProjects = NULL;

	if ( SUCCEEDED( m_pApplication->get_Projects( (LPDISPATCH*)&pProjects) ) )
	{
			
		pProjects->get_Count(&nProjects);

		pProjects->Release();
	}
	return (int)nProjects;
}


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

	CProjectArray arrayProjects;
	GetProjects(arrayProjects);

	for (int n = 0; n < arrayProjects.GetSize(); n++)
	{
		IGenericProject* pProject = arrayProjects[n];
		if (NULL != pProject)
		{
			rarrayNames.Add( GetFullName(pProject) );

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


int CVc6AutomationHelper::GetProjects(CProjectArray& rarrayProjects) const
{
	IProjects* pProjects = NULL;

	if ( SUCCEEDED( m_pApplication->get_Projects( (LPDISPATCH*)&pProjects) ) )
	{
		int nProjects = GetProjectCount();
			
		for (int n = 1; n <= nProjects; n++)	// This bit me the first time. The index is 1 based. BLOODY VB PROGRAMMERS!
		{
			IGenericProject* pProject = NULL;
			_variant_t Index( (long)n);

			pProjects->Item(Index, &pProject);

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


IGenericProject* CVc6AutomationHelper::GetProject(const CString& sFullName) const
{
	USES_CONVERSION;

	IProjects* pProjects		= NULL;
	IGenericProject* pProject	= NULL;

	if ( SUCCEEDED( m_pApplication->get_Projects( (LPDISPATCH*)&pProjects) ) )
	{
		CComBSTR bsFullName = T2OLE(sFullName);

		_variant_t Index(bsFullName);

		pProjects->Item(Index, &pProject);

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


IGenericProject* CVc6AutomationHelper::GetActiveProject(void) const
{
	IGenericProject* pActiveProject = NULL;

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

	return pActiveProject;
}


BOOL CVc6AutomationHelper::SetActiveProject(IGenericProject* pActiveProject)
{
	BOOL bResult = FALSE;

	IProjects* pProjects			= NULL;

	if ( SUCCEEDED( m_pApplication->get_Projects( (LPDISPATCH*)&pProjects) ) )
	{
		if ( SUCCEEDED( m_pApplication->put_ActiveProject( (LPDISPATCH)pActiveProject) ) )
		{
			bResult = TRUE;
		}
	}
	return bResult;
}


BOOL CVc6AutomationHelper::LoadProject(IGenericProject* pProject)
{
	UNREFERENCED_PARAMETER(pProject);

	BOOL bResult = FALSE;

	return bResult;
}


BOOL CVc6AutomationHelper::UnloadProject(IGenericProject* pProject)
{
	UNREFERENCED_PARAMETER(pProject);

	BOOL bResult = FALSE;

	return bResult;
}



CString CVc6AutomationHelper::GetName(IGenericProject* 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 CVc6AutomationHelper::GetFullName(IGenericProject* 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;
}

/////////////////////////////////////////////////////////////////////////////
// CVc6AutomationHelper Document operations

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

	IDocuments* pDocuments = NULL;

	if ( SUCCEEDED( m_pApplication->get_Documents( (LPDISPATCH*)&pDocuments) ) )
	{
			
		pDocuments->get_Count(&nDocuments);

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


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

	CDocumentArray arrayDocuments;
	GetDocuments(arrayDocuments);

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

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


int CVc6AutomationHelper::GetDocuments(CDocumentArray& rarrayDocuments) const
{
	USES_CONVERSION;

	rarrayDocuments.RemoveAll();

	IDocuments* pDocuments = NULL;

	if ( SUCCEEDED( m_pApplication->get_Documents( (LPDISPATCH*)&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!
		{
			IGenericDocument* pDocument = NULL;
			_variant_t Index( (long)n);

			pDocuments->Item(Index, (LPDISPATCH*)&pDocument);

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


IGenericDocument* CVc6AutomationHelper::GetDocument(const CString& sFullName) const
{
	USES_CONVERSION;

	IDocuments* pDocuments		= NULL;
	IGenericDocument* pDocument = NULL;

	if ( SUCCEEDED( m_pApplication->get_Documents( (LPDISPATCH*)&pDocuments) ) )
	{
		CComBSTR bsFullName = T2OLE(sFullName);

		_variant_t Index(bsFullName);

		pDocuments->Item(Index, (LPDISPATCH*)&pDocument);

		pDocuments->Release();
	}
	return pDocument;
}


CString CVc6AutomationHelper::GetName(IGenericDocument* 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 CVc6AutomationHelper::GetFullName(IGenericDocument* 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 CVc6AutomationHelper::CloseDocument(const CString& sFileName)
{
	BOOL bResult = FALSE;

	IGenericDocument* pDocument = GetDocument(sFileName);
	if (NULL != pDocument)
	{
		// RC file is open - close it (save first if need be)
		DsSaveStatus Saved;
		_variant_t SaveChanges( (long)dsSaveChangesYes);

		if ( SUCCEEDED( pDocument->Close(SaveChanges, &Saved) ) )
		{
			bResult = TRUE;

			pDocument->Release();
		}
	}
	return bResult;
}


/////////////////////////////////////////////////////////////////////////////
// CVc6AutomationHelper Resource File operations

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

	CString sResourceFile;

	IGenericProject* pProject = GetProject(sProject);

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

		CProjectFileBuffer prj;
		if (!sFullName.IsEmpty())
		{
			// Parse the DSP 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 CVc6AutomationHelper::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;
}


/////////////////////////////////////////////////////////////////////////////
// CVc6AutomationHelper implementation

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