WIN API ReadFile()返回GetLastError()ERROR_INVALID_PARAMETER

时间:2015-02-04 15:49:40

标签: c++ winapi

我在下面编写了这段代码,它在code :: blocks下使用mingw gcc 4.7编译得很好。我已经决定开始使用Visual Studio 2013 express。现在,调用ReadFile()时出现错误。这似乎是一个无效的参数。我无法看到错误,希望有人能发现它。

这全部包含在一个Serial类中。从我在IDE中看到的情况来看,m_hSerial的内存引用与引用CreateFile()返回句柄相比是正确的。

m_hSerial = CreateFile(m_pchPort,
                       GENERIC_READ | GENERIC_WRITE,
                       0,
                       0,
                       OPEN_EXISTING,
                       FILE_FLAG_OVERLAPPED,
                       0);

我这样称呼WorkThread

m_hThread = (HANDLE)_beginthreadex(0, 0, &WorkThread, (void*) this, 0, 0);

这是WorkThread代码

unsigned int __stdcall Serial::WorkThread(void* pvParam)
{
// This is a pointer to the 'this' serial class.
// Needed to be able to set members of the class in a static class function
Serial * cThis = (Serial*) pvParam;
// Set up the overlapped event
OVERLAPPED ov;
memset(&ov, 0, sizeof(ov));
ov.hEvent = CreateEvent(0, true, 0, 0);
DWORD dwEventMask = 0;
DWORD dwWait;
HANDLE aHandles[2];
aHandles[0] = cThis->m_hThreadTerminator;
aHandles[1] = ov.hEvent;

SetEvent(cThis->m_hThreadRunning);
while (true)
{
    if (!WaitCommEvent(cThis->m_hSerial, &dwEventMask, &ov))
    {
        assert(GetLastError() == ERROR_IO_PENDING);

    }

    dwWait = WaitForMultipleObjects(2, aHandles, FALSE, INFINITE);
    switch(dwWait)
    {
        case WAIT_OBJECT_0:
        {
            _endthreadex(1);
        }
        case WAIT_OBJECT_0 + 1:
        {
            if (dwEventMask & EV_TXEMPTY)
            {
                ResetEvent(ov.hEvent);
            }
            else if (dwEventMask & EV_RXCHAR)
            {
                // read data here
                DWORD dwBytesRead = 0;
                DWORD dwErrors;
                COMSTAT cStat;
                OVERLAPPED ovRead;
                ovRead.hEvent = CreateEvent(0, true, 0, 0);

                // Get the Bytes in queue
                ClearCommError(cThis->m_hSerial, &dwErrors, &cStat);
                DWORD nSize = cStat.cbInQue;
                // EM_REPLACESEL needs a LPARAM null terminated string, make room and set the CString NULL
                char *szBuf = new char[nSize+1];
                memset(szBuf, 0x00, sizeof(szBuf));

                if (!ReadFile(cThis->m_hSerial, &szBuf, nSize, &dwBytesRead, &ovRead))
                    DWORD err = GetLastError();
                if (dwBytesRead == nSize)
                    SendMessage(cThis->m_hHwnd, WM_SERIAL, 0, LPARAM(&szBuf));

                CloseHandle(ovRead.hEvent); // clean up!!!
                delete[] szBuf;
            }
            // Reset the overlapped event
            ResetEvent(ov.hEvent);
        }
        break;
    }//switch
}

return 0;

}

1 个答案:

答案 0 :(得分:4)

ReadFile(cThis->m_hSerial, &szBuf, nSize, &dwBytesRead, &ovRead)

您要求进行异步操作,但也要求函数告诉您已读取了多少字节。您将&dwBytesRead作为倒数第二个参数传递。执行重叠读取时,请为此参数传递NULLdocumentation说:

  

如果这是一个异步操作,则为此参数使用NULL,以避免可能出现错误的结果。

在上面的代码中传递&szBuf也是错误的。你的意思是通过szBuf

您也无法初始化OVERLAPPED结构。这样做:

OVERLAPPED ovRead = {};

更大的问题是您要求异步访问,但随后编写代码,因为它是同步的。只要ReadFile返回,您就会尝试从dwBytesRead中获取有意义的信息,并关闭放入重叠结构中的事件。

如果您真的要异步编写代码,则需要重新编写代码以使其异步。从表面上看,您似乎还没有完全理解重叠I / O的含义,您应该切换到非重叠的同步I / O.