C++ 命名管道示例

想做一个 Hook CreateFile 重定向到内存的功能,貌似可以假借命名管道实现这个功能。不熟悉命名管道,做了几个demo,如下:

Server:

复制代码
// NamedPipeServer.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE hPipe = CreateNamedPipe(L"\\\\.\\Pipe\\mypipe",PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT
        ,PIPE_UNLIMITED_INSTANCES,0,0,NMPWAIT_WAIT_FOREVER,0);

    //waiting to be connected
    if(ConnectNamedPipe(hPipe, NULL) == NULL)
        return 0;

    DWORD    dwWrite;     
    const char *pStr = "data from server";
    if( !WriteFile(hPipe, pStr, strlen(pStr), &dwWrite, NULL) )    
    {        
        cout << "write failed..." << endl<< endl;        
        return 0;    
    }    
    cout << "sent data: " << endl << pStr<< endl<< endl;

    system("pause");
    return 0;
}
复制代码

 

Client:

复制代码
// NamedPipeClient.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;
#define BUFSIZE 5

int _tmain(int argc, _TCHAR* argv[])
{
    if (WaitNamedPipe(L"\\\\.\\Pipe\\mypipe", NMPWAIT_WAIT_FOREVER) == FALSE)
        return 0;

    HANDLE hPipe=CreateFile(L"\\\\.\\Pipe\\mypipe", GENERIC_READ | GENERIC_WRITE, 0,
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
     
    if((long)hPipe == -1)
        return 0;

    BOOL fSuccess = false;
    DWORD len = 0;
    char buffer[BUFSIZE];
    string recvData = "";
    do
    {
        fSuccess = ReadFile(hPipe ,buffer ,BUFSIZE*sizeof(char) ,&len ,NULL);
        char buffer2[BUFSIZE+1] = {0};
        memcpy(buffer2,buffer,len);
        recvData.append(buffer2);
        if(!fSuccess || len < BUFSIZE)
            break;
    }while(true);

    cout<<"recv data:"<<endl<<recvData.c_str()<<endl<<endl;

    FlushFileBuffers(hPipe); 
    DisconnectNamedPipe(hPipe);
    CloseHandle(hPipe);

    system("pause");
    return 0;
}
复制代码

 

Server & Client:

复制代码
// MultiThreadDemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;
#define BUFSIZE 5

DWORD WINAPI ThreadFunction(LPVOID lpParam)
{
    wstring* pipeName = static_cast<wstring*>(lpParam);

    HANDLE hPipe = CreateNamedPipe((*pipeName).c_str(),PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT
        ,PIPE_UNLIMITED_INSTANCES,0,0,NMPWAIT_WAIT_FOREVER,0);

    //waiting to be connected
    if(ConnectNamedPipe(hPipe, NULL) == NULL)
        return 0;

    DWORD    dwWrite;     
    const char *pStr = "data from server";
    if( !WriteFile(hPipe, pStr, strlen(pStr), &dwWrite, NULL) )    
    {        
        cout << "write failed..." << endl<< endl;        
        return 0;    
    }    
    cout << "sent data: " << endl << pStr<< endl<< endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    wstring pipeName = L"\\\\.\\Pipe\\mypipe";

    DWORD dwThreadID = 0;
    HANDLE hThread = CreateThread(NULL, 0, ThreadFunction, &pipeName , 0, &dwThreadID);

    Sleep(1000);

    if (WaitNamedPipe(pipeName.c_str(), NMPWAIT_WAIT_FOREVER) == FALSE)
        return 0;

    HANDLE hPipe=CreateFile(pipeName.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
     
    if((long)hPipe == -1)
        return 0;

    BOOL fSuccess = false;
    DWORD len = 0;
    char buffer[BUFSIZE];
    string recvData = "";
    do
    {
        fSuccess = ReadFile(hPipe ,buffer ,BUFSIZE*sizeof(char) ,&len ,NULL);
        char buffer2[BUFSIZE+1] = {0};
        memcpy(buffer2,buffer,len);
        recvData.append(buffer2);
        if(!fSuccess || len < BUFSIZE)
            break;
    }while(true);

    cout<<"recv data:"<<endl<<recvData.c_str()<<endl<<endl;

    FlushFileBuffers(hPipe); 
    DisconnectNamedPipe(hPipe);
    CloseHandle(hPipe);

    system("pause");
    return 0;
}
复制代码

 

posted on   空明流光  阅读(3374)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示