ReadFile
So I used the following code:
BOOL GetDriveGeometry(DISK_GEOMETRY *pdg, HANDLE hDevice)
{
BOOL bResult; // results flag
DWORD junk; // discard results
bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
NULL, 0, // no input buffer
pdg, sizeof(*pdg), // output buffer
&junk, // # bytes returned
(LPOVERLAPPED) NULL); // synchronous I/O
return (bResult);
}
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
long tempo;
char buffer[8192] = {0};
ULLONG hd_size;
HANDLE hDevice; // handle to the drive to be examined
ULLONG posicao;
LARGE_INTEGER liPos;
int minutos = 1, ciclos = 1;
srand(time(0));
tempo = minutos * ciclos * 60 + time(0);
DISK_GEOMETRY pdg; // disk drive geometry structure
BOOL bResult; // generic results flag
hDevice = CreateFile(TEXT("\\\\.\\PhysicalDrive0"), // drive
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
MessageBox(NULL, "Can't Open Disk", "CreateFile", 0);
return 3;
}
bResult = GetDriveGeometry (&pdg, hDevice);
hd_size = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder * (ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
while(!abortar && tempo > time(0))
{
DWORD nBytesRead;
posicao = ((((ULLONG)rand() << 32) + rand()) % (hd_size / sizeof(buffer))) * sizeof(buffer);
liPos.QuadPart = posicao;
SetFilePointerEx(hDevice, liPos, NULL, FILE_BEGIN);
// Attempt a synchronous read operation.
bResult = ReadFile(hDevice,
buffer,
sizeof(buffer),
&nBytesRead,
(LPOVERLAPPED)NULL) ;
MessageBox(NULL, buffer, "Buffer", 0);
if(nBytesRead == 0)
{
// This is the end of the file.
if (bResult)
break;
else
return 3;
}
}
CloseHandle(hDevice);
if (abortar)
return -1;
return 0;
}
but ReadFile always fails. I want to do a synchronous read. I'm doing something wrong?
Thanks in advance!

