PeekNamedPipe: 将数据从命名管道或匿名管道复制到缓冲区中,而不将其从管道中删除。它还返回有关管道中数据的信息。

示例:

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std; int main(int argc, const char** argv)
{
wcout << "Creating an instance of a named pipe..." << endl; // Create a pipe to send data
HANDLE pS = CreateNamedPipe(L"\\\\.\\pipe\\my_pipe", PIPE_ACCESS_DUPLEX, 0, 1, 100, 100, 0, NULL); // Open the named pipe
HANDLE pC = CreateFile(L"\\\\.\\pipe\\my_pipe", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (pC == INVALID_HANDLE_VALUE)
{
wcout << "Failed to connect to pipe." << endl;
return 1;
} wcout << "Test PeekNamedPipe #1." << endl;
DWORD PipeByteNum = 0;
BOOL res = PeekNamedPipe(pC, NULL, 0, NULL, &PipeByteNum, NULL);
if (!res)
{
wcout << "PeekNamedPipe() - failed." << endl;
return 1;
}
wcout << " - Number of bytes in pipe: " << PipeByteNum << endl << endl; wcout << "Sending data to pipe..." << endl; const wchar_t* data = L"Hello Pipe World";
DWORD numBytesWritten = 0;
BOOL result = WriteFile(pS, data, wcslen(data) * sizeof(wchar_t), &numBytesWritten, NULL); if (result)
wcout << "Number of bytes sent: " << numBytesWritten << endl;
else
{
wcout << "Failed to send data." << endl;
return 1;
} wcout << "Test PeekNamedPipe #2." << endl;
PipeByteNum = 0;
res = PeekNamedPipe(pC, NULL, 0, NULL, &PipeByteNum, NULL);
if (!res)
{
wcout << "PeekNamedPipe() - failed." << endl;
return 1;
}
wcout << " - Number of bytes in pipe: " << PipeByteNum << endl << endl; wcout << "Reading data from pipe..." << endl; // The read operation will block until there is data to read
wchar_t buffer[128];
DWORD numBytesRead = 0;
result = ReadFile(pC, buffer, 5 * sizeof(wchar_t), &numBytesRead, NULL); if (result)
{
buffer[numBytesRead / sizeof(wchar_t)] = '\0'; // null terminate the string
wcout << "Number of bytes read: " << numBytesRead << endl;
wcout << "Message: " << buffer << endl;
}
else
{
wcout << "Failed to read data from the pipe." << endl;
return 1;
} wcout << "Test PeekNamedPipe #3." << endl;
PipeByteNum = 0;
DWORD bytesAvail = 0;
BOOL isOK = PeekNamedPipe(pC, NULL, 0, NULL, &bytesAvail, NULL);
// Allocate buffer and peek data from pipe
DWORD bytesRead = 0;
std::vector<wchar_t> buffer_(bytesAvail);
isOK = PeekNamedPipe(pC, &buffer_[0], bytesAvail, &bytesRead, NULL, NULL);
if (!isOK)
{
wcout << "PeekNamedPipe() - failed." << endl;
return 1;
}
wcout << " - Number of bytes in pipe: " << bytesRead << endl << endl; // Close client.
CloseHandle(pC);
wcout << "Done with client." << endl; // Close the pipe.
CloseHandle(pS);
wcout << "Done with server." << endl; return 0;
}

ReadFile会先从管道内读取5*sizeof(wchar_t)大小的字节,然后PeekNamedPipe会读取剩下的字节。最后缓存区buffer_会打印“ Pipe World”

win32 - PeekNamedPipe的用法的更多相关文章

  1. Win32 配置文件用法

    #include "stdafx.h"#include <Shlobj.h>#include <Shlwapi.h> #pragma comment(lib ...

  2. C# 调Win32 API SendMessage简单用法及wMsg常量

    函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.该函数是应用程序和应用程序之间进行消息传递的主要手段之一.     函数原型:LRESU ...

  3. win32 TreeCtrl控件通知消息, LVN_SELCHANGED和LVN_ITEMCHANGED用法

    今天出了个奇怪的问题,当我在主窗口上创建一个用模板对话框的子窗口时, 在子窗口上放的TreeCtrl控件不响应LVN_SELCHANGED消息,也是晕死了, 我以为是消息捕获的问题,我在主窗口上也捕获 ...

  4. cocos2d-x 从win32到android移植的全套解决方案

    引言:我们使用cocos2d-x引擎制作了一款飞行射击游戏,其中创新性地融入了手势识别功能.但是我们在移植过程中遇到了很多的问题,同时也发现网上的资料少而不全.所以在项目行将结束的时候,我们特地写了这 ...

  5. 自动创建WIN32下多级子目录的C++封装类

            这是 WIN32 自动创建多级子目录的 C++ 封装类,用法简单.         封装没有采用类的静态函数方式,而是在构造函数里面直接完成工作.没什么具体的原因,只是当时做成这样了, ...

  6. sprintf()函数的用法

    Visual C++ sprintf()函数用法 转:http://blog.csdn.net/masikkk/article/details/5634886 在将各种类型的数据构造成字符串时,spr ...

  7. Delphi常用关键字用法详解

    本文详细介绍了Delphi中常用的各个关键字名称及用法,供大家在编程过程中借鉴参考之用.详情如下: absolute: ? 1 2 3 4 5 6 7 8 9 10 //它使得你能够创建一个新变量, ...

  8. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  9. 一步一步了解Cocos2dx 3.0 正式版本开发环境搭建(Win32/Android)

    cocos2d-x 3.0发布有一段时间了,作为一个初学者,我一直觉得cocos2d-x很坑.每个比较大的版本变动,都会有不一样的项目创建方式,每次的跨度都挺大…… 但是凭心而论,3.0RC版本开始 ...

  10. Delphi编程建议遵守的规范1---缩进、各种语句的用法

    在编程时候,尤其是在一个大的团队里面,遵守统一的编程规范是极其重要的.为所有的开发人员制定一个源代码书写标准,以及程序和文件的命名标准,使他们在编程时有一致的格式,这样,每个编程人员编写的代码能够被其 ...

随机推荐

  1. [转帖]linux中批量多行缩进与添加空格

    用vim打开修改python脚本的时候,将代码整体向后移动4个空格操作如下: ESC之后,ctrl+v进入多行行首选中模式 使用上下键进行上下移动,选中多行行首 shift+i,进入插入模式 连续敲击 ...

  2. TiKV占用内存超过的解决过程

    TiKV占用内存超过的解决过程 背景 为了后去TiDB的极限数据. 晚上在每台服务器上面增加了多个TiKV的节点. 主要方式为: 每个NVME的硬盘增加两个TiKV的进程. 这样每个服务器两个磁盘, ...

  3. ChatGPT 提高工作效率-一例SQL编写的过程

    ChatGPT 提高工作效率-一例SQL编写的过程 前言 遇到一个问题, 怀疑是有一些补丁没有被依赖. 导致第一次更新时没有更新这些没依赖的补丁. 后面更新时又更新了这些游离态的补丁. 导致出现 ol ...

  4. nginx日志定期备份清理的方法

    nginx日志定期备份清理的方法 前言 实在不想动不动就 yum install 也不太想因为一个很小的需求就搞一下ansible. 想着能够尽量简单, 尽量方便的进行一些工作. 具体思路就是 压缩, ...

  5. 【转帖】What are segfault rip/rsp numbers and how to use them

    https://stackoverflow.com/questions/1456899/what-are-segfault-rip-rsp-numbers-and-how-to-use-them   ...

  6. [专题]中立遭质疑,提价遭反对,ARM的生存难题怎么破?

    中立遭质疑,提价遭反对,ARM的生存难题怎么破? https://news.cnblogs.com/n/669715/ ARM税要提高.. RISC-V的机会? 文/黎文婕 来源:锌刻度(ID:znk ...

  7. CDP技术系列(一):使用bitmap存储数十亿用户ID的标签或群体

    一.背景介绍 CDP系统中目前存在大量由用户ID集合组成的标签和群体,截止当前已有几千+标签,群体2W+. 大量的标签都是亿级别数据量以上,例如性别.职业.学历等均,甚至有群体中的ID数量达到了数十亿 ...

  8. 文件上传change事件只执行一次的问题

    参考地址:https://blog.csdn.net/john_xiaoweige/article/details/81392110 本节中的注意点: this.$refs.attenceInput. ...

  9. 像elementui一样封装自定义按钮

    <template> <div> <button @click.prevent="coverHandler" class="btn-btn& ...

  10. [Go] string、int、int64相互转换

    import "strconv" //先导入strconv包 // string到int int, err := strconv.Atoi(string) // string到in ...