我的代码旨在将原始数据的前512个字节转储到驱动器中。 CreateFileW非常适合打开手柄,我已经通过获取驱动器几何体来测试它,所以我知道这不是问题所在。 ReadFileEx声称它正在工作,GetLastError返回0,但缓冲区保持为空并且没有数据被读取。这是我的代码:
#define UNICODE 1
#define _UNICODE 1
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>
#define wszDrive L"\\\\.\\X:"
#define BUFFERSIZE 512
DWORD g_BytesTransferred = 0;
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 GetDriveHandle(LPWSTR wszPath)
{
HANDLE hDevice = INVALID_HANDLE_VALUE;
BOOL bResult = FALSE;
DWORD junk = 0;
hDevice = CreateFileW(wszPath,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
return (hDevice);
}
int wmain(int argc, wchar_t *argv[])
{
BOOL bResult = FALSE;
ULONGLONG DiskSize = 0;
char ReadBuffer[BUFFERSIZE] = { 0 };
OVERLAPPED ol = { 0 };
DWORD dwBytesRead = 0;
HANDLE hDevice = GetDriveHandle(wszDrive);
if (hDevice == INVALID_HANDLE_VALUE) {
printf("Error Opening Drive");
}
else {
printf("Drive Handle Opened. Starting Read.\n");
ReadFileEx(hDevice, ReadBuffer, BUFFERSIZE, &ol, FileIOCompletionRoutine);
if (dwBytesRead == 0) {
printf("No Bytes Read. Error: %08x.\n", GetLastError());
}
ReadBuffer[dwBytesRead] = '\0';
printf("%s", ReadBuffer);
}
CloseHandle(hDevice);
while (true) {
}
return ((int)bResult);
}
答案 0 :(得分:0)
关于输入句柄,ReadFileEx的文档说:
此参数可以是CreateFile函数使用FILE_FLAG_OVERLAPPED标志打开的任何句柄,也可以是套接字或接受函数返回的套接字句柄。
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365468(v=vs.85).aspx
但你似乎没有使用那面旗帜。
它还声明:
此句柄还必须具有GENERIC_READ访问权限。有关访问权限的更多信息,请参阅文件安全性和访问权限。