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. [转帖]061、监控指标之TiKV

    资源相关 Grafana监控 TiKV-Details -> Cluster - Store Size / Available Size Grafana监控 TiKV-Details -> ...

  2. [转帖]《Linux性能优化实战》笔记(三)—— CPU 上下文切换(下)

    上篇介绍了三种CPU 上下文切换以及它们可能造成的问题和原因,这一篇来看看在系统中如何发现CPU 上下文切换问题. 一. 查看上下文切换情况 主要使用两个命令:vmstat以及之前用过的pidstat ...

  3. [转帖]jmeter实现不写代码把测试结果存入execl

    这里使用数据库作为中间件来实现不写代码就把测试结果存入execl,下面是步骤 1.新建一个setup线程组用来设置数据库连接信息和新建数据库,如下图所示,我们使用sqlite数据库来存储信息,因为不需 ...

  4. [转帖]金仓数据库KWR使用说明

    金仓数据库KWR使用说明 1.KWR性能报告介绍 2.部署KWR性能报告功能 2.1.修改配置文件 2.2.使配置文件中参数生效 2.3.创建KWR扩展 3.使用KWR快照功能 3.1.创建KWR快照 ...

  5. overcommit_memory的简单学习

    overcommit_memory的简单学习 背景 前几天一个测试环境启动失败. 总是有如下的提示: Native memory allocation (mmap) failed to map 122 ...

  6. 设置echarts线的样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. sass中使用穿透属性(deep)修改第三方组件样似

    <el-form-item> <el-button class="save-btn" type="primary" @click=" ...

  8. docker 推送镜像到harbor

    服务器A的镜像要推送到已安装harbor的服务器B 1.修改服务器A的/etc/docker/daemon.json文件 其中,http://211.131.241.221:8888为你要推送的服务器 ...

  9. 期盼已久全平台支持-开源IM项目OpenIM之uniapp更新

    国内uniapp使用广泛,OpenIM的uniapp sdk以及文档和demo (https://github.com/OpenIMSDK/Open-IM-Uniapp-Demo)都已更新,本文主要展 ...

  10. WPF内嵌Http协议的Server端

    需求:有时后比如WPF,WinForm,Windows服务这些程序可能需要对外提供接口用于第三方服务主动通信,调用推送一些服务或者数据. 想到的部分实现方式: 一.使用Socket/WebSocket ...