gpt4 book ai didi

c++ - 使用来自 CreateFile 的有效句柄来自 ReadFileEx 的无效句柄错误

转载 作者:行者123 更新时间:2023-11-28 08:16:12 25 4
gpt4 key购买 nike

我遇到的问题: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++ - 使用来自 CreateFile 的有效句柄来自 ReadFileEx 的无效句柄错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7690734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com