c++ - 即使缓冲区被未决操作覆盖,WriteFile 也成功

标签 c++ c winapi named-pipes

在我的程序中(发布到此 question ),我在客户端进行了以下更改:

wretry:   
   cbToWrite = _stprintf(chBuf[0], TEXT("Message %d from Client"), retrycount - numberofsend + 1);
   cbToWrite *= sizeof(TCHAR);

   fSuccess = WriteFile(hPipe, chBuf[0], cbToWrite,
  &cbWritten, &woverlapped[retrycount-numberofsend]);

这样,现在每次写入都将使用相同的缓冲区。

在这种情况下,WriteFile 每次返回 ERROR_IO_PENDING 时都处于挂起状态。因为缓冲区的内容每次都会被覆盖,所以服务器应该收到写入缓冲区的最后一条消息。但是服务器正在毫无问题地接收消息。

这是否意味着消息首先被复制到某个内部缓冲区中?我可以销毁缓冲区并期望将消息传送到服务器吗?

最佳答案

再次引用MSDN:

lpBuffer [in] A pointer to the buffer containing the data to be written to the file or device. This buffer must remain valid for the duration of the write operation. The caller must not use this buffer until the write operation is completed.

因此,如果您在写入操作进行时弄乱了缓冲区,那么您的消息似乎无法保证安全到达。你绝对不应该破坏缓冲区

关于c++ - 即使缓冲区被未决操作覆盖,WriteFile 也成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23102845/

相关文章:

c++ - 从 VC++ 调用 R 函数

c++ - 如何知道两个 C 字符串是否指向一个内存块?

c++ - win32 GUI 的拖放支持

c++ - 使用C++ catch框架验证assert语句

c++ - 为什么乘以常数有符号整数分数没有优化?

c++ - 为什么 ICC 以这种方式展开这个循环并使用 lea 进行算术运算?

c - C 中的通用指针和内存分配

C - 调用函数并给出二维数组

c++ - Gif在闪烁

c++ - 当 hModule 和 lpProcName 有效时 GetProcAddress 返回 NULL