home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Softwarová Záchrana 3
/
Softwarova-zachrana-3.bin
/
pserv.cpl
/
pserv-2.4.exe
/
source
/
pserv2Doc.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-01-05
|
11KB
|
373 lines
// pserv2Doc.cpp : implementation of the CPserv2Doc class
//
#include "stdafx.h"
#include "pserv2.h"
#include "MainFrm.h"
#include "pserv2Doc.h"
#include "pserv2View.h"
#include "CConfiguration.h"
#include "TemplateDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPserv2Doc
IMPLEMENT_DYNCREATE(CPserv2Doc, CDocument)
BEGIN_MESSAGE_MAP(CPserv2Doc, CDocument)
//{{AFX_MSG_MAP(CPserv2Doc)
ON_COMMAND(ID_DISPLAY_EXPORT_ASXML, OnDisplayExportAsXml)
ON_COMMAND(ID_VIEW_SERVICES, OnViewServices)
ON_COMMAND(ID_VIEW_DEVICES, OnViewDevices)
ON_COMMAND(ID_VIEW_SYSTEMEVENTS, OnViewSystemEvents)
ON_COMMAND(ID_VIEW_SECURITYEVENTS, OnViewSecurityEvents)
ON_COMMAND(ID_VIEW_PROCESSLIST, OnViewProcessList)
ON_COMMAND(ID_VIEW_MODULESLIST, OnViewModulesList)
ON_COMMAND(ID_VIEW_APPLICATIONEVENTS, OnViewApplicationEvents)
ON_COMMAND(ID_DISPLAY_EXPORT_TOCLIPBOARD, OnDisplayExportToClipboard)
ON_COMMAND(ID_TEMPLATES_APPLY, OnTemplatesApply)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPserv2Doc construction/destruction
CPserv2Doc::CPserv2Doc()
: m_pEntries( &m_ServiceList ),
m_DocType( DocType_Services )
{
// TODO: add one-time construction code here
m_Providers.AddTail(&m_ServiceList);
m_Providers.AddTail(&m_EventLog);
m_Providers.AddTail(&m_ProcessList);
m_Providers.AddTail(&m_ModulesList);
DoConfigExchange(FALSE);
}
CPserv2Doc::~CPserv2Doc()
{
DoConfigExchange(TRUE);
}
void GetCommandLineTokens(CStringArray& tokens)
{
LPTSTR p = tstrdup(GetCommandLine());
LPCTSTR token = tstrtok( p, _T(" ") );
while( token != NULL )
{
tokens.Add( token );
token = tstrtok( NULL, _T(" ") );
}
free(p);
}
void CPserv2Doc::DoConfigExchange(BOOL bSave)
{
POSITION pos = m_Providers.GetHeadPosition();
while( pos != NULL )
{
CListViewEntries* provider = (CListViewEntries*) m_Providers.GetNext(pos);
ASSERT_VALID(provider);
CRegistry key( HKEY_CURRENT_USER,
FormattedString(REGISTRY_BASE _T("\\%s"), (LPCTSTR) provider->m_strName ),
bSave );
provider->DoConfigExchange(key);
}
}
BOOL CPserv2Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
CStringArray Tokens;
GetCommandLineTokens(Tokens);
CString strMachine;
for( int i = 0, imax = Tokens.GetSize(); i < imax; i++ )
{
CString token(Tokens.GetAt(i));
if( token.Left(9).CompareNoCase(_T("/MACHINE=")) == 0 )
{
strMachine = LPCTSTR(token) + 9;
}
else if( token.CompareNoCase(_T("/DEVICES")) == 0 )
{
m_ServiceList.SetEnumType( SERVICE_DRIVER );
}
else if( token.CompareNoCase(_T("/SYSEVENTS")) == 0 )
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
m_pEntries = &m_EventLog;
m_DocType = DocType_Events;
m_EventLog.SetEnumType( _T("System") );
}
else if( token.CompareNoCase(_T("/SECURITYEVENTS")) == 0 )
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
m_pEntries = &m_EventLog;
m_DocType = DocType_Events;
m_EventLog.SetEnumType( _T("Security") );
}
else if( token.CompareNoCase(_T("/APPEVENTS")) == 0 )
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
m_pEntries = &m_EventLog;
m_DocType = DocType_Events;
m_EventLog.SetEnumType( _T("Application") );
}
else if( token.CompareNoCase(_T("/MODULES")) == 0 )
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_MODULES);
m_pEntries = &m_ModulesList;
m_DocType = DocType_ModulesList;
}
else if( token.CompareNoCase(_T("/PROCESSES")) == 0 )
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_PROCESSES);
m_pEntries = &m_ProcessList;
m_DocType = DocType_ProcessList;
}
}
m_pEntries->ConnectTo(strMachine);
m_pEntries->Refresh();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CPserv2Doc serialization
void CPserv2Doc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CPserv2Doc diagnostics
#ifdef _DEBUG
void CPserv2Doc::AssertValid() const
{
CDocument::AssertValid();
}
void CPserv2Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPserv2Doc commands
extern CPserv2View* theCurrentView;
void CPserv2Doc::IGetExportFilename(LPCTSTR lpszDefExt, CString& refFilename, LPCTSTR lpszFilter, BOOL& success, BOOL bOpenFileDialog)
{
LPCTSTR lpszFileName = refFilename;
WIN32_FIND_DATA fd;
HANDLE hFF = FindFirstFile( refFilename, &fd );
if( hFF == INVALID_HANDLE_VALUE )
lpszFileName = NULL;
else
FindClose(hFF);
CFileDialog box( bOpenFileDialog, // BOOL bOpenFileDialog
lpszDefExt, // LPCTSTR lpszDefExt = NULL
lpszFileName, // LPCTSTR lpszFileName = NULL
OFN_OVERWRITEPROMPT, // DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
lpszFilter, // LPCTSTR lpszFilter = NULL,
NULL // CWnd* pParentWnd = NULL
);
if( box.DoModal() == IDOK )
{
refFilename = box.GetPathName();
success = TRUE;
}
}
BOOL CPserv2Doc::GetExportFilename(LPCTSTR lpszDefExt, CString& refFilename, LPCTSTR lpszFilter, BOOL bOpenFileDialog)
{
BOOL success = FALSE;
__try
{
IGetExportFilename(lpszDefExt, refFilename, lpszFilter, success,bOpenFileDialog);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}
return success;
}
void CPserv2Doc::OnDisplayExportAsXml()
{
if( GetExportFilename(_T("xml"), theConfiguration.m_strExportXmlFilename, _T("XML files (*.xml)|*.xml|All files (*.*)|(*.*)||"), FALSE ) )
{
if( !m_pEntries->ExportAsXml( theConfiguration.m_strExportXmlFilename ) )
{
DisplayErrorMessage(_T("Unable to export %s"), (LPCTSTR) theConfiguration.m_strExportXmlFilename );
}
}
}
void CPserv2Doc::OnViewServices()
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME);
m_pEntries = &m_ServiceList;
m_DocType = DocType_Services;
m_ServiceList.SetEnumType( SERVICE_WIN32 );
m_ServiceList.Refresh();
UpdateAllViews(0);
}
void CPserv2Doc::OnViewDevices()
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME);
m_DocType = DocType_Services;
m_pEntries = &m_ServiceList;
m_ServiceList.SetEnumType( SERVICE_DRIVER );
m_ServiceList.Refresh();
UpdateAllViews(0);
}
void CPserv2Doc::OnViewProcessList()
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_PROCESSES);
m_pEntries = &m_ProcessList;
m_DocType = DocType_ProcessList;
m_ProcessList.Refresh();
UpdateAllViews(0);
}
void CPserv2Doc::OnViewModulesList()
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_MODULES);
m_pEntries = &m_ModulesList;
m_DocType = DocType_ModulesList;
m_ModulesList.Refresh();
UpdateAllViews(0);
}
void CPserv2Doc::OnViewSecurityEvents()
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
m_pEntries = &m_EventLog;
m_DocType = DocType_Events;
m_EventLog.SetEnumType( _T("Security") );
m_EventLog.Refresh();
UpdateAllViews(0);
}
void CPserv2Doc::OnViewSystemEvents()
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
m_pEntries = &m_EventLog;
m_DocType = DocType_Events;
m_EventLog.SetEnumType( _T("System") );
m_EventLog.Refresh();
UpdateAllViews(0);
}
void CPserv2Doc::OnViewApplicationEvents()
{
TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
m_pEntries = &m_EventLog;
m_DocType = DocType_Events;
m_EventLog.SetEnumType( _T("Application") );
m_EventLog.Refresh();
UpdateAllViews(0);
}
void CPserv2Doc::OnDisplayExportToClipboard()
{
CWnd* pWnd = AfxGetMainWnd();
ASSERT_VALID(pWnd);
CMemFile file;
m_pEntries->ExportXmlToFile(&file);
DWORD dwSize = file.GetPosition();
file.Seek(0,CFile::begin);
LPCTSTR lpszString = NULL;
LPCTSTR lpszEndOfString = NULL;
file.GetBufferPtr(0, 0, (void**) &lpszString, (void**) &lpszEndOfString );
if( pWnd->OpenClipboard() )
{
int nstrlen = dwSize;
HGLOBAL hMemory = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, nstrlen+1 );
if( hMemory )
{
LPTSTR lpMemory = (LPTSTR) GlobalLock( hMemory );
tstrncpy( lpMemory, lpszString, nstrlen );
lpMemory[nstrlen] = 0;
GlobalUnlock( hMemory );
SetClipboardData( CF_TEXT, (HANDLE) hMemory );
}
CloseClipboard();
}
}
#include "PXmlReader.h"
void CPserv2Doc::OnTemplatesApply()
{
if( GetExportFilename(_T("xml"), theConfiguration.m_strExportXmlFilename, _T("XML files (*.xml)|*.xml|All files (*.*)|(*.*)||"), TRUE ) )
{
PXmlReader reader;
if( reader.Read( theConfiguration.m_strExportXmlFilename ) )
{
TemplateDialog box( &m_ServiceList, &reader.m_Actions );
if( box.DoModal() == IDOK )
{
SETSTARTTYPEFN method[] = {
&CService::StartAtBoot,
&CService::StartAtSystem,
&CService::StartAutomatically,
&CService::Enable,
&CService::Disable,
};
POSITION pos = box.m_ActionsToPerform.GetHeadPosition();
while( pos )
{
ServiceAction* sa = (ServiceAction*)box.m_ActionsToPerform.GetNext( pos );
if( !(sa->m_pService->*method[sa->m_dwActionToTake])() )
{
DisplayErrorMessage(_T("Unable to set startup type to %s '%s'"),
(LPCTSTR) CService::GetStartAsString(sa->m_dwActionToTake),
(LPCTSTR)sa->m_pService->m_strDisplayName );
}
}
UpdateAllViews(0);
}
}
else
{
DisplayErrorMessage(_T("Unable to read %s"), (LPCTSTR) theConfiguration.m_strExportXmlFilename );
}
}
}