win32 – PeekNamedPipe的用法

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”

原文链接: https://www.cnblogs.com/strive-sun/p/13720037.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    win32 - PeekNamedPipe的用法

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/404832

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月25日 下午4:45
下一篇 2023年4月25日 下午4:45

相关推荐