我只想使用函数 CreateFile 读取文件,如下所示:
#include <iostream>
#include <windows.h>
#include <stdio.h>
int main(int argc, char **argv)
{
PIMAGE_DOS_HEADER pImageDosHeader;
HANDLE hFile = NULL;
HANDLE hMapObject;
PUCHAR uFileMap;
if (argc < 2)
return (-1);
std::cout << "hFile=" << hFile << std::endl;
if (!(hFile = CreateFile((LPCWSTR)argv[1], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)))
{
return (-1);
} else {
std::cout << argv[1] << std::endl;
std::cout << "hFile=" << hFile << std::endl;
getchar();
}
return (0);
}
问题是输出如下:
hFile=000000 (the pointer is initialized to NULL -> OK)
hFile=FFFFFF (invalid pointer)
请问有人能帮帮我吗? 预先感谢您的帮助。
最佳答案
以下内容:
if (!(hFile = CreateFile(...)))
不是检查错误的方式。
来自documentation :
If the function fails, the return value is
INVALID_HANDLE_VALUE
. To get extended error information, callGetLastError
.
INVALID_HANDLE_VALUE
是 -1
,或十六进制的 0xFFFFFFFFF
。您需要调用 GetLastError()
来查明发生了什么。
关于c++ - CreateFile() 返回地址为 FFFFFF 的无效指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15273270/