⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfcfatlinedlg.cpp

📁 直接对磁盘的bit数据进行读写
💻 CPP
字号:
// MFCFatlineDlg.cpp : implementation file
//

 #include "stdafx.h"
#include "MFCFatline.h"
#include "MFCFatlineDlg.h"
#include <math.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()

/////////////////////////////////////////////////////////////////////////////
// CMFCFatlineDlg dialog

CMFCFatlineDlg::CMFCFatlineDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMFCFatlineDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMFCFatlineDlg)
	m_edit = _T("");
	m_BiaoTi = _T("");
	m_ReadFrom = 0;
	m_ReadTo = 0;
	m_edit_ShowNum = 0;
	m_xinxi = _T("");
	m_num_Sector = 0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	DriveName=_T("\\\\.\\F:");
	DBRBuffer=new BYTE[513];
	FDTbuffer=new BYTE[512*8+1];
	FATbuffer=new BYTE[512*8+1];



}

void CMFCFatlineDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMFCFatlineDlg)
	DDX_Control(pDX, IDC_COMBO_FenQu, m_comboBox);
	DDX_Control(pDX, IDC_TREE1, m_tree);
	DDX_Text(pDX, IDC_EDIT1, m_edit);
	DDV_MaxChars(pDX, m_edit, 20971521);
	DDX_Text(pDX, IDC_WenZi, m_BiaoTi);
	DDX_Text(pDX, IDC_EDIT_read_from, m_ReadFrom);
	DDX_Text(pDX, IDC_EDIT_read_to, m_ReadTo);
	DDX_Text(pDX, IDC_EDIT_showNum, m_edit_ShowNum);
	DDX_Text(pDX, IDC_STATIC_xinxi, m_xinxi);
	DDX_Text(pDX, IDC_EDIT_num_sector, m_num_Sector);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMFCFatlineDlg, CDialog)
//{{AFX_MSG_MAP(CMFCFatlineDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Btn_showDBR, OnBtnshowDBR)
ON_BN_CLICKED(IDC_Btn_showFDT, OnBtnshowFDT)
	ON_CBN_SELENDOK(IDC_COMBO_FenQu, OnSelendokCOMBOFenQu)
	ON_BN_CLICKED(IDC_Btn_show, OnBtnshow)
	ON_BN_CLICKED(IDC_Btn_save, OnBtnsave)
	ON_BN_CLICKED(IDC_Btn_show2, OnBtnshow2)
	ON_BN_CLICKED(IDC_Btn_showFAT, OnBtnshowFAT)
	ON_BN_CLICKED(IDC_Btn_next, OnBtnnext)
	ON_BN_CLICKED(IDC_Btn_Forward, OnBtnForward)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCFatlineDlg message handlers

BOOL CMFCFatlineDlg::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

	ReadFileMessage();
	m_tree.Init();//初始化树
	m_comboBox.Init();//初始化comboBox
	m_BiaoTi="f:\\";
	m_strSelcom="f:\\";

	UpdateData(FALSE);	
//	MyShowwindow(TRUE,true,FALSE);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMFCFatlineDlg::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 CMFCFatlineDlg::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 CMFCFatlineDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMFCFatlineDlg::OnBtnshowDBR() 
{
	// TODO: Add your control notification handler code here
	ReadFileMessage();
	m_edit_ShowNum=0;
	m_BiaoTi="";
	m_BiaoTi=m_strSelcom;
	m_BiaoTi+="分区的DBR为:";
	UpdateData(FALSE);
	showData(DBRBuffer,512);	
}
void CMFCFatlineDlg::OnBtnshowFAT() 
{
	// TODO: Add your control notification handler code here
	ReadFileMessage();
	m_edit_ShowNum=FatStart;
	m_BiaoTi="";
	m_BiaoTi=m_strSelcom;
	m_BiaoTi+="分区的FAT为:";
	m_xinxi="FAT所占的扇区数为:";
	m_num_Sector=FatSize;
	showData(FATbuffer,512*8);
	UpdateData(FALSE);
}



void CMFCFatlineDlg::OnBtnshowFDT() 
{
	// TODO: Add your control notification handler code here
	ReadFileMessage();
	m_edit_ShowNum=FDTpos;
	m_BiaoTi="";
	m_BiaoTi=m_strSelcom;
	m_BiaoTi+="分区的FDT为:";
	UpdateData(FALSE);
	showData(FDTbuffer,512*8);
	
}
void CMFCFatlineDlg::OnBtnForward() 
{
	// TODO: Add your control notification handler code here
	m_edit_ShowNum-=8;
	if(m_edit_ShowNum<=0)
	{
		MessageBox("已到分区的首部!");
		m_edit_ShowNum=0;
	}
		
		FATbuffer=MyCreateFile(DriveName,m_edit_ShowNum*512,512*8);	
		showData(FATbuffer,512*8);



	UpdateData(FALSE);
}

void CMFCFatlineDlg::OnBtnnext() 
{
	// TODO: Add your control notification handler code here
	m_edit_ShowNum+=8;
	if(m_edit_ShowNum==m_num_Sector)
	{
		MessageBox("已到硬盘的结尾!");
	}
	

		memset(FATbuffer,0,512*8+1);
		FATbuffer=MyCreateFile(DriveName,m_edit_ShowNum*512,512*8);	
		showData(FATbuffer,512*8);
	

	UpdateData(FALSE);
}	


void CMFCFatlineDlg::showData(BYTE *buffer, DWORD NUM)
{
	char * a=new char[512*30];//
	memset(a,0,512*30);
    for(DWORD i=0;i<NUM;i++)
	{
		sprintf(a,"%s%02X ",a,buffer[i]);//格式化数字到字符串中
		if(i%512==511)
			sprintf(a,"%s\r\n",a);
		if((i%16)==15)
			sprintf(a,"%s\r\n",a);
		else if(i%16==7)
			sprintf(a,"%s-",a);
		
		
	}
	m_edit=a;
    SetDlgItemText(IDC_EDIT1,m_edit);
	delete[]a;	
}

BYTE* CMFCFatlineDlg::MyCreateFile(CString DriveName,long lDistanceToMove,
								    DWORD nNumberOfBytesToRead)
{
	byte *buffer=new byte[512*8+1];
	memset(buffer,0,512*8+1);
	HANDLE hDrive;
	hDrive=CreateFile(DriveName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);
	if(hDrive==INVALID_HANDLE_VALUE)
	{
		MessageBox("初始化错误!即将退出程序");
		exit(0); 

	}
	SetFilePointer(hDrive,lDistanceToMove,0,FILE_CURRENT);//设置文件指针	
	DWORD readOutUnm=0;
	ReadFile(hDrive,(LPVOID)buffer,nNumberOfBytesToRead,&readOutUnm,NULL);
	if(readOutUnm==0)
	{
		MessageBox("读取扇区失败!即将退出程序");
		return 0;	
	}
	CloseHandle(hDrive);
	return buffer;
	delete[]buffer;	
}

CMFCFatlineDlg::~CMFCFatlineDlg()
{

	delete []FDTbuffer;
	delete[]FATbuffer;
	delete[]DBRBuffer;

}


 void CMFCFatlineDlg::OnSelendokCOMBOFenQu() 
 {
 	// TODO: Add your control notification handler code here
	int m_Index_combox;
 	m_Index_combox=m_comboBox.GetCurSel();
 	m_comboBox.GetLBText(m_Index_combox,m_strSelcom);
 	m_BiaoTi=m_strSelcom;
	CString a=DriveName.Left(4);
	if(m_strSelcom=="PhysicalDrive0")
	{
		a+="PhysicalDrive0";
		DriveName=a.Left(19);
		GetDlgItem(IDC_EDIT_num_sector)->EnableWindow(FALSE);
		SetDlgItemText(IDC_Btn_showDBR,"显示MBR");
		GetDlgItem(IDC_Btn_showFAT)->EnableWindow(FALSE);
		GetDlgItem(IDC_Btn_showFDT)->EnableWindow(FALSE);

			
	}

	else
	{
		a+=m_strSelcom;
		DriveName=a.Left(6);
		GetDlgItem(IDC_EDIT_num_sector)->EnableWindow(TRUE);
		SetDlgItemText(IDC_Btn_showDBR,"显示DBR");
		GetDlgItem(IDC_Btn_showFAT)->EnableWindow(TRUE);
		GetDlgItem(IDC_Btn_showFDT)->EnableWindow(TRUE);
	}
	ReadFileMessage();
	UpdateData(FALSE);
	
 }

void CMFCFatlineDlg::ReadFileMessage()
{
	DBRBuffer=MyCreateFile(DriveName,0,512);
	SectorUnmPerCluster=DBRBuffer[0x0d];	//获得每簇扇区数
	//输出FDT所在扇区编号:
	XTSize=DBRBuffer[0x0f]*16+DBRBuffer[0x0e];
	FatStart=XTSize;
	FatSize=(DWORD)DBRBuffer[0x27]*pow(0X100,3)+(DWORD)DBRBuffer[0x26]*pow(0X100,2)+(DWORD)DBRBuffer[0x25]*0X100+(DWORD)DBRBuffer[0x24];
	FATbuffer=MyCreateFile(DriveName,FatStart*512,512*8);	
	m_tree.FATBuffer=FATbuffer;//把FAT传递给tree控件
	FDTpos=XTSize+FatSize*2;

	FDTbuffer=MyCreateFile(DriveName,FDTpos*512,512*8);
	m_tree.FDTBuffer=FDTbuffer;//把FDT传递给tree控件
	m_xinxi="该分区的容量为(GB)";
	m_num_Sector=((double)DBRBuffer[0x23]*pow(0x100,3)+(double)DBRBuffer[0x22]*pow(0x100,2)
		+(double)DBRBuffer[0x21]*0x100+(double)DBRBuffer[0x20])/(2*1024*1024);
	UpdateData(FALSE);	

}


void CMFCFatlineDlg::OnBtnshow() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_ReadTo-m_ReadFrom>7)
	{
		MessageBox("输入范围过大,请重新输入!");
	}
	else
	{	
		BYTE *Buffer=new BYTE[512*20];
		memset(Buffer,0,sizeof(Buffer[512*20]));
		Buffer=MyCreateFile(DriveName,m_ReadFrom*512,(m_ReadTo-m_ReadFrom+1)*512);
		showData(Buffer,(m_ReadTo-m_ReadFrom+1)*512);
		m_BiaoTi="";
		m_BiaoTi=m_strSelcom;
		m_BiaoTi+="所选扇区的数据为:";
		UpdateData(FALSE);
		delete []Buffer;	
	}	
}

void CMFCFatlineDlg::OnBtnshow2() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	BYTE*buffer=new BYTE[512];
	memset(buffer,0,sizeof(buffer[512]));
	buffer=MyCreateFile(DriveName,m_edit_ShowNum*512,512);
	showData(buffer,512);
	delete[]buffer;
	
}
void CMFCFatlineDlg::OnBtnsave() 
{
	// TODO: Add your control notification handler code here
	if(IDCANCEL==MessageBox("是否保存",NULL,MB_OK|MB_OKCANCEL))
		return;

	
	UpdateData();
	byte *buffer=new byte[512*8+1];
	memset(buffer,0,512*8+1);
	HANDLE hDrive;
	hDrive=CreateFile(DriveName,GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);
	if(hDrive==INVALID_HANDLE_VALUE)
	{
		MessageBox("初始化错误!即将退出程序");
		exit(0); 
		
	}
	SetFilePointer(hDrive,m_edit_ShowNum*512,0,FILE_CURRENT);//设置文件指针	
	int num=0;
	decstr2hexbyte(m_edit,buffer);

	DWORD WriteInUnm;
	WriteFile(hDrive,buffer,512*8,&WriteInUnm,NULL);
	if(WriteInUnm==0)
	{
		MessageBox("读取扇区失败!即将退出程序");
		
		exit(0);	
	}
	CloseHandle(hDrive);
	delete[]buffer;	
	
}

int CMFCFatlineDlg::decstr2hexbyte( const char* str , BYTE * hexbyte) 
{ 
	int i=0, count; 
	int nRet = 0; 
	char* StrSector=new char[1025];
	
	if ( str == NULL )
		return nRet;
	
	for(int ii=0;ii<32;ii++)
	{
		for(int j=0;j<48;j+=3)
		{
			StrSector[i]=str[j+ii*51];
			StrSector[i+1]=str[j+ii*51+1];
			i+=2;
			if(j==21)
				j++;

		}
	}
	count = (int)strlen(StrSector); 
	if (count == 0)
		return nRet; 
	
	char stmp[5] = {0}; 
	strcpy(stmp,"0x"); 
	for (i=0; i <count; i+=2, nRet++) 
	{ 
		stmp[2] = StrSector[i]; 
		stmp[3] = StrSector[i+1]; 
		hexbyte[nRet] = strtol(stmp, NULL, 0); 
	} 
	delete[]StrSector;
	
	return nRet;


} 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -