VC管道 通过匿名管道在进程间双向通信

管道 通过匿名管道在进程间双向通信

参考网址:http://www.xuebuyuan.com/1959215.html

我的测试代码:

Pipe_Server_VC6_Console.exe :

#include <windows.h>

#include <iostream>
using namespace std;

int main()
{
    //定义四个句炳保留两个管道的信息
    HANDLE hReadPipe1, hWritePipe1, hReadPipe2, hWritePipe2;
    SECURITY_ATTRIBUTES sat;
    STARTUPINFO startupinfo;
    PROCESS_INFORMATION pinfo;
    BYTE buffer[1024];
    DWORD byteRead, byteWrite;

    string rString;
    string m_Host= "Pipe_Client_VC6_Console.exe";

    sat.nLength=sizeof(SECURITY_ATTRIBUTES);
    sat.bInheritHandle=true;
    sat.lpSecurityDescriptor=NULL;

    //创建管道,它用来做子进程的输出
    if(!CreatePipe(&hReadPipe1, &hWritePipe1, &sat, NULL))
    {
        cout<<("Create Pipe Error!")<<endl;
        return 1;
    }

    //创建管道,它用来做子进程的输入
    if(!CreatePipe(&hReadPipe2, &hWritePipe2, &sat, NULL))
    {
        cout<<"Create Pipe Error!"<<endl;
        return 1;
    }

    startupinfo.cb=sizeof(STARTUPINFO);
    //用GetStartupInfo获得当前进程的参数,否则STARTUPINFO参数太多,会让人抓狂
    GetStartupInfo(&startupinfo);
    startupinfo.hStdInput = hReadPipe2;
    startupinfo.hStdError=hWritePipe1;
    startupinfo.hStdOutput=hWritePipe1;
    //要有STARTF_USESTDHANDLES,否则hStdInput, hStdOutput, hStdError无效
    startupinfo.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    startupinfo.wShowWindow=SW_HIDE;
    if(!CreateProcess(NULL, (char*)m_Host.c_str(), NULL, NULL, TRUE, NULL, NULL, NULL, &startupinfo, &pinfo))
    {
        cout<<("create process error!")<<endl;
        return 1;
    }

    RtlZeroMemory(buffer,1024);
    //进程的输出重定向到hWritePipe1,所以从hReadPipe1读取
    // 在从管道中读取到数据前父进程将发生阻塞
    if (ReadFile(hReadPipe1,buffer,1023,&byteRead,NULL))
    {
        cout<<buffer;
    }

    string csWrite = "ping www.sina.com.cn\r\n";
    //进程的输入重定向到hReadPipe2,所以从hWritePipe2写入
    // 向管道中写数据不会发生阻塞,此循环会一直执行下去。
    while (WriteFile(hWritePipe2, (LPCVOID)csWrite.c_str(), csWrite.length() + 1, &byteWrite, NULL))
    {
        Sleep(1000);
        cout << "write " << byteWrite << endl;
    }


    CloseHandle(hReadPipe1);
    CloseHandle(hReadPipe2);
    CloseHandle(hWritePipe1);
    CloseHandle(hWritePipe2);

    return 0;
}

Pipe_Client_VC6_Console.exe :

#include <windows.h>

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    HANDLE hRead = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hWrite = GetStdHandle(STD_OUTPUT_HANDLE);

    char s[] = "Hello, I am child process\n";
    DWORD dwWrite;

    if (!WriteFile(hWrite, s, strlen(s) + 1, &dwWrite, NULL))
    {
        cout << "Write to pipe failed!" << endl;
        return -1;
    }

    char buf[100];
    DWORD dwRead;
    if(!ReadFile(hRead, buf, 100, &dwRead, NULL))
    {
        cout << "Read from pipe failed!" << endl;
        return -1;
    }

    return 0;
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值