How to read the sectors of physical drive under win32?

The API ReadFile is most useful function, and with it, you can read the physical sectors directly.

Here is a sample function, BOOL ReadSectors(…)



BOOL ReadSectors(
BYTE bDrive, //drive index
DWORD dwStartSector, //start sector
WORD wSectors, //sectors number
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;
}

Tags: , , ,