WIN API ReadFile()返回GetLastError()ERROR_INVALID_PARAMETER

用户名

我在下面编写了此代码,并使用mingw gcc 4.7在code :: blocks下很好地进行了编译。从那以后,我决定开始使用Visual Studio 2013 Express。现在,在调用ReadFile()时出现错误。这似乎是无效的参数。我看不到错误,希望有人能发现它。

所有这些都包装在类Serial中。从我在IDE中可以看到,与引用CreateFile()返回到句柄相比,m_hSerial的内存引用是正确的。

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;

}

大卫·赫弗南(David Heffernan)
ReadFile(cThis->m_hSerial, &szBuf, nSize, &dwBytesRead, &ovRead)

您既要进行异步操作,又要使函数告诉您已读取了多少字节。您将其&dwBytesRead作为倒数第二个参数传递当您执行重叠读取时,请传递NULL此参数。文档说:

如果这是异步操作,请对该参数使用NULL以避免潜在的错误结果。

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

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

OVERLAPPED ovRead = {};

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章