Nothing Special   »   [go: up one dir, main page]

C++ - Read Bytes of Hard Drive

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

12/28/2017 c++ - Read bytes of hard drive - Stack Overflow

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.)

I am using MinGW on Windows 7 if that matters.

c++ windows hard-drive read-write

edited May 23 at 10:29 asked Dec 22 '13 at 1:47


Community ♦ Thomas
1 1 530 2 10 29

Maybe this will help.. stackoverflow.com/questions/7289453/… – Half_Baked Dec 22 '13 at 1:50

@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

I'd start with `\\physicaldevice` (search: stackoverflow.com/search?q=physicaldevice) – sehe Dec 22 '13 at


1:58

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:

HANDLE hdisk = CreateFile(L"\\\\.\\C:",


GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
0, NULL);
if (hdisk == INVALID_HANDLE_VALUE) {
int err = GetLastError();
// report error...
return -err;
}

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).

answered Dec 22 '13 at 2:29


Hans Passant
732k 100 1166
1876

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

You might also like