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

发布于 2024-12-08 17:38:52 字数 1570 浏览 5 评论 0原文

我遇到的问题: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;
}

The problem I'm having: CreateFile returns a handle of 0x194. ReadFileEx is saying that this handle is invalid. (Error 6.) Any ideas? The argument passed in is "C:\testfile.txt", which is a valid text file I made in Notepad. Despite being a C++ programmer for the last 12 years, this is my first time writing anything with "windows.h" or threads.

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2024-12-15 17:38:52

我的猜测是改变
ReadFileEx(&文件,&缓冲区,1024,&重叠,NULL);

ReadFileEx(文件,&缓冲区,1024,&重叠,NULL);

My guess is to change
ReadFileEx(&file, &buffer, 1024, &overlapped, NULL);
to
ReadFileEx(file, &buffer, 1024, &overlapped, NULL);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文