C++ - Read Bytes of Hard Drive
C++ - Read Bytes of Hard Drive
C++ - Read Bytes of Hard Drive
Using the hex editor HxDen one can read (and edit) the bytes on the hard drive or a USB key or the RAM. That is, one can read/change the first
byte on the hard disk.
I understand how to read the bytes from a file using C++, but I was wondering how one might do this for the hard disk.
To make it simple, given a positive integer n, how can I read byte number n on the hard drive using C++? (I would like to do C++, but if there is
an easier way, I would like to hear about that.)
@Half_Baked He's on Windows 7 (and yes, that matters) – sehe Dec 22 '13 at 1:54
@sehe: You might say the advice was... half baked? – Joe Z Dec 22 '13 at 1:55
I edited the question after Half_Baked wrote his comment.... (sorry) – Thomas Dec 22 '13 at 1:55
1 Answer
It is documented in the MSDN Library article for CreateFile, section "Physical Disks and
Volumes". This code worked well to directly read the C: drive:
LARGE_INTEGER position = { 0 };
BOOL ok = SetFilePointerEx(hdisk, position, nullptr, FILE_BEGIN);
assert(ok);
BYTE buf[65536];
DWORD read;
ok = ReadFile(hdisk, buf, 65536, &read, nullptr);
assert(ok);
// etc..
Admin privileges are required, you must run your program elevated on Win7 or you'll get error
5 (Access denied).
Join Stack Overflow to learn, share knowledge, and build your career. Email Sign Up OR SIGN IN WITH Google Facebook
https://stackoverflow.com/questions/20725397/read-bytes-of-hard-drive 1/1