// DiskCopyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DiskCopy.h"
#include "DiskCopyDlg.h"
#include <windows.h>
#include <winioctl.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDiskCopyDlg dialog
CDiskCopyDlg::CDiskCopyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDiskCopyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDiskCopyDlg)
m_strDestFilePath = _T("");
m_strSourceFilePath = _T("");
m_nRadioSource = -1;
m_nRadioDest = -1;
m_StaticProgress = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDiskCopyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDiskCopyDlg)
DDX_Control(pDX, IDC_COMDESTDRIVE, m_ComDestDrive);
DDX_Control(pDX, IDC_COMSOURCEDRIVE, m_ComSourceDrive);
DDX_Control(pDX, IDC_PROGRESS, m_Progress);
DDX_Text(pDX, IDC_EDITDESTFILEPATH, m_strDestFilePath);
DDX_Text(pDX, IDC_EDITSOURCEFILEPATH, m_strSourceFilePath);
DDX_Radio(pDX, IDC_RADIO1, m_nRadioSource);
DDX_Radio(pDX, IDC_RADIO3, m_nRadioDest);
DDX_Text(pDX, IDC_STATICPROGRESS, m_StaticProgress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDiskCopyDlg, CDialog)
//{{AFX_MSG_MAP(CDiskCopyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BTNBROWSE, OnBtnbrowse)
ON_BN_CLICKED(IDC_BTNBROWSE1, OnBtnbrowse1)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BTNSTOP, OnBtnstop)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDiskCopyDlg message handlers
BOOL CDiskCopyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
InitData();
return TRUE; // return TRUE unless you set the focus to a control
}
void CDiskCopyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CDiskCopyDlg::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 CDiskCopyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDiskCopyDlg::InitData()
{
pCopyThread = new COPYTHREAD;
InitCopyThread(pCopyThread);
InitDiskList();
char ConfigFile[MAX_PATH];
GetCurrentDirectory(MAX_PATH,ConfigFile);
m_strConfigFile = ConfigFile;
m_strConfigFile += "\\DiskCopy.ini";
ReadData();
m_Progress.SetStep(1);
m_Progress.SetRange(0,PROGRESSLENG);
m_StaticProgress = _T("0.00%");
m_Progress.SetPos(0);
UpdateData(FALSE);
}
void CDiskCopyDlg::InitDiskList()
{
HANDLE hDev = 0;
char driveName [256];
int index = 0;
do
{
sprintf (driveName, "\\\\.\\PhysicalDrive%d",index);
hDev = CreateFile (driveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if(hDev != INVALID_HANDLE_VALUE)
{
m_ComSourceDrive.AddString(driveName);
m_ComDestDrive.AddString(driveName);
CloseHandle(hDev);
}
index++;
}while(hDev != INVALID_HANDLE_VALUE);
}
int CDiskCopyDlg::GetHardDeskSize(CString strPhysicalDrive)
{
DISK_GEOMETRY dgeometry;
DISK_GEOMETRY *pdg = &dgeometry;
HANDLE hDevice; // handle to the drive to be examined
DWORD junk; // discard results
hDevice = CreateFile(strPhysicalDrive, // drive to open
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return 0;
}
DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
NULL, 0, // no input buffer
pdg, sizeof(*pdg), // output buffer
&junk, // # bytes returned
(LPOVERLAPPED) NULL); // synchronous I/
CloseHandle(hDevice);
ULONGLONG DiskSize = pdg->Cylinders.QuadPart * (ULONG)pdg->TracksPerCylinder *
(ULONG)pdg->SectorsPerTrack * (ULONG)pdg->BytesPerSector;
return int(DiskSize / (1024 * 1024));
}
int CDiskCopyDlg::GetFileSize(CString strPath)
{
WIN32_FIND_DATA fileInfo;
HANDLE hFind;
DWORD fileSize;
hFind = FindFirstFile(strPath ,&fileInfo);
if(hFind != INVALID_HANDLE_VALUE)
fileSize = fileInfo.nFileSizeLow;
FindClose(hFind);
return int(fileSize / (1024 * 1024));
}
void CDiskCopyDlg::WriteData()
{
UpdateData(TRUE);
CString strTemp;
int nIndex = 0;
WritePrivateProfileString("DiskCopy","SourceFilePath",m_strSourceFilePath,m_strConfigFile);
strTemp.Format("%d",m_nRadioSource);
WritePrivateProfileString("DiskCopy","RadioSource",strTemp,m_strConfigFile);
nIndex = m_ComSourceDrive.GetCurSel();
strTemp.Format("%d",nIndex);
WritePrivateProfileString("DiskCopy","SourceDrive",strTemp,m_strConfigFile);
WritePrivateProfileString("DiskCopy","DestFilePath",m_strDestFilePath,