CreateFile因读取文本文件的INVALID_HANDLE_VALUE错误而失败

时间:2015-01-20 08:09:06

标签: file winapi

我正在尝试在Win32应用中阅读一个简单的文本文件。但我在以下代码

中收到INVALID_HANDLE_VALUE错误
void Avatar::readData(LPCSTR filename)
{
    LPVOID data[4096] = {};
    HANDLE hFile;
    MessageBox(NULL, (LPCWSTR)filename, L"File", MB_OK);
    hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        MessageBox(NULL, L"Failed to CreateFile - 'hFile'", L"ERROR", MB_OK);
    }

    DWORD fileSize = GetFileSize(hFile, &fileSize);
    DWORD read = -1;
    if (!ReadFile(hFile, static_cast< LPVOID >(&data), 4096, &read, NULL))
    {
        DWORD err = GetLastError();
        MessageBox(NULL, L"Failed to ReadFile - 'hFile'", L"ERROR", MB_OK);
    }
    CloseHandle(hFile);
}

任何人都可以帮我解决吗?

0 个答案:

没有答案