?? accesssectordlg.cpp
字號:
editSector.Remove(editSector.GetAt(4));
editSector.Remove(editSector.GetAt(5));
editSector.Remove(editSector.GetAt(6));
editSector.Remove(editSector.GetAt(7));
editSector.Remove(editSector.GetAt(8));
editSector.Remove(editSector.GetAt(9));
editSector.Remove(editSector.GetAt(10));
*/
//editSector.Remove(a);
DWORD bDone;
HANDLE hDev=CreateFile(device,GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
if(hDev==INVALID_HANDLE_VALUE)
{MessageBox("打開硬盤失敗。"); return;}
if((MessageBox("這是不可逆的!確定寫入嗎?",NULL,MB_OKCANCEL|MB_ICONWARNING))!=MB_OKCANCEL)
{
CloseHandle(hDev);
return;
}
WriteFile(hDev,backToPreviousDiskBuf,5120,&bDone,0);
//CloseHandle(hFile);
CloseHandle(hDev);
MessageBox("完成。");
/*
/*
char buf[512000];
memset(buf,0,51200);
char add[64];
UINT count;
memset(add,0,64);
if(showSect>10) count=5120;
else count=showSect*512;
for(unsigned int i=0;i<count;i++)
{
if(i == 0)
sprintf(add,"第 %d 扇區:\r\n%02X ",i/512,buf[i]);
else if(i % 512 == 0)
sprintf(add,"\r\n第 %d 扇區:\r\n%02X ",i/512,buf[i]);
else if((i==8) || i % 16 ==8)
sprintf(add,"- %02X ",buf[i]);
else if( (i>0) && (i % 16 ==0))
sprintf(add,"\r\n%02X ",buf[i]);
else sprintf(add,"%02X ",buf[i]);
//m_edit=sbuf;
strcat(sbuf,add);
memset(add,0,64);
/*
unsigned char *buf=new unsigned char[512];
memset(buf,0,512);
FILE *f;
DWORD bDone;
HANDLE hDev=CreateFile("\\\\.\\C:",GENERIC_READ,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
//如果得到的句柄有效,就可以使用ReadFile來讀取了,
ReadFile(hDev,buf,512,&bDone,0);
//讀取結束要關閉該句柄,
CloseHandle(hDev);
CString filename;
GetDlgItemText(IDC_EDIT2,filename);
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);
/*
for(unsigned int i=0;i<512;i++)
{
if(i == 0)
sprintf(sbuf,"%s第 %d 扇區:\r\n%02X ",sbuf,i/512,buf[i]);
else if(i % 512 == 0)
sprintf(sbuf,"%s\r\n第 %d 扇區:\r\n%02X ",sbuf,i/512,buf[i]);
else if((i==8) || i % 16 ==8)
sprintf(sbuf,"%s- %02X ",sbuf,buf[i]);
else if( (i>0) && (i % 16 ==0))
sprintf(sbuf,"%s\r\n%02X ",sbuf,buf[i]);
else sprintf(sbuf,"%s%02X ",sbuf,buf[i]);
//m_edit=sbuf;
}
SetDlgItemText(IDC_EDIT3,sbuf);
MessageBox("完成");
*/
}
void CAccessSectorDlg::OnButton4()
{
// TODO: Add your control notification handler code here
if(stop==TRUE) stop=FALSE;
cmd=3;
AfxBeginThread(doThread,this);
/*
char *buf=new char[512];
memset(buf,0,512);
FILE *f;
DWORD bDone;
CString filename;
GetDlgItemText(IDC_EDIT2,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("\\\\.\\C:",GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
//如果得到的句柄有效,就可以使用ReadFile來讀取了,
WriteFile(hDev,buf,512,&bDone,0);
//讀取結束要關閉該句柄,
CloseHandle(hDev);
MessageBox("完成");
}
*/
}
void CAccessSectorDlg::fileError(CString filename)
{
CString error=CString("無效的文件名:")+CString("\"")+CString(filename)+CString("\"");
MessageBox(error);
}
void CAccessSectorDlg::OnButton5()
{
// TODO: Add your control notification handler code here
/*
CString formatStr=sbuf;
int startIndexRemove=0,endIndexRemove=0;
while(startIndexRemove=formatStr.Find("第")!=-1)
{
endIndexRemove=formatStr.Find(":");
formatStr.Delete(startIndexRemove-1,endIndexRemove-startIndexRemove+3);
// 以下測試搜索的結果:搜索到的‘第’的位置卻都是 1 !
// 搜索到的‘:’的位置是 對的 !
// char show[10];
// sprintf(show,"%d,%d",startIndexRemove,endIndexRemove);
// MessageBox(show);
// MessageBox(formatStr);
}
*/
SetDlgItemText(IDC_EDIT3,sbuf);
}
void CAccessSectorDlg::OnButton6()
{
// TODO: Add your control notification handler code here
/*
UpdateData();
//UpdateData(FALSE);
GetDlgItemText(IDC_EDIT4,device);
GetDlgItemText(IDC_EDIT7,filename);
//MessageBox(filename);
doAbsoluteSector(GENERIC_READ,device,filename);
*/
if(stop==TRUE) stop=FALSE;
cmd=2;
AfxBeginThread(doThread,this);
}
void CAccessSectorDlg::OnButton7()
{
// TODO: Add your control notification handler code here
if(MessageBox("NOTICE!",0,MB_YESNO|MB_ICONEXCLAMATION)!=IDYES) return;
GetDlgItemText(IDC_EDIT4,device);
GetDlgItemText(IDC_EDIT7,filename);
//doAbsoluteSector(GENERIC_WRITE,device,filename);
}
void CAccessSectorDlg::showSector(BYTE *buf, DWORD showSect)
{
char *buf2=(char *)buf;
char add[64];
UINT count;
memset(sbuf,0,51200);
memset(add,0,64);
if(showSect>10) count=5120;
else count=showSect*512;
char text[17];
memset(text,0,17);
text[0]=' ';
for(unsigned int i=0;i<=count;i++)
{
if(i == 0)
sprintf(add,"第 %d 扇區:\r\n %08X : %02X ",i/512,i,buf[i]);
//else if(i % 512 == 0)
//sprintf(add,"\r\n第 %d 扇區:\r\n%02X ",i/512,buf[i]);
else if((i==8) || i % 16 ==8)
sprintf(add,"- %02X ",buf[i]);
else if( (i>0) && (i % 16 ==0))
{
memcpy(text+1,buf2+(i-16),16);
for(int j=1;j<17;j++)
{
if(text[j]==0x00 || text[j]==0xFF || text[j]==0x0D || text[j]==0x0A || text[j]==0x09 || text[j]==0x08 || text[j]==0x0C) text[j]='.';
// sprintf(text+j,"%c",text[j]);
//itoa((buf+(i-16))[j],&text[j+1],2);
}
memcpy(add,text,17);
if(i==count) goto end;
if(i % 512 == 0)
sprintf(add+17,"\r\n第 %d 扇區:\r\n %08X : %02X ",i/512,i,buf[i]);
else sprintf(add+17,"\r\n %08X : %02X ",i,buf[i]);
}
else sprintf(add,"%02X ",buf[i]);
//m_edit=sbuf;
end :
strcat(sbuf,add);
memset(add,0,64);
}
/*
{
if(i == 0)
sprintf(sbuf,"%s第 %d 扇區:\r\n%02X ",sbuf,i/512,buf[i]);
else if(i % 512 == 0)
sprintf(sbuf,"%s\r\n第 %d 扇區:\r\n%02X ",sbuf,i/512,buf[i]);
else if((i==8) || i % 16 ==8)
sprintf(sbuf,"%s- %02X ",sbuf,buf[i]);
else if( (i>0) && (i % 16 ==0))
sprintf(sbuf,"%s\r\n%02X ",sbuf,buf[i]);
else sprintf(sbuf,"%s%02X ",sbuf,buf[i]);
//m_edit=sbuf;
}
*/
SetDlgItemText(IDC_EDIT3,sbuf);
//UpdateData(FALSE);
//MessageBox("完成");
}
UINT doThread(LPVOID var)
{
DWORD deviceGeneric,fileGeneric,open;
HANDLE readHandle,writeHandle;
__int64 startSector=0;
CAccessSectorDlg *p=(CAccessSectorDlg *)var;
((CProgressCtrl *)p->GetDlgItem(IDC_PROGRESS1))->SetPos(0);
p->SetDlgItemText(IDC_EDIT3,NULL);
//if(p->stop==TRUE) p->stop=FALSE;
p->UpdateData();
p->GetDlgItemText(IDC_EDIT4,p->device);
p->GetDlgItemText(IDC_EDIT7,p->filename);
CString device=p->device;
startSector=_atoi64((char *)(LPCSTR)p->startSector);
//DWORD sectorLow=startSector;
//DWORD sectorHigh=startSector>>32;
//CString test;
//test.Format("%x,%x",*(LONG *)(&startSector>>32),startSector);
//p->MessageBox(test);
//return 0;
//DWORD startSector=p->startSector;
DWORD sectorToDo=p->sectorToDo;
CString filename=p->filename;
//memset(sbuf,0,51200);
//SetDlgItemText(IDC_EDIT3,NULL);
//GetDlgItem(IDC_EDIT3)->UpdateData();
//CString comp="0";
//ULONG c=512*100;
BYTE buf[512000];
__int64 startbPos=startSector*512;
//DWORD bCountToDo=sectorToDo*512;
/*
CString test;
test.Format("%lu,%lu",sectorToDo,bCountToDo);
MessageBox(test);
*/
DWORD bufCount=sectorToDo/1000;
DWORD restSector=sectorToDo%1000;
DWORD sectorDone=0;
HANDLE hDev=0;
HANDLE hFile=0;
DWORD doWriteSector=0;
DWORD bDone;
//CClientDC dc(p);
//CString j;
//CString doneSector("");
if(device.IsEmpty()==TRUE)
{p->MessageBox("請輸入設備名。"); return 0;}
if(sectorToDo==0)
{p->MessageBox("讀取的扇區數不能為0。"); return 0;}
if(p->cmd==3 && (filename.IsEmpty()==TRUE))
{p->MessageBox("請輸入文件名。"); return 0;}
//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;
if(p->cmd==2)
{
deviceGeneric=GENERIC_READ;
fileGeneric=GENERIC_WRITE;
open=CREATE_ALWAYS;
}
else if(p->cmd==3)
{
deviceGeneric=GENERIC_WRITE;
fileGeneric=GENERIC_READ;
open=OPEN_EXISTING;
}
hDev=CreateFile(device,deviceGeneric,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
if(hDev==INVALID_HANDLE_VALUE)
{p->MessageBox("打開硬盤失敗。"); return 0;}
DWORD sectorHigh=startbPos>>32;
// SetFilePointer(readHandle,startbPos,(LONG *)§orHigh,FILE_BEGIN);
if(filename.IsEmpty()!=TRUE)
{
hFile=CreateFile(filename,fileGeneric,FILE_SHARE_WRITE,0,open,FILE_ATTRIBUTE_NORMAL,0);
if(hFile==INVALID_HANDLE_VALUE)
{p->MessageBox("文件錯誤。"); CloseHandle(hDev); return 0;}
}
if(p->cmd==2)
{
readHandle=hDev;
writeHandle=hFile;
}
else if(p->cmd==3)
{
readHandle=hFile;
writeHandle=hDev;
}
SetFilePointer(readHandle,startbPos,(LONG *)§orHigh,FILE_BEGIN);
ReadFile(readHandle,buf,512000,&bDone,0);
memcpy(p->comp,buf,51200);
p->showSector(buf,sectorToDo);
p->SetDlgItemText(IDC_EDIT3,p->sbuf);
if(filename.IsEmpty()==TRUE)
{
CloseHandle(hDev); return 0;
}
//HANDLE hFile=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
//CloseHandle(hFile);
//如果得到的句柄有效,就可以使用ReadFile來讀取了,
//while()
//{
if(sectorToDo<1000) doWriteSector=sectorToDo;
else doWriteSector=1000;
//CClientDC dc(this);
// dc.SetBkMode(TRANSPARENT);
//dc.SetBkMode(TRANSPARENT);
//doneSector.Format("已處理 扇區");
//dc.TextOut(63,270,doneSector);
//dc.SetBkMode(OPAQUE);
//dc.SetBkColor(NULL_PEN);
//dc.GetCurrentBrush()->
if(p->cmd==3)
{
if((p->MessageBox("這是不可逆的!確定寫入嗎?",NULL,MB_OKCANCEL|MB_ICONWARNING))!=MB_OKCANCEL)
{CloseHandle(hFile);
CloseHandle(hDev);
return 0;
}
}
for(DWORD i=0;sectorDone<sectorToDo;i++)
{
if(p->stop==TRUE)
{
((CProgressCtrl *)p->GetDlgItem(IDC_PROGRESS1))->SetPos(0);
CloseHandle(hFile);
CloseHandle(hDev);
DeleteFile(filename);
return 0;
}
sectorHigh=startbPos>>32;
SetFilePointer(readHandle,startbPos,(LONG *)§orHigh,FILE_BEGIN);
ReadFile(readHandle,buf,512000,&bDone,0);
//SetFilePointer(hFile,i*512000,NULL,FILE_BEGIN);
if(i>=bufCount) doWriteSector=restSector;
WriteFile(writeHandle,buf,doWriteSector*512,&bDone,0);
sectorDone+=doWriteSector;
startbPos+=512000;
//doneSector.Format("%d",sectorDone);
//dc.TextOut(120,270,doneSector);
((CProgressCtrl *)p->GetDlgItem(IDC_PROGRESS1))->SetPos(sectorDone*100/sectorToDo);
//MessageBox("doneSector");
//Invalidate(FALSE);
//CloseHandle(hFile);
//if(j>=bufCount) return;
}
CloseHandle(hFile);
CloseHandle(hDev);
p->MessageBox("完成。");
/*
for(DWORD i=0;i<1000000;i++)
{
if(((CAccessSectorDlg*)var)->stop==TRUE) return 0;
j.Format("%d",i);
dc.TextOut(40,300,j);}
*/
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -