python读写磁盘扇区数据_C++-如何直接读取Windows磁盘扇
区的数据?
1.通过CreateFile系列来完成
读写扇区可以通过CreateFile打开磁盘逻辑分区,还要通过SetFilePointer以⽂件操作的⽅式把指针移到要操作的磁盘扇区开始处,在定位到要访问的扇区开始位置后就可以通过ReadFile或WriteFile函数实施相应的读写访问了,具体操作与⽂件读写并没有什么太⼤的差别。
⽰例代码:
BOOL CDirectAccessHDDlg::WriteSectors(BYTE bDrive, DWORD dwStartSector,
WORD wSectors, LPBYTE lpSectBuff)
// 对磁盘扇区数据的写⼊
{
if (bDrive == 0) return 0;
char devName[] = "\.\A:";
python怎么读的devName[4] ='A' + bDrive - 1;
HANDLE hDev = CreateFile(devName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (hDev == INVALID_HANDLE_VALUE) return 0;
SetFilePointer(hDev, 512 dwStartSector, 0, FILE_BEGIN);
DWORD dwCB;
BOOL bRet = WriteFile(hDev, lpSectBuff, 512 wSectors, &dwCB, NULL);
CloseHandle(hDev);
return bRet;
}
BOOL CDirectAccessHDDlg::ReadSectors(BYTE bDrive, DWORD dwStartSector,
WORD wSectors, LPBYTE lpSectBuff)
// 对磁盘扇区数据的读取
{
if (bDrive == 0) return 0;
char devName[] = "\.\A:";
devName[4] ='A' + bDrive - 1;
HANDLE hDev = CreateFile(devName, GENERIC_READ, FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hDev == INVALID_HANDLE_VALUE) return 0;
SetFilePointer(hDev, 512 dwStartSector, 0, FILE_BEGIN);
DWORD dwCB;
BOOL bRet = ReadFile(hDev, lpSectBuff, 512 wSectors, &dwCB, NULL); CloseHandle(hDev);
return bRet;
}
2.通过ReadSectors直接读写磁盘扇区数据
⽰例代码:
if (ReadSectors(uDiskID, m_uFrom, (UINT)dwSectorNum, bBuf) == FALSE) { MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_IConERROR); return;
}
//为了⽅便数据的显⽰,可做如下处理以完成格式转换等⼯作:
for (DWORD i = 0; i < dwSectorNum * 512; 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);
else if ((i % 16) == 7)
sprintf(cBuf, "%s- ", cBuf);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论