使用 ReadFile WinAPI 读取 'Binary' 个文件
Read 'Binary' files with ReadFile WinAPI
当我尝试用 ReadFile()
Windows API 打开 '.exe' 文件时,它只是 return 文件的第 2 个字符,例如:MZ
这是我的代码:
#define BUFFERSIZE 5000
VOID CALLBACK FileIOCompletionRoutine(
__in DWORD dwErrorCode,
__in DWORD dwNumberOfBytesTransfered,
__in LPOVERLAPPED lpOverlapped
);
VOID CALLBACK FileIOCompletionRoutine(
__in DWORD dwErrorCode,
__in DWORD dwNumberOfBytesTransfered,
__in LPOVERLAPPED lpOverlapped)
{
_tprintf(TEXT("Error code:\t%x\n"), dwErrorCode);
_tprintf(TEXT("Number of bytes:\t%x\n"), dwNumberOfBytesTransfered);
g_BytesTransferred = dwNumberOfBytesTransfered;
}
HANDLE hFile;
DWORD dwBytesRead = 0;
char ReadBuffer[BUFFERSIZE] = { 0 };
OVERLAPPED ol = { 0 };
hFile = CreateFile(fullFilePath.c_str(), // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file
NULL); // no attr. template
ReadFileEx(hFile, ReadBuffer, BUFFERSIZE - 1, &ol, FileIOCompletionRoutine);
当我打印 ReadBuffer
时,它只是 MZ
(exe 文件)。
但使用:
std::ifstream file(argv[1], std::ios::in | std::ios::binary);
它工作得很好。
如何使用 ReadFile 读取二进制文件?
How Can I Read Binary files With ReadFile?
ReadFile
(和 ReadFileEx
)有效 'in binary mode'。您可以逐字节获取准确的文件内容,无需任何翻译。
您对 writing/printing 有疑问。这主要取决于您要写入的位置,但是对于在 C++ 中可能包含空值的输出(二进制)数据,请选择 write
方法
some_output_stream.write( buffer_ptr, num_bytes_in_buffer );
some_output_stream
应设置为二进制模式 (std::ios::binary)。如果没有此标志,所有值为 10 的字节都可以转换为对 13,10。
如果使用 C FILE 函数
fwrite( buffer_ptr, 1, num_bytes_in_buffer, some_output_file );
同样 some_output_file
必须采用二进制模式。
在某些情况下ios WriteFile
可以补充您对 ReadFile
的使用。
不是阅读的问题,是打印的问题。
您没有显示您的代码,但您可能正在尝试使用 printf
或类似的东西进行打印。 IOW,您将其打印为 C 字符串。
嗯,二进制数据包含 0,在这种情况下,前 3 个字节是 'M'、'Z'、'\0' - 打印为空终止字符串 "MZ".
如果你想看到有意义的二进制数据打印,你必须编写一个转换为每字节十六进制数的转换器:4D 5A 00
等等
当我尝试用 ReadFile()
Windows API 打开 '.exe' 文件时,它只是 return 文件的第 2 个字符,例如:MZ
这是我的代码:
#define BUFFERSIZE 5000
VOID CALLBACK FileIOCompletionRoutine(
__in DWORD dwErrorCode,
__in DWORD dwNumberOfBytesTransfered,
__in LPOVERLAPPED lpOverlapped
);
VOID CALLBACK FileIOCompletionRoutine(
__in DWORD dwErrorCode,
__in DWORD dwNumberOfBytesTransfered,
__in LPOVERLAPPED lpOverlapped)
{
_tprintf(TEXT("Error code:\t%x\n"), dwErrorCode);
_tprintf(TEXT("Number of bytes:\t%x\n"), dwNumberOfBytesTransfered);
g_BytesTransferred = dwNumberOfBytesTransfered;
}
HANDLE hFile;
DWORD dwBytesRead = 0;
char ReadBuffer[BUFFERSIZE] = { 0 };
OVERLAPPED ol = { 0 };
hFile = CreateFile(fullFilePath.c_str(), // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file
NULL); // no attr. template
ReadFileEx(hFile, ReadBuffer, BUFFERSIZE - 1, &ol, FileIOCompletionRoutine);
当我打印 ReadBuffer
时,它只是 MZ
(exe 文件)。
但使用:
std::ifstream file(argv[1], std::ios::in | std::ios::binary);
它工作得很好。 如何使用 ReadFile 读取二进制文件?
How Can I Read Binary files With ReadFile?
ReadFile
(和 ReadFileEx
)有效 'in binary mode'。您可以逐字节获取准确的文件内容,无需任何翻译。
您对 writing/printing 有疑问。这主要取决于您要写入的位置,但是对于在 C++ 中可能包含空值的输出(二进制)数据,请选择 write
方法
some_output_stream.write( buffer_ptr, num_bytes_in_buffer );
some_output_stream
应设置为二进制模式 (std::ios::binary)。如果没有此标志,所有值为 10 的字节都可以转换为对 13,10。
如果使用 C FILE 函数
fwrite( buffer_ptr, 1, num_bytes_in_buffer, some_output_file );
同样 some_output_file
必须采用二进制模式。
在某些情况下ios WriteFile
可以补充您对 ReadFile
的使用。
不是阅读的问题,是打印的问题。
您没有显示您的代码,但您可能正在尝试使用 printf
或类似的东西进行打印。 IOW,您将其打印为 C 字符串。
嗯,二进制数据包含 0,在这种情况下,前 3 个字节是 'M'、'Z'、'\0' - 打印为空终止字符串 "MZ".
如果你想看到有意义的二进制数据打印,你必须编写一个转换为每字节十六进制数的转换器:4D 5A 00
等等