问题描述
使用十六进制编辑器 HxDen 可以读取(和编辑)上的字节硬盘驱动器或 USB 密钥或 RAM.也就是说,可以读取/更改硬盘上的第一个字节.
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.
我了解如何使用 C++ 从文件中读取字节,但我想知道如何对硬盘执行此操作.
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.
为了简单起见,给定一个正整数 n,如何使用 C++ 读取硬盘驱动器上的字节数 n?(我想做 C++,但如果有更简单的方法,我想听听.)
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.)
我正在使用 MinGW 在 Windows 7 上,如果这很重要.
I am using MinGW on Windows 7 if that matters.
推荐答案
CreateFile,物理磁盘和卷"部分.这段代码可以很好地直接读取 C: 驱动器:
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..
需要管理员权限,您必须在 Win7 上运行提升的程序,否则您将收到错误 5(拒绝访问).
Admin privileges are required, you must run your program elevated on Win7 or you'll get error 5 (Access denied).
这篇关于读取硬盘字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!