ReadFileEx使用CreateFile的有效句柄时出现无效句柄错误
Invalid handle error from ReadFileEx using valid handle from CreateFile
我遇到的问题:CreateFile返回0x194的句柄。ReadFileEx说这个句柄无效。(错误6)。什么好主意吗?传入的参数是"C:testfile.txt",这是我在记事本中创建的有效文本文件。尽管我做了12年的c++程序员,但这是我第一次用"windows.h"或线程写东西。
#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
using namespace System;
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("To display a file, you must enter the filename as the only command argument.");
scanf("n");
return 0;
}
HANDLE file;
int nameLen = (strlen(argv[1]) + 1);
wchar_t *filename = new wchar_t[nameLen];
if (filename == 0)
{
printf("To display a file, you must enter the filename as the only command argument.");
scanf("n");
return 0;
}
memset(filename, 0, nameLen);
::MultiByteToWideChar(CP_ACP, NULL, argv[1], -1, filename, nameLen);
file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == NULL)
{
printf("A valid filename is required.");
scanf("n");
return 0;
}
char *buffer;
buffer = new char[1024];
OVERLAPPED overlapped;
overlapped.Offset = overlapped.OffsetHigh = 0;
ReadFileEx(file, &buffer, 1024, &overlapped, NULL);
WaitForSingleObject(&file, 0);
if (GetLastError() != ERROR_SUCCESS)
{
printf("A valid file is required., Error: %d", GetLastError());
scanf("n");
return 0;
}
printf("%s", buffer);
scanf("n");
delete buffer;
return 0;
}
我的猜测是改变ReadFileEx(&file, &buffer, 1024, &overlapped, NULL);
到ReadFileEx(file, &buffer, 1024, &overlapped, NULL);
相关文章:
- 枚举进程模块在有效句柄上返回无效句柄
- 未知C++错误:致命错误:glibc检测到无效的stdio句柄
- 获取进程内存信息失败,错误 6 句柄无效
- 发布信号量返回错误 6(无效句柄)
- 如何测试 USB HID 句柄由于设备被重新插入而无效
- 为什么错误1400无效的窗口句柄
- 打电话给谁是无效的关闭手(句柄)是安全的吗?
- 在列表视图中启用分组 - 无效句柄
- DirectShow BindToObject返回无效句柄
- DoModal 返回 -1 Getlasterror 说句柄无效
- C++RegOpenKeyEx-错误6:句柄无效
- 错误 6 句柄在 Windows 上写入文件无效
- 错误 1400 无效的窗口句柄
- 关闭句柄调用上的句柄异常无效
- 调用IOCTLSTORAGE_GET_MEDIA_SERIAL_NUMBER时句柄无效
- 无效句柄(使用应用程序验证程序)
- AssignProcessToJobObject失败句柄无效
- QTcpSocket -退出QRunnable时指定的句柄无效
- 为什么未初始化的返回值会导致createwindowwex的无效窗口句柄错误
- Qt 5.8 windows打印机api无效句柄错误