为什么调用ReadFile()会返回错误代码1?

时间:2016-09-21 16:41:44

标签: c++ windows visual-studio io driver

目前,我正在尝试访问我的驱动程序并从中读取数据。根据MSDN,一种方法是调用CreateFile,传入驱动程序的PDO文件路径,并使用ReadFileWriteFile函数读取和写入分别到文件。

我的代码很简单:

int main()
{

    CHAR readBuffer[32];

    HANDLE handle = INVALID_HANDLE_VALUE;
    LPDWORD bytesRead = 0;

    // File path to my custom device. Valid path.
    LPCWSTR filePath = L"\\\\?\\GLOBALROOT\\Device\\00000010";

    // Obtain handle to Device File for custom driver
    handle = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE,
                    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
                    FILE_FLAG_NO_BUFFERING, NULL);

    DWORD error = GetLastError();
    if(handle != INVALID_HANDLE_VALUE)
    {
        cout << "File Exists! \n";
    }
    else
        cout << "Cannot Open File with error: " << error << " \n";

    // Call to CreateFile succeeds.
    // Below here is where I have the issues----------------------------------

    cout << "\nAttempting to Read 32 bits from opened file... \n";

    // Attempt to Read data from the device 
    BOOL success = ReadFile(handle, readBuffer, sizeof(readBuffer), bytesRead, NULL);
    if (success)
        cout << "Read File status is: " << success << " \n";
    else
    {
        error = GetLastError();
        cout << "Read File status is: " << success << " with error code: " << error << "\n";
    }
    system("pause");
    return 0;
}

如代码中所述,CreateFile函数成功打开相对于驱动程序的PDO。对于最后一个输出行,我得到了:

Read File status is: 0 with error code: 1

这意味着ReadFile功能未正确完成。在MSDN上,错误代码1为ERROR_INVALID_FUNCTION,在这种情况下,它与ReadFile的调用有关。

我知道有更好的&#34;这样做的方法,但我尝试访问我的驱动程序的方式应该是最简单的方法。另外,我试图在&#34;管理员模式&#34;中运行它。就像人们在其他类似帖子中建议的那样,但无济于事。

我对ReadFile函数的调用会导致什么?另外,导致这种模糊错误的原因(功能无效)?

提前感谢您抽出时间并帮助解决此问题。

0 个答案:

没有答案