Read bytes of hard drive

橙三吉。 提交于 2021-02-08 10:37:25

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/20725397/read-bytes-of-hard-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!