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

📄 udiskdlg.cpp

📁 一个通过直接读取磁盘扇区的试验程序,对开发单片机读取U盘的程序员,可以了解FAT16的文件系统.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// udiskDlg.cpp : implementation file
//

#include "stdafx.h"
#include "udisk.h"
#include "udiskDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CUdiskDlg dialog

CUdiskDlg::CUdiskDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUdiskDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUdiskDlg)
	m_sec_cnt = 0;
	m_start_sec = 0;
	m_bps = _T("");
	m_start_ser = _T("");
	m_total_ser = _T("");
	m_type = _T("");
	m_fdt = _T("");
	m_fat = _T("");
	m_dbr = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUdiskDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUdiskDlg)
	DDX_Control(pDX, IDC_TREE1, m_ctr_dir);
	DDX_Control(pDX, IDC_STATIC_msg, m_ctr_msg);
	DDX_Control(pDX, IDC_COMBO_U, m_u);
	DDX_Text(pDX, IDC_EDIT_SEC_CNT, m_sec_cnt);
	DDX_Text(pDX, IDC_EDIT_START_SEC, m_start_sec);
	DDX_Text(pDX, IDC_STATIC_byte_ser, m_bps);
	DDX_Text(pDX, IDC_STATIC_start_ser, m_start_ser);
	DDX_Text(pDX, IDC_STATIC_total_ser, m_total_ser);
	DDX_Text(pDX, IDC_STATIC_type, m_type);
	DDX_Text(pDX, IDC_STATIC_fdt, m_fdt);
	DDX_Text(pDX, IDC_STATIC_fat, m_fat);
	DDX_Text(pDX, IDC_STATIC_dbr, m_dbr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUdiskDlg, CDialog)
	//{{AFX_MSG_MAP(CUdiskDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonRead)
	ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
	ON_BN_CLICKED(IDC_BUTTON_READ_DBR, OnButtonReadDbr)
	ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
	ON_BN_CLICKED(IDC_BUTTON_read_file, OnBUTTONreadfile)
	ON_BN_CLICKED(IDC_BUTTON_save_file, OnBUTTONsavefile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUdiskDlg message handlers

BOOL CUdiskDlg::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
	m_u.SetCurSel(3);
	return TRUE;  // return TRUE  unless you set the focus to a control
}
void CUdiskDlg::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 CUdiskDlg::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 CUdiskDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CUdiskDlg::OnButtonRead() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CString u;
	int res;
	unsigned long bytesread;
	unsigned char buffer[65536] ;
	int i = m_u.GetCurSel();
	CFile save_file ;
	m_ctr_msg.SetWindowText("");
	m_u.GetWindowText(u);
	u= "\\\\.\\" + u + ":";
	HANDLE hDevice = CreateFile(u, 
        GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL, OPEN_EXISTING, 0, NULL); 
	if (hDevice == INVALID_HANDLE_VALUE) 
	{
		m_ctr_msg.SetWindowText("设备打开失败 !");
        return ;
	}
	res = SetFilePointer (hDevice, (m_start_sec*512), NULL, FILE_BEGIN);

	res = ReadFile (hDevice, buffer, 512*m_sec_cnt, &bytesread, NULL);
	if( res != 1 ) 
	{
		m_ctr_msg.SetWindowText("读取数据失败 !");
        return ;
	}
	save_file.Open ("read_data.dat", CFile::modeCreate | CFile::modeWrite  , NULL ) ;
	save_file.Write (buffer, bytesread) ;
	save_file.Close ();
	CloseHandle(hDevice);
	u.Format("读取数据成功,存入 read_data.dat 长度:%i",bytesread);
	m_ctr_msg.SetWindowText(u);

}

void CUdiskDlg::OnButtonWrite() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CString u;
	int res;
	unsigned long bytesread;
	unsigned char buffer[16384] ;
	int i = m_u.GetCurSel();
	CFile read_file ;
	memset(buffer,0xff,16384);
	m_ctr_msg.SetWindowText("");
	res = read_file.Open ("write_data.dat",  CFile::modeRead  , NULL ) ;
	if( res != 1 ) 
	{
		m_ctr_msg.SetWindowText("读取数据文件失败 !");
        return ;
	}
	bytesread = read_file.Read(buffer, sizeof(buffer)) ;
	read_file.Close ();
	
	
	m_u.GetWindowText(u);
	u= "\\\\.\\" + u + ":";
	HANDLE hDevice = CreateFile(u, 
        GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL, OPEN_EXISTING, 0, NULL); 
	if (hDevice == INVALID_HANDLE_VALUE) 
	{
		m_ctr_msg.SetWindowText("设备打开失败 !");
        return ;
	}
	res = SetFilePointer (hDevice, (m_start_sec*512), NULL, FILE_BEGIN);

	res = WriteFile (hDevice, buffer, 512*m_sec_cnt, &bytesread, NULL);
	if(( res != 1 ) | ( bytesread == 0 ))
	{
		m_ctr_msg.SetWindowText("写入数据失败 !");
        return ;
	}
	CloseHandle(hDevice);
	u.Format("写入数据成功,长度:%i",bytesread);
	m_ctr_msg.SetWindowText(u);
	
}

void CUdiskDlg::OnButtonReadDbr() 
{
	// TODO: Add your control notification handler code here
	CString u;
	int res;
	unsigned long bytesread;
	unsigned char buffer[512] ;
	int i = m_u.GetCurSel();
	unsigned int start_sec;
	m_ctr_msg.SetWindowText("");
	m_u.GetWindowText(u);
	u= "\\\\.\\" + u + ":";
	HANDLE hDevice = CreateFile(u, 
        GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL, OPEN_EXISTING, 0, NULL); 
	if (hDevice == INVALID_HANDLE_VALUE) 
	{
		m_ctr_msg.SetWindowText("设备打开失败 !");
        return ;
	}
	dbr_lba = 0 ;//系统引导区绝对扇区号
	res = SetFilePointer (hDevice, dbr_lba, NULL, FILE_BEGIN);

	res = ReadFile (hDevice, buffer, 512, &bytesread, NULL);
	if(( res != 1 ) |  ( bytesread != 512))
	{
		m_ctr_msg.SetWindowText("读取数据失败 !");
        return ;
	}
	CloseHandle(hDevice);
	m_ctr_msg.SetWindowText("读取逻辑0扇区成功");
	res = 0 ;
	memcpy(&res,&buffer[14],2); //DBR
	start_sec = res ;

	res = 0 ;
	memcpy(&res,&buffer[22],2); //每个fat中的扇区数
	res = res * buffer[16] ;	//fat个数
	start_sec = start_sec + res ;
	res = 0 ;
	memcpy(&res,&buffer[17],2); //根目录的项数
	i = 0 ;
	memcpy(&i,&buffer[11],2); //每扇区字节数
	m_bps.Format("%i",i);
	res = res * 32 / i ;
	start_sec = start_sec + res ;
	m_start_ser.Format("%i",start_sec);

	bytesread = 0;
	memcpy(&bytesread,&buffer[32],4); //扇区总数
	if( bytesread == 0 ) memcpy(&bytesread,&buffer[19],2); 
	m_total_ser.Format("%i",bytesread);
	buffer[62] = 0 ;
	m_type="";
	strcat(( char *)m_type.GetBuffer(0),( char *)&buffer[54]);
	if( m_type.Find("FAT",0 ) < 0 ) m_type = " NOT FAT 16" ;
	
	
	res = 0 ;
	memcpy(&res,&buffer[14],2); //保留扇区数
	fat1_lba = dbr_lba + res ;	//文件分配表1绝对扇区号 = 系统引导区绝对扇区号 + 保留扇区数
	res = 0 ;
	memcpy(&res,&buffer[22],2); //每个fat中的扇区数
	fat2_lba = fat1_lba + res ;	//文件分配表2绝对扇区号 = 文件分配表1绝对扇区号 + 每个文件分配表中的扇区数

	res = 0 ;
	memcpy(&res,&buffer[22],2); //每个fat中的扇区数

	fdt_lba = fat1_lba + res * buffer[16] ; //文件目录表绝对扇区号 = 文件分配表1绝对扇区号 + 每个文件分配表中的扇区数 * 文件分配表个数
	m_dbr.Format("%i",dbr_lba);
	m_fat.Format("%i",fat1_lba);
	m_fdt.Format("%i",fdt_lba);
	UpdateData(FALSE);
	
	res = 0 ;
	memcpy(&res,&buffer[17],2); //根目录的项数
	data_lba = fdt_lba + 32 * res / 512 ;	//数据区绝对扇区号 = 文件目录表绝对扇区号 + 32 * 根目录的项数 / 每扇区字节数
}


void CUdiskDlg::OnButtonTest() 
{
	// TODO: Add your control notification handler code here
	
}


void CUdiskDlg::OnBUTTONreadfile() 
{
	// TODO: Add your control notification handler code here
	CString u;
	int res;
	unsigned int loop,loop1;
	unsigned long bytesread ;
	unsigned char fdt_buf[512];
	char buf[32];
	struct DIR_tag root_dir;

	m_u.GetWindowText(u);
	m_hRoot = m_ctr_dir.InsertItem(u);
	m_ctr_dir.Expand(m_hRoot,TVE_EXPAND);
	u= "\\\\.\\" + u + ":";
	hDevice = CreateFile(u, 
        GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL, OPEN_EXISTING, 0, NULL); 
	if (hDevice == INVALID_HANDLE_VALUE) 
	{
		m_ctr_msg.SetWindowText("设备打开失败 !");

⌨️ 快捷键说明

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