ReadFile

I'm trying to read the entire disk drive.

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!
[3073 byte] By [gabirata] at [2007-11-20 9:57:13]
# 1 Re: ReadFile
ReadFile is used to read file not device. If you want to read the contents of
device, use File Management Control Codes of DeviceIoControl() instead.
henky@nok.co.id at 2007-11-9 13:31:39 >
# 2 Re: ReadFile
With a small modification in the CreateFile function I can read the contents of the disk in the Windows XP. I add the GENERIC_READ access rights and it works fine, but under Windows Vista the CreateFile function fails and GetLastError() returns "Access Denied". I think that I need to do something more in Windows Vista, but I don't know what!
gabirata at 2007-11-9 13:32:39 >
# 3 Re: ReadFile
With a small modification in the CreateFile function I can read the contents of the disk in the Windows XP. I add the GENERIC_READ access rights and it works fine, but under Windows Vista the CreateFile function fails and GetLastError() returns "Access Denied". I think that I need to do something more in Windows Vista, but I don't know what!

The "Access Denied" error is related to security. You need to get the privilege
to access the resource. But, I'm very sorry i never touch the Vista :cry:
Probably this link will inform you something:

http://www.dev-archive.com/cpp/security/article.php/c13695/
henky@nok.co.id at 2007-11-9 13:33:48 >