I'm trying to read bytes from a raw disk. (Windows 7, VS 2010.)
I get a value of -1 for hDisk (which I assume is an error).
How can I get the real data?
#include "stdafx.h"
#include <windows.h>
#include <WinIoCtl.h>
#include <stdio.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
printf("hello world\n");
DWORD nRead;
char buf[512];
HANDLE hDisk = CreateFile(L"\\\\.\\PhysicalDrive0",
GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL);
printf("hDisk %i\n", hDisk);
SetFilePointer(hDisk, 0xA00, 0, FILE_BEGIN);
ReadFile(hDisk, buf, 512, &nRead, NULL);
for (int currentpos=0;currentpos < 512;currentpos++) {
std::cout << buf[currentpos];
}
CloseHandle(hDisk);
std::cin.get();
return 0;
}
Your code should I think work (when run with elevated privileges) except that:
buf[]
isn't properly aligned.Physical Drive 0 is likely to be the system drive, and hence in use.
Here's some code I use for disk imaging. (Previously posted here.) Public domain, but no warranty, express or implied, etc. (Looking over the code I note that I haven't explicitly checked the sector size in order to make sure that the buffer and read/write operations are properly aligned. This shouldn't be a problem in practice because the largest sector size in common use is 4K and this is also the memory page size on Windows. However, in production code, you should always explicitly determine the sector size of the device in question and ensure proper alignment accordingly.)