HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile ); |
DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod); |
BOOL CDirectAccessHDDlg::WriteSectors(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_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; } |
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, "%s\r\n第%d扇区\r\n", cBuf, (int)(i / 512) + m_uFrom); if ((i % 16) == 15) sprintf(cBuf, "%s\r\n", cBuf); else if ((i % 16) == 7) sprintf(cBuf, "%s- ", cBuf); } |
![]() |
file.Open(fileDlg.GetPathName(), Cfile::modeCreate | Cfile::modeReadWrite); …… if (ReadSectors(uDiskID, m_uFrom, (UINT)dwSectorNum, bBuf) == FALSE) { MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_IConERROR); return; } file.Write(bBuf, dwSectorNum * 512); file.Close(); |
file.Open(fileDlg.GetPathName(), Cfile::modeReadWrite); DWORD dwSectorNum = file.GetLength(); if (dwSectorNum % 512 != 0) return; dwSectorNum /= 512; unsigned char* bBuf = new unsigned char[dwSectorNum * 512]; file.Read(bBuf, dwSectorNum * 512); if (WriteSectors(uDiskID, m_uFrom, (UINT)dwSectorNum, bBuf) == FALSE) { MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_IConERROR); return; } file.Close(); delete[] bBuf; |
unsigned char bBuf[512]; UINT i = 0; BOOL bRet = TRUE; while (m_bAllDisk){ memset(bBuf, 0xFF, sizeof(bBuf)); bRet = WriteSectors(uDiskID, i, 1, bBuf); memset(bBuf, 0, sizeof(bBuf)); bRet = WriteSectors(uDiskID, i, 1, bBuf); if (bRet == FALSE){ if (i == 0) MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_IConERROR); else MessageBox("磁盘数据擦除完毕!", "错误", MB_OK | MB_IConERROR); return; } i++; } |