// MainDlg.cpp : implementation file
//
// RegSR.h : main header file for the REGSR application
// Author: A.YEZZA
// Date: August 2004
// Subject: Save/Restore registry keys in very safe mode
// Comment: Use with your own risk
// Licence: This code and application are free. The only
// condition is preserving author name
///////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RegSR.h"
#include "MainDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CRegSRApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CMainDlg dialog
CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMainDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMainDlg)
m_Root = -1;
m_ConfFile = _T("");
m_InOutFile = _T("");
m_KeyPath = _T("");
m_Result = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMainDlg)
DDX_CBIndex(pDX, IDC_COMBO_ROOT, m_Root);
DDX_CBString(pDX, IDC_COMBO_CONF_FILE, m_ConfFile);
DDV_MaxChars(pDX, m_ConfFile, 260);
DDX_CBString(pDX, IDC_COMBO_IN_OUT_FILE, m_InOutFile);
DDV_MaxChars(pDX, m_InOutFile, 260);
DDX_CBString(pDX, IDC_COMBO_KEY_PATH, m_KeyPath);
DDV_MaxChars(pDX, m_KeyPath, 260);
DDX_Text(pDX, IDC_EDIT_RESULT, m_Result);
DDV_MaxChars(pDX, m_Result, 15);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
//{{AFX_MSG_MAP(CMainDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_BROWSE_FILE, OnButtonBrowseForModule)
ON_BN_CLICKED(IDC_BUTTON_BROWSE_FILE1, OnButtonBrowseForFileData)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
ON_BN_CLICKED(IDC_BUTTON_RESTORE, OnButtonRestore)
ON_BN_CLICKED(IDC_BUTTON_GET_ERR_DESC, OnButtonGetErrDesc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMainDlg message handlers
BOOL CMainDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMainDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMainDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMainDlg::OnButtonBrowseForModule()
{
// TODO: Add your control notification handler code here
UpdateData();
this->m_ConfFile=GetFile(CString("Sp�cify module:"), 0, this->m_ConfFile,
CString("Ini files (*.ini)|*.ini|All files (*.*)|*.* ||"));
UpdateData(FALSE);
}
CString CMainDlg::GetFile(CString & Title, int Falg, CString & DefFile, CString & DefExt)
{
if (Falg==0) { //Get file
CFileDialog dlg(TRUE, DefFile, NULL, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, DefExt, this);
if (dlg.DoModal()==IDOK)
return (dlg.GetPathName());
else return DefFile;
}
else { //Get directory
TCHAR path[MAX_PATH];
BROWSEINFO bi = {0};
bi.lpszTitle ="Select path:";
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl!=0) {
// get the name of the folder and put it in path
SHGetPathFromIDList(pidl, path );
//Set the current directory to path
SetCurrentDirectory (path);
if (strcmp(path, "")!=0)
return CString(path);
else return DefFile;
}
else return DefFile;
}
}
void CMainDlg::OnButtonBrowseForFileData()
{
// TODO: Add your control notification handler code here
UpdateData();
this->m_InOutFile =GetFile(CString("Sp�cify data file:"), 0, this->m_InOutFile,
CString("DATA files (*.dat)|*.dat|All files (*.*)|*.*||"));
UpdateData(FALSE);
}
void CMainDlg::OnButtonSave()
{
// TODO: Add your control notification handler code here
int ParamsNum=0;
DWORD RetErr=0;
m_Result="";
// set dialog params for Save
SetParams(CString("S"));
// set application params and call the
// corresponding method
theApp.SetParams(m_Params, ParamsNum);
theApp.DoSaveRestore(RetErr);
GetDlgItem(IDC_BUTTON_GET_ERR_DESC)->EnableWindow((RetErr==0)?FALSE:TRUE);
// Display result error code
char strErr[20];
ltoa(RetErr,strErr,10);
m_Result=strErr;
UpdateData(FALSE);
}
void CMainDlg::OnButtonRestore()
{
// TODO: Add your control notification handler code here
int ParamsNum=0;
DWORD RetErr=0;
m_Result="";
// set dialog params for Restore
SetParams(CString("R"));
// set application params and call the
// corresponding method
theApp.SetParams(m_Params, ParamsNum);
theApp.DoSaveRestore(RetErr);
GetDlgItem(IDC_BUTTON_GET_ERR_DESC)->EnableWindow((RetErr==0)?FALSE:TRUE);
// Display result error code
char strErr[20];
ltoa(RetErr,strErr,10);
m_Result=strErr;
UpdateData(FALSE);
}
void CMainDlg::SetParams(CString &Op)
{
UpdateData();
m_Params.ConfigFile=m_ConfFile;
m_Params.KeyPath =m_KeyPath;
m_Params.Op =Op;
m_Params.InOutFile =m_InOutFile;
CComboBox *cbo=(CComboBox *)GetDlgItem(IDC_COMBO_ROOT);
if ( (cbo) && (m_Root>=0) ) {
CString strElem="";
cbo->SendMessage(CB_GETLBTEXT, m_Root, (LPARAM)(LPCSTR)strElem);
m_Params.Root=strElem;
}
// Add to history lists
AddToList(IDC_COMBO_CONF_FILE,m_ConfFile);
AddToList(IDC_COMBO_KEY_PATH,m_KeyPath);
AddToList(IDC_COMBO_IN_OUT_FILE,m_InOutFile);
}
void CMainDlg::AddToList(UINT ListID, CString &NewItem)
{
CComboBox *cbo=(CComboBox *)GetDlgItem(ListID);
if (cbo) {
int LC=cbo->GetCount();
if (LC>0) {
CString strElem;
for (int i=0; i<LC; i++) {
cbo->GetLBText(i, strElem.GetBuffer(255));
strElem.ReleaseBuffer();
if (strElem.CompareNoCase(NewItem)==0) {
return;
}
}
cbo->InsertString(0, NewItem);
} else cbo->InsertString(0, NewItem);
}
return;
}
void CMainDlg::OnButtonGetErrDesc()
{
// TODO: Add your control notification handler code here
LPVOID lpMsgBuf;
DWORD ErrorCode;
UpdateData();
if (m_Result.GetLength()==0) return;
ErrorCode=atol(m_Result);
if (!FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
ErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL) ) {
// Handle the error.
MessageBox("Unknown!", "", MB_OK|MB_ICONINFORMATION);;
}
else {
MessageBox((LPCTSTR)lpMsgBuf, "", MB_OK|MB_ICONINFORMATION);
// Free the buffer.
LocalFree( lpMsgBuf );
}
return;
}