资源简介
在操作硬盘的时候,我们需要直接读写硬盘扇区,这里提供C++写的readSectors和writeSectors函数,还有一个输出函数PrintInform,希望能为大家提供帮助
代码片段和文件信息
// WipeTest.cpp: implementation of the WipeTest class.
//
//////////////////////////////////////////////////////////////////////
#include “WipeTest.h“
#include
#include
#include
#include
using namespace std;
void PrintInform(LPBYTE pBuf UINT uSize) {
for(UINT i =1; i<=uSize; i++) {
printf(“%02X“ pBuf[i-1]);
if(i%16 == 0) {
printf(“\n“); //end of line when word number more than 16
}else if(i%8 == 0) {
printf(“ “); //print “ “ between every word
}else{
printf(“ “);
}//end of if...else if..
}//end of for
}//end of method PrintInform
BOOL readSectors( char pDiskPath[]BYTE bReadBuffer[]DWORD dwOffsetByte DWORD dNumberOfBytesToRead) {
HANDLE hFile = CreateFile(pDiskPath GENERIC_READ FILE_SHARE_READ NULL OPEN_EXISTING 0 NULL );
if(hFile == INVALID_HANDLE_VALUE){
MessageBox(0 “can‘t open the disk!“ 0 0);
return 0;
}//end of if
SetFilePointer(hFile dwOffsetByte *512 0 FILE_BEGIN);//
DWORD dwReadByte; //被读取的字节数
BOOL bRead = ReadFile(hFile bReadBuffer dNumberOfBytesToRead &dwReadByte NULL);
//cout << dwReadByte < if(dwReadByte == 0){
MessageBox(0 “read disk error!“ 0 0);
return 0;
} //end of if
CloseHandle(hFile);
return bRead;
}//end of method readSectors
BOOL writeSectors( char pDiskPath[] BYTE cWriteBuffer[]DWORD dwStartSector DWORD dNumberOfBytesToWrite){
HANDLE hFile = CreateFile(pDiskPath GENERIC_WRITE FILE_SHARE_READ NULL OPEN_EXISTING 0 NULL );
if(hFile == INVALID_HANDLE_VALUE){
MessageBox(0 “can‘t open the disk!“ 0 0);
return 0;
}//end of if
SetFilePointer(hFile dwStartSector * 512 0 FILE_BEGIN);
DWORD dwWriteByte; //被写入的字节数
BOOL bWrite = WriteFile(hFilecWriteBufferdNumberOfBytesToWrite &dwWriteByteNULL);
//cout << “****************“ << endl;
if(dwWriteByte == 0){
MessageBox(0 “Write disk error!“ 0 0);
return 0;
} //end of if
CloseHandle(hFile);
return bWrite;
}//end of method writeSectors
int main(int argcchar *argv){
char cFilePath[] = “\\\\.\\F:\\TTTT.txt“ ; //“\\\\.\\PhysicalDrive0“; //文件路径(一定要正确!)
char cPhisycalDiskPath[] = “\\\\.\\PhysicalDrive0“; //物理盘符路径
DWORD readOffSetSector = 0; //开始扇区
DWORD uSectorNum = 1; //预计读取的扇区总数
DWORD uReadSize = uSectorNum * 512; //预计读取的扇区总数的总大小(Byte)
DWORD uWriteSize = uSectorNum * 512; //预计写入的扇区总数的总大小(Byte)
BYTE bBuffer[512]; //数组存放DPT的内容
if(readSectors(cFilePathbBuffer readOffSetSector uReadSize)){
PrintInform(bBufferuReadSize);
cout << “read appoint sector success!“ <
}//end of if
memset(bBuffer5512);
if(writeSectors(cFilePathbBuffer readOffSetSector uReadSize)){
cout << “write oK!“ < }//*//*
return 0;
}//end of method main()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4306 2008-01-14 22:05 WipeTest\WipeTest.dsp
文件 539 2008-01-14 13:50 WipeTest\WipeTest.dsw
文件 58368 2008-05-29 12:33 WipeTest\WipeTest.ncb
文件 250 2008-05-29 12:32 WipeTest\WipeTest.plg
文件 732 2008-01-16 15:29 WipeTest\WipeTest.h
文件 2998 2008-04-08 12:48 WipeTest\WipeTest.cpp
文件 197632 2008-05-29 12:32 WipeTest\Debug\vc60.idb
文件 135168 2008-04-08 12:48 WipeTest\Debug\vc60.pdb
文件 0 2008-04-08 12:48 WipeTest\Debug\WipeTest.sbr
文件 229428 2008-04-08 12:48 WipeTest\Debug\WipeTest.exe
文件 1082368 2008-04-08 12:48 WipeTest\Debug\WipeTest.pdb
文件 6447432 2008-04-08 12:45 WipeTest\Debug\WipeTest.pch
文件 51571 2008-04-08 12:48 WipeTest\Debug\WipeTest.obj
文件 359248 2008-04-08 12:48 WipeTest\Debug\WipeTest.ilk
文件 2098176 2008-04-08 12:48 WipeTest\Debug\WipeTest.bsc
文件 53760 2008-05-29 12:33 WipeTest\WipeTest.opt
目录 0 2008-04-08 15:48 WipeTest\Debug
目录 0 2008-04-08 15:48 WipeTest
----------- --------- ---------- ----- ----
10721976 18
评论
共有 条评论