微信口令-udp

writefile
2023年4月1日发(作者:电脑无法启动)

python读写磁盘扇区数据_C++-如何直接读取Windows磁盘扇

区的数据?

1.通过CreateFile系列来完成

读写扇区可以通过CreateFile打开磁盘逻辑分区,还要通过SetFilePointer以⽂件操作的⽅式把指针移到要操作的磁盘扇区开始处,在定位

到要访问的扇区开始位置后就可以通过ReadFile或WriteFile函数实施相应的读写访问了,具体操作与⽂件读写并没有什么太⼤的差别。

⽰例代码:

BOOLCDirectAccessHDDlg::WriteSectors(BYTEbDrive,DWORDdwStartSector,

WORDwSectors,LPBYTElpSectBuff)

//对磁盘扇区数据的写⼊

{

if(bDrive==0)return0;

chardevName[]=".A:";

devName[4]='A'+bDrive-1;

HANDLEhDev=CreateFile(devName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,

OPEN_EXISTING,0,NULL);

if(hDev==INVALID_HANDLE_VALUE)return0;

SetFilePointer(hDev,512dwStartSector,0,FILE_BEGIN);

DWORDdwCB;

BOOLbRet=WriteFile(hDev,lpSectBuff,512wSectors,&dwCB,NULL);

CloseHandle(hDev);

returnbRet;

}

BOOLCDirectAccessHDDlg::ReadSectors(BYTEbDrive,DWORDdwStartSector,

WORDwSectors,LPBYTElpSectBuff)

//对磁盘扇区数据的读取

{

if(bDrive==0)return0;

chardevName[]=".A:";

devName[4]='A'+bDrive-1;

HANDLEhDev=CreateFile(devName,GENERIC_READ,FILE_SHARE_WRITE,

NULL,OPEN_EXISTING,0,NULL);

if(hDev==INVALID_HANDLE_VALUE)return0;

SetFilePointer(hDev,512dwStartSector,0,FILE_BEGIN);

DWORDdwCB;

BOOLbRet=ReadFile(hDev,lpSectBuff,512wSectors,&dwCB,NULL);

CloseHandle(hDev);

returnbRet;

}

2.通过ReadSectors直接读写磁盘扇区数据

⽰例代码:

if(ReadSectors(uDiskID,m_uFrom,(UINT)dwSectorNum,bBuf)==FALSE){

MessageBox("所选磁盘分区不存在!","错误",MB_OK|MB_IConERROR);

return;

}

//为了⽅便数据的显⽰,可做如下处理以完成格式转换等⼯作:

for(DWORDi=0;i

sprintf(cBuf,"%s%02X",cBuf,bBuf[i]);

if((i%512)==511)

sprintf(cBuf,"%srn第%d扇区rn",cBuf,(int)(i/512)+m_uFrom);

if((i%16)==15)

sprintf(cBuf,"%srn",cBuf);

elseif((i%16)==7)

sprintf(cBuf,"%s-",cBuf);

}

更多推荐

writefile