- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在下面编写了这段代码,它在 code::blocks 下运行良好,使用 mingw gcc 4.7 进行编译。从那以后,我决定开始使用 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);
这是工作线程代码
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;
最佳答案
ReadFile(cThis->m_hSerial, &szBuf, nSize, &dwBytesRead, &ovRead)
你要求一个异步操作,同时要求函数告诉你已经读取了多少字节。您传递了 &dwBytesRead
作为倒数第二个参数。当您执行重叠读取时,为此参数传递 NULL
。 documentation说:
Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.
在上面的代码中传递&szBuf
也是错误的。你的意思是传递 szBuf
。
您也无法初始化 OVERLAPPED
结构。这样做:
OVERLAPPED ovRead = {};
一个更大的问题是您请求异步访问,但随后编写的代码就像它是同步的一样。 ReadFile
返回后,您将尝试从 dwBytesRead
中获取有意义的信息,并关闭放置在重叠结构中的事件。
如果您真的打算异步编写代码,则需要重新编写异步代码。从表面上看,您似乎还没有完全理解重叠 I/O 的含义,您或许应该切换到非重叠、同步 I/O。
关于c++ - WIN API ReadFile() 返回 GetLastError() ERROR_INVALID_PARAMETER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28325589/
我正在尝试通过 wifi 连接将数据发送到接收器以进行实验。我设法使用适用于 wifi 的 Windows API Native 将卡连接到接收器。 我将卡的 GUID 存储在 pIfInfo 指针
我正在尝试使用 ReadFile 函数。这是我的代码: #define BUFFERSIZE 5 int main(int argc, char* argv[]) { OVERLAPPED o
我正在尝试执行 SetIpInterfaceEntry功能,但我总是得到 ERROR_INVALID_PARAMETER 结果。我想更改哪个确切参数并不重要,因为即使我不更改任何内容,我也会收到此错误
我使用 visual studio 2008 service pack1(9.0.30729.1)、windows 7 professional 64 位和 8 GB 内存运行我的应用程序 我的应用程
我的目标是从集群中获取文件名。为此,我使用函数 FSCTL_LOOKUP_STREAM_FROM_CLUSTER。使用它时,我收到错误 87,根据 MSDN,它代表 ERROR_INVALID_PAR
我正在尝试获取 betfair API使用 Windows API。 我无法尝试使用以下代码登录(用户名和密码已更改)。 char *headers = "X-Application: MakJhSA
在尝试读取和获取事件日志中的事件数时,出现错误 87。 #include #include #include #define PROVIDER_NAME "System" void wmain(
Windows 嵌入式紧凑型。 Trying to send and receive a message with MsgQueue. 看起来写入工作正常但读取给出了不正确的参数错误。 知道缺少什么吗
CryptHashData ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa380202(v=vs.85).aspx) 一直返
重现我的问题的示例代码: #include #include int main() { using namespace std; HANDLE hdl = CreateFile("test
此代码在 Windows 7 上启动时完全没问题: HANDLE hVol = CreateFile(L"\\\\.\\c:", GENERIC_WRITE | GENERIC_READ,
我为 NtWriteFile 做了一个简单的包装,但我遇到了来自 NtWriteFile 的错误。这是我的代码: BOOL WINAPI WriteFile(HANDLE hFile, PVOID l
DnsQueryConfig的文档在这里: https://docs.microsoft.com/en-us/windows/win32/api/windns/nf-windns-dnsqueryco
我一直在开发一个使用 winapi 获取管理员组成员的应用程序。为此,我使用了 NetLocalGroupGetMembers 方法。我的问题是当我尝试释放缓冲区的堆空间时,我从 NetApiBuff
我的程序创建了一个邮槽,但是当我调用 ReadFile 时,我得到了 ERROR_INVALID_PARAMETER。 参数不对怎么办? 程序代码: SECURITY_DESCRIPTOR sd;
我在调用 AttachThreadInput() 时得到一个 ERROR_INVALID_PARAMETER 错误代码。这是代码块: HWND hwnd = MYFINDWINDOW::FindWin
我正在尝试使用 LsaCallAuthenticationPackage 从缓存中清除特定票证。我总是在包状态中得到 ERROR_INVALID_PARAMETER 87。可能是什么原因?这是我的
我在下面编写了这段代码,它在 code::blocks 下运行良好,使用 mingw gcc 4.7 进行编译。从那以后,我决定开始使用 Visual Studio 2013 Express。现在调用
我正在调用 Windows 事件跟踪 StartTrace功能: StartTrace(sessionHandle, KERNEL_LOGGER_NAME, sessionProperties); 失
我正在尝试使用 EvtQuery 读取 Windows 事件日志和 winapi crate . 我收到系统错误 87 - ERROR_INVALID_PARAMETER fn to_vec(str:
我是一名优秀的程序员,十分优秀!