📄 vc7automationhelper.cpp
字号:
/************************************************************************
*
* Resource ID Organiser Add-In for Visual C++
*
* (c) Copyright 2000-2005 by Riverblade Limited (UK). All rights reserved.
*
************************************************************************
*
* Filename : Vc7AutomationHelper.h
*
* Description : CVc7AutomationHelper - Visual Studio.NET COM interface
* wrapper class
*
* Compiler : Microsoft Visual C++ .NET 2003
*
* Target
* Environment : Windows 2000/XP/Vista;
* Visual Studio 2002, 2003 or 2005
*
* NOTE:
*
* This software is provided "as is". All title and copyrights in and
* to the software, including but not limited to any images, text etc.
* etc. incorporated into it, are the property of Anna-Jayne Metcalfe
* and Riverblade Limited, except where acknowledged otherwise.
*
* Your may freely use this code in your own products, PROVIDED
* this notice is not removed or modified.
*
* Please visit http://www.riverblade.co.uk/products/resorg or email
* support@riverblade.co.uk for latest updates and product support
*
************************************************************************
*
* MODIFICATION HISTORY:
*
* This is a controlled document. See project configuration
* control tool for latest version and full version history.
*
* $Archive: /Projects/AddIns/ResOrg/ResOrgNETAddIn/Vc7AutomationHelper.cpp $
* $Revision: 6 $
* $Date: 22/10/05 16:40 $
* $Author: Anna $
*
* $History: Vc7AutomationHelper.cpp $
*
* ***************** Version 6 *****************
* User: Anna Date: 22/10/05 Time: 16:40
* Updated in $/Projects/AddIns/ResOrg/ResOrgNETAddIn
* Moved CVc6AutomationHelper and CVc7AutomationHelper to the ResOrgAddIn
* and ResOrgNETAddIn projects respectively.
*
* ***************** Version 4 *****************
* User: Anna Date: 13/10/05 Time: 13:07
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Modified #imports for DTE and MSO to import smart pointer interfaces as
* well as raw ones. Also included lint directives so PC-Lint can identify
* the .tlh and .tli files for interfaces imported by LIBID.
*
* ***************** 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: $
*
************************************************************************/
#include "StdAfx.h"
#if _MSC_VER >= 1300 // Visual C++.NET only
#include <comdef.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->raw_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->raw_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->raw_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->raw_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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -