📄 vc6automationhelper.cpp
字号:
/************************************************************************
*
* Resource ID Organiser Add-In for Visual C++
*
* (c) Copyright 2000-2005 by Riverblade Limited (UK). All rights reserved.
*
************************************************************************
*
* Description : CVc6AutomationHelper - Visual Studio COM interface
* wrapper class
*
* Compiler : Microsoft Visual C++ 6.0, Service Pack 3 or later
*
* Target
* Environment : Windows 2000/XP/Vista;
* Visual Studio 5.0 or 6.0
*
* 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/ResOrgAddIn/Vc6AutomationHelper.cpp $
* $Revision: 11 $
* $Date: 22/10/05 17:04 $
* $Author: Anna $
*
* $History: Vc6AutomationHelper.cpp $
*
* ***************** Version 11 *****************
* User: Anna Date: 22/10/05 Time: 17:04
* Updated in $/Projects/AddIns/ResOrg/ResOrgAddIn
* Moved CVc6AutomationHelper from the ResOrgCore module to ResOrgAddIn.
*
* ***************** 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 "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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -