出于某种原因,当我尝试为驱动程序创建句柄时,我得到incorrect function.
(即错误消息1)。这是否意味着我使用错误的函数来创建句柄?我在这里做错了什么?
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if(errorMessageID == 0)
return std::string(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
int main()
{
HANDLE hDriver = CreateFileA("\\\\.\\myDevice", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0, OPEN_EXISTING, 0, 0);
cout << GetLastErrorAsString() << endl; //"Wrong function"
//hDriver == INVALID_HANDLE_VALUE
CloseHandle(hDriver);
return 0;
}