www.pudn.com > SectorEdit2000.rar > SectorEdit2000Dlg.cpp


// SectorEdit2000Dlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "SectorEdit2000.h" 
#include "SectorEdit2000Dlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CSectorEdit2000Dlg dialog 
 
CSectorEdit2000Dlg::CSectorEdit2000Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CSectorEdit2000Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CSectorEdit2000Dlg) 
	m_sData = _T(""); 
	m_uFrom = 0; 
	m_uTo = 0; 
	m_bAllDisk = FALSE; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CSectorEdit2000Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CSectorEdit2000Dlg) 
	DDX_Control(pDX, IDC_COMBO_DRIVE, m_DrvListBox); 
	DDX_Text(pDX, IDC_DATA, m_sData); 
	DDX_Text(pDX, IDC_FROM, m_uFrom); 
	DDV_MinMaxUInt(pDX, m_uFrom, 0, 99999999); 
	DDX_Text(pDX, IDC_TO, m_uTo); 
	DDV_MinMaxUInt(pDX, m_uTo, 0, 99999999); 
	DDX_Check(pDX, IDC_ALLDISK, m_bAllDisk); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CSectorEdit2000Dlg, CDialog) 
	//{{AFX_MSG_MAP(CSectorEdit2000Dlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_VIEW, OnView) 
	ON_BN_CLICKED(IDC_CLEARDATA, OnCleardata) 
	ON_BN_CLICKED(IDC_BACKUP, OnBackup) 
	ON_BN_CLICKED(IDC_RESTORE, OnRestore) 
	ON_CBN_SELCHANGE(IDC_COMBO_DRIVE, OnSelchangeComboDrive) 
	ON_BN_CLICKED(IDC_CHECK, OnCheck) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSectorEdit2000Dlg message handlers 
 
BOOL CSectorEdit2000Dlg::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 
	m_DrvListBoxSResult=""; 
	m_bPhysicalDisk=false; 
	VERIFY( m_DrvListBox.LoadItems( false ) >= 0 );	 
	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 CSectorEdit2000Dlg::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 CSectorEdit2000Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CSectorEdit2000Dlg::OnView()  
{ 
	UpdateData(TRUE); 
	if (m_uTo < m_uFrom) 
		return; 
	 
	char cTemp[1]; 
	memcpy(cTemp, m_DrvListBoxSResult.Left(1), 1); 
	UINT uDiskID = cTemp[0] - 64; 
 
	DWORD dwSectorNum = m_uTo - m_uFrom + 1; 
	if (dwSectorNum > 100) 
		return; 
 
	unsigned char* bBuf = new unsigned char[dwSectorNum * 512]; 
	 
	if (ReadSectors(uDiskID, m_uFrom, (UINT)dwSectorNum, bBuf) == FALSE) 
	{ 
		MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_ICONERROR); 
		return; 
	} 
	 
	char* cBuf = new char[dwSectorNum * 5120]; 
	memset(cBuf, 0, sizeof(cBuf)); 
 
	for (DWORD i = 0; i < dwSectorNum * 512; i++) 
	{ 
		sprintf(cBuf, "%s%02X ", cBuf, bBuf[i]); 
 
		if ((i % 512) == 511) 
			sprintf(cBuf, "%s\r\n第%d扇区\r\n", cBuf, (int)(i / 512) + m_uFrom); 
		if ((i % 16) == 15) 
			sprintf(cBuf, "%s\r\n", cBuf); 
		else if ((i % 16) == 7) 
			sprintf(cBuf, "%s- ", cBuf); 
	} 
	SetDlgItemText(IDC_DATA, cBuf); 
	delete[] bBuf; 
	delete[] cBuf; 
} 
 
void CSectorEdit2000Dlg::OnCleardata()  
{ 
	UpdateData(TRUE); 
 
	char cTemp[1]; 
	memcpy(cTemp, m_DrvListBoxSResult.Left(1), 1); 
	UINT uDiskID = cTemp[0] - 64; 
	if (uDiskID > 2) 
	{ 
		if (MessageBox("要清理的是硬盘分区,请确认是否继续?", "提示", MB_YESNO | MB_ICONWARNING) != 6) 
			return; 
		if (uDiskID == 3) 
		{ 
			if (MessageBox("要清理的是系统分区,请再次确认是否继续?", "提示", MB_YESNO | MB_ICONWARNING) != 6) 
				return; 
		} 
	} 
	 
	unsigned char bBuf[512]; 
 
	UINT i = 0; 
	BOOL bRet = TRUE; 
	while (m_bAllDisk)		 
	{ 
		memset(bBuf, 0xFF, sizeof(bBuf)); 
		bRet = WriteSectors(uDiskID, i, 1, bBuf); 
		memset(bBuf, 0, sizeof(bBuf)); 
		bRet = WriteSectors(uDiskID, i, 1, bBuf); 
		 
		if (bRet == FALSE) 
		{ 
			if (i == 0) 
				MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_ICONERROR); 
			else 
				MessageBox("磁盘数据擦除完毕!", "错误", MB_OK | MB_ICONERROR); 
			return; 
		} 
		i++; 
	}	 
 
	if (m_bAllDisk == FALSE) 
	{ 
		for (DWORD i = m_uFrom; i <= m_uTo; i++) 
		{ 
			memset(bBuf, 0xFF, sizeof(bBuf)); 
			bRet = WriteSectors(uDiskID, i, 1, bBuf); 
			memset(bBuf, 0, sizeof(bBuf)); 
			bRet = WriteSectors(uDiskID, i, 1, bBuf); 
			if (bRet == FALSE) 
			{ 
				if (i == 0) 
					MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_ICONERROR); 
				else 
					MessageBox("磁盘数据擦除完毕!", "提示", MB_OK | MB_ICONINFORMATION); 
				return; 
			} 
		} 
	} 
} 
 
 
void CSectorEdit2000Dlg::OnBackup()  
{ 
	UpdateData(TRUE); 
	if (m_uTo < m_uFrom) 
		return; 
 
	CFileDialog fileDlg(FALSE, "*.sec", "*.sec", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "磁盘扇区数据(*.sec)|*.sec||", NULL); 
	CFile file; 
	if (fileDlg.DoModal() != IDOK) 
		return; 
	 
	file.Open(fileDlg.GetPathName(), CFile::modeCreate | CFile::modeReadWrite); 
	char cTemp[1]; 
	memcpy(cTemp, m_DrvListBoxSResult.Left(1), 1); 
	UINT uDiskID = cTemp[0] - 64; 
 
	DWORD dwSectorNum = m_uTo - m_uFrom + 1; 
	unsigned char* bBuf = new unsigned char[dwSectorNum * 512]; 
	 
	if (ReadSectors(uDiskID, m_uFrom, (UINT)dwSectorNum, bBuf) == FALSE) 
	{ 
		MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_ICONERROR); 
		return; 
	} 
 
	file.Write(bBuf, dwSectorNum * 512); 
	file.Close(); 
 
	delete[] bBuf; 
 
	MessageBox("数据备份完毕!", "提示", MB_OK | MB_ICONINFORMATION); 
} 
 
void CSectorEdit2000Dlg::OnRestore()  
{ 
	UpdateData(TRUE); 
	 
	char cTemp[1]; 
	memcpy(cTemp, m_DrvListBoxSResult.Left(1), 1); 
	UINT uDiskID = cTemp[0] - 64; 
 
	CFileDialog fileDlg(TRUE, "*.sec", "*.sec", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "磁盘扇区数据(*.sec)|*.sec||", NULL); 
	CFile file; 
	if (fileDlg.DoModal() != IDOK) 
		return; 
 
	file.Open(fileDlg.GetPathName(), CFile::modeReadWrite); 
	DWORD dwSectorNum = file.GetLength(); 
	if (dwSectorNum % 512 != 0) 
		return; 
	dwSectorNum /= 512; 
 
	unsigned char* bBuf = new unsigned char[dwSectorNum * 512]; 
	file.Read(bBuf, dwSectorNum * 512); 
 
	if (WriteSectors(uDiskID, m_uFrom, (UINT)dwSectorNum, bBuf) == FALSE) 
	{ 
		MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_ICONERROR); 
		return; 
	} 
 
	file.Close(); 
	delete[] bBuf; 
 
	MessageBox("数据恢复完毕!", "提示", MB_OK | MB_ICONINFORMATION); 
} 
 
BOOL CSectorEdit2000Dlg::WriteSectors(BYTE bDrive, DWORD dwStartSector, WORD wSectors, LPBYTE lpSectBuff) 
{ 
	if (bDrive == 0) 
		return 0; 
 
	char devName[] = "\\\\.\\A:"; 
	devName[4] ='A' + bDrive - 1; 
	HANDLE hDev; 
	if(m_bPhysicalDisk==false) 
	{ 
		hDev = CreateFile(devName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 
	} 
	else 
		hDev = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 
 
 
	if (hDev == INVALID_HANDLE_VALUE) 
		return 0; 
 
	SetFilePointer(hDev, 512 * dwStartSector, 0, FILE_BEGIN); 
 
	DWORD dwCB; 
	BOOL bRet = WriteFile(hDev, lpSectBuff, 512 * wSectors, &dwCB, NULL); 
	CloseHandle(hDev); 
	return bRet; 
} 
 
BOOL CSectorEdit2000Dlg::ReadSectors(BYTE bDrive, DWORD dwStartSector, WORD wSectors, LPBYTE lpSectBuff) 
{ 
	if (bDrive == 0) 
		return 0; 
 
	char devName[] = "\\\\.\\A:"; 
	devName[4] ='A' + bDrive - 1; 
	HANDLE hDev; 
	if(m_bPhysicalDisk==false) 
		hDev = CreateFile(devName, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 
	else 
		hDev = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 
	if (hDev == INVALID_HANDLE_VALUE) 
		return 0; 
 
	SetFilePointer(hDev, 512 * dwStartSector, 0, FILE_BEGIN); 
 
	DWORD dwCB; 
	BOOL bRet = ReadFile(hDev, lpSectBuff, 512 * wSectors, &dwCB, NULL); 
	CloseHandle(hDev); 
	return bRet; 
} 
 
void CSectorEdit2000Dlg::OnSelchangeComboDrive()  
{ 
	// TODO: Add your control notification handler code here 
	int s; 
 
	s = m_DrvListBox.GetCurSel(); 
	if( s != CB_ERR ) 
		m_DrvListBoxSResult = ( const char * )m_DrvListBox.GetItemDataPtr( m_DrvListBox.GetCurSel()); 
 
} 
 
void CSectorEdit2000Dlg::OnCheck()  
{ 
	// TODO: Add your control notification handler code here 
	m_bPhysicalDisk=!m_bPhysicalDisk; 
	if(m_bPhysicalDisk==true) 
	{ 
		GetDlgItem( IDC_COMBO_DRIVE)->EnableWindow( false ); 
	} 
	if(m_bPhysicalDisk==false) 
	{ 
		GetDlgItem( IDC_COMBO_DRIVE)->EnableWindow( true ); 
	}	 
}