我有一个线程不断地寻找新的数据,如果数据还没有在串行缓冲区中,ReadFile和GetOverlappedResult似乎告诉我有数据,它读取了数据,但没有传输到我的缓冲区.
func read()
{
if(state == 0)
{
memset(bytes, '\0', sizeof(amount_to_read));
readreturn = ReadFile(h, bytes, amount_to_read,NULL, osReader);
if(readreturn <= 0)
{
errorcode = GetLastError();
if(errorcode != ERROR_IO_PENDING)
{
SetEAIError(ERROR_INTERNALERROR);
return -1;
}
}
}
if (GetOverlappedResult(h, osReader, &dwRead, FALSE) == false)
{
errorcode = GetLastError();
if (errorcode == ERROR_IO_INCOMPLETE || errorcode == 0)
{
if(dwRead > 0)
{
return 1;
}
//timeout
SetEAIError(ERROR_EAITIMEOUT);
return -1;
}
else
{
//other error
SetEAIError(ERROR_WIN_ERROR);
return -1;
}
}
else
{
//read succeded, check if we read the amount required
if(dwRead != amount_to_read)
{
if(dwRead == 0)
{
//nothing read, treat as timeout
SetEAIError(ERROR_EAINOREAD);
return -1;
}
else
{
//memcpy_s(bytes, sizeof(bytes), readbuf, dwRead);
SetEAIError(ERROR_PARTIALREAD);
*_bytesRead = dwRead;
return -1;
}
}
else
{
if(strlen((char*)bytes) == 0)
{
//nothing read, treat as timeout
SetEAIError(ERROR_EAINOREAD);
return -1;
}
//memcpy_s(bytes, sizeof(bytes), readbuf, dwRead);
*_bytesRead = dwRead;
return 0;
}
}
}这就是错误代码的含义:
GetOverlappedResult。如果我用GetOverlappedResult来阻止(传递为真),它每次都会起作用。如果我把我的线程切换到只有当我知道那里有数据时,它工作每次。
但是,如果没有数据,当有数据时,它似乎“丢失”了数据,我的读量参数dwRead显示正确的读取字节数(可以通过端口监视器看到它),但是字节没有存储在我的char*中。
我经常得到ERROR_EAINOREAD
我做错了什么?
我不想使用标志,我只想使用ReadFile和GetOverlappedResult,我应该能够用我的代码.我想
发布于 2012-11-28 20:04:39
问题就在于数据丢失的原因.它丢失的原因是传入readfile的字节参数是父线程中的局部变量。由于它是本地的,所以在我再次进入读之后,跳过readfile并转到重叠的结果,我现在可能正在处理一个不同的内存区域。
https://stackoverflow.com/questions/13609037
复制相似问题