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

📄 accesssectordlg.cpp

📁 这是有关磁盘分区表操作的材料
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// accessSectorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "accessSector.h"
#include "accessSectorDlg.h"

#include <io.h>
#include <stdlib.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

UINT doThread(LPVOID);

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CAccessSectorDlg dialog

CAccessSectorDlg::CAccessSectorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAccessSectorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAccessSectorDlg)
//	m_edit = _T("ojiioj");
//	startSector = 0;
	sectorToDo = 0;

	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
memset(sbuf,0,51200);
}

void CAccessSectorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAccessSectorDlg)
//	DDX_Text(pDX, IDC_EDIT3, m_edit);
	DDX_Text(pDX, IDC_EDIT5, startSector);
	
	DDX_Text(pDX, IDC_EDIT6, sectorToDo);
//	DDV_MinMaxDWord(pDX, sectorToDo, 0, 4294967295);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAccessSectorDlg, CDialog)
	//{{AFX_MSG_MAP(CAccessSectorDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)	
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAccessSectorDlg message handlers

BOOL CAccessSectorDlg::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
	CProgressCtrl *p=(CProgressCtrl *)GetDlgItem(IDC_PROGRESS1);
	
	p->SetRange(1,100);
	p->SetStep(1);
	p->SetPos(0);


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

void CAccessSectorDlg::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 CAccessSectorDlg::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
	{
		/*
		CPaintDC dc(this);
		dc.SetBkMode(TRANSPARENT);
		dc.TextOut(40,300,doneSector);
		*/
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAccessSectorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CAccessSectorDlg::doAbsoluteSector()
{
}
	
/*
//memset(sbuf,0,51200);	
//SetDlgItemText(IDC_EDIT3,NULL);
//GetDlgItem(IDC_EDIT3)->UpdateData();	

CString deviceName(device);
//CString comp="0";
//ULONG c=512*100;

BYTE buf[512000];
DWORD startbPos=startSector*512;
//DWORD bCountToDo=sectorToDo*512;

CString test;
test.Format("%lu,%lu",sectorToDo,bCountToDo);
MessageBox(test);

DWORD bufCount=sectorToDo/1000;
unsigned int restSector=sectorToDo%1000;
DWORD sectorDone=0;

HANDLE hFile;
DWORD doWriteSector=0;
DWORD bDone;


if(device.IsEmpty()==TRUE)
{MessageBox("请输入设备名。"); return;} 
if(sectorToDo==0)
{MessageBox("读取的扇区数不能为0。"); return;}
//DWORD doWriteSector=0;


if((f=fopen((char *)(LPCSTR)filename,"wb+"))==NULL)
{
	MessageBox("FILE ERROR");
	return;
}

//f.Open(filename,CFile::modeCreate);
//f.Write(NULL,0);
//f.Close();

//f.Open(filename,CFile::modeCreate);
//f.Close();

//f.Open(filename,CFile::modeCreate|CFile::modeReadWrite);

//MessageBox(deviceName);
if(device.Compare("0")==0)
//{MessageBox("equal");
//return;}
//{MessageBox("equal");return;}

device="\\\\.\\PHYSICALDRIVE0";
else if(device.Compare("1")==0)
device="\\\\.\\PHYSICALDRIVE1";
else device=CString("\\\\.\\")+CString(device);
//MessageBox(deviceName);
//return;


HANDLE hDev=CreateFile(device,GENERIC_READ,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);

	SetFilePointer(hDev,startbPos,0,FILE_BEGIN);

	
ReadFile(hDev,buf,512000,&bDone,0);
showSector(buf,sectorToDo);
SetDlgItemText(IDC_EDIT3,sbuf);
//HANDLE hFile=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
//CloseHandle(hFile);

//如果得到的句柄有效,就可以使用ReadFile来读取了,    

//while()
//{

hFile=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);


if(filename.IsEmpty()==TRUE)
{CloseHandle(hDev); return;}

if(sectorToDo<1000) doWriteSector=sectorToDo;
else doWriteSector=1000;

CClientDC dc(this);
	//	dc.SetBkMode(TRANSPARENT);
	
for(DWORD i=0;sectorDone<sectorToDo;i++)
{

if(stop==TRUE) return;
	SetFilePointer(hDev,startbPos,0,FILE_BEGIN);

	
ReadFile(hDev,buf,512000,&bDone,0);
//CloseHandle(hDev);

//hFile=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);

SetFilePointer(hFile,i*512000,NULL,FILE_BEGIN);

if(i>=bufCount) doWriteSector=restSector;
WriteFile(hFile,buf,doWriteSector*512,&bDone,0);
sectorDone+=doWriteSector;
startbPos+=512000;

doneSector.Format("%d",sectorDone);

dc.TextOut(40,300,doneSector);
//MessageBox("doneSector");
Invalidate(FALSE);
//CloseHandle(hFile);

//if(j>=bufCount) return;

}

CloseHandle(hDev);
}																														
*/										
//for(int i=0;i<=2;i++)
//{
//MessageBox(filename);

/*	
if((f=fopen((char *)(LPCSTR)filename,"ab+"))==NULL)
	{
		MessageBox("FILE ERROR");
		return;
	}
	fwrite(buf,512*doSector,1,f);
*/
  //	UINT
//buf+=65536;

//f.Write(buf,6000);
//MessageBox(filename);
//buf+=51200;
//}
//f.Write(buf,rest);
//fclose(f);

/*
else
{
	
for(unsigned int i=0;i<count;i++)
{
	f.Read(buf,65535);
buf+=65535;
}
f.Read(buf,rest);
f.Close();	
	WriteFile(hDev,buf,512*sectorToDo,&bDone,0);
}
	
	//读取结束要关闭该句柄,   
	
	*/
//}


void CAccessSectorDlg::OnButton1() 
{
stop=TRUE;

	// TODO: Add your control notification handler code here

//	unsigned char buf[512];
//	memset(buf,0,512);
//	FILE *f;
//	DWORD bDone;	

	
//	HANDLE hDev=CreateFile("\\\\.\\PHYSICALDRIVE0",GENERIC_READ,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
//如果得到的句柄有效,就可以使用ReadFile来读取了,
 //    ReadFile(hDev,buf,512,&bDone,0);
//读取结束要关闭该句柄,
 //    CloseHandle(hDev);

/*	 
	 CString filename;
GetDlgItemText(IDC_EDIT1,filename);

//filename.Remove('b');
	
if((f=fopen((char *)(LPCSTR)filename,"wb+"))==NULL)
	{
	fileError(filename);
		return;
	}


	fwrite(buf,512,1,f);
	fclose(f);

//	sbuf[0]=0;
	memset(sbuf,0,512);

  */
}
void CAccessSectorDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	
	stop=TRUE;
	OnCancel();
	/*
	unsigned char *buf=new unsigned char[512];
	//memset(buf,0,1164000);
	FILE *f;
	DWORD bDone;
	

	 CString filename;
GetDlgItemText(IDC_EDIT1,filename);

	if((f=fopen((char *)(LPCSTR)filename,"rb"))==NULL)
		{
	fileError(filename);
		return;
	}
		fread(buf,512,1,f);	
	fclose(f);

if(MessageBox("这将是不可逆的!确定吗?","严重警告!",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
	HANDLE hDev=CreateFile("\\\\.\\PHYSICALDRIVE0",GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
//如果得到的句柄有效,就可以使用ReadFile来读取了,
     WriteFile(hDev,buf,512,&bDone,0);
//读取结束要关闭该句柄,
     CloseHandle(hDev);
MessageBox("完成");	 
}	

}
*/
}

void CAccessSectorDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
//UpdateData();	

UpdateData();
GetDlgItemText(IDC_EDIT4,device);
GetDlgItemText(IDC_EDIT7,filename);

if(device.IsEmpty()==TRUE)
{MessageBox("请输入设备名。"); return;} 


if(device.Compare("0")==0)

device="\\\\.\\PHYSICALDRIVE0";
else if(device.Compare("1")==0)
device="\\\\.\\PHYSICALDRIVE1";
else device=CString("\\\\.\\")+CString(device);


CString editSector;
char buf[512000];
memset(buf,0,512000);
char edit[10240];
char backToPreviousDiskBuf[5120];
memset(edit,0,10240);
memset(backToPreviousDiskBuf,0,5120);
GetDlgItemText(IDC_EDIT3,editSector);


//MessageBox(&edit[1130]);



for(unsigned int i=0,j=0;i<10;i++)
{
editSector.Delete(j,13);
//editSector.Delete(1132,12);
//editSector.
//j+=1664;
j+=2656;
}

for(i=0,j=0;i<320;i++)
{
editSector.Delete(j,14);
j+=69;
}

for(i=0,j=51;i<320;i++)
{
if(i==319) editSector.Delete(j,16);
else	editSector.Delete(j,18);
j+=51;
}


editSector.Remove(' ');
editSector.Remove('-');
//editSector.Remove('\r');
//editSector.Remove('\n');

//if(editSector.GetLength()==10240) {MessageBox("ok"); return;}
//else return;
for(i=0;i<10240;i++)
edit[i]=editSector.GetAt(i);

char buffer[3];
buffer[2]='\0';
char *stopString="\0";


for(i=0,j=0;j<5120;i+=2,j++)
{

	memcpy(buffer,edit+i,2);
//StrbackToPreviousDiskBufEx(buffer,STIF_SUPPORT_HEX,&backToPreviousDiskBuf[j]);
backToPreviousDiskBuf[j]=(char)strtol(buffer,&stopString,16);

}

//memcpy(edit,(char *)(LPCSTR)editSector,5120);

//if(memcmp(comp,backToPreviousDiskBuf,5120)==0) MessageBox("OK");

//SetDlgItemText(IDC_EDIT3,editSector);

//editSector.Format("%d",editSector.GetLength());

/*
//for(int i=0;i<13;i++)
editSector.Remove(editSector.GetAt(0));
editSector.Remove(editSector.GetAt(1));
editSector.Remove(editSector.GetAt(2));
editSector.Remove(editSector.GetAt(3));

⌨️ 快捷键说明

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