Pipe,利用匿名管道实现进程间通信

管道是一种用于在进程间共享数据的机制,其实质是一段共享内存.
Windows系统为这段共享的内存设计采用数据流I/0的方式来访问.
命令管道可以在任意进程间通信,通信是双向的,任意一端都可读可写,但是在同一时间只能有一端读,一端写.

Note:须包含头文件 #include <windows.h>

一、父进程

//
// 1.Create pipe
//
HANDLE hReadPipe;
HANDLE hWritePipe;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
BOOL flag;
flag = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);
if (flag)
{
    // CreatePipe successfully.
    ......
}
else
{
    // CreatePipe failed.To get extended error information, call GetLastError.
    ......
}

//
// 2.Create child process
//
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = hReadPipe;
si.hStdOutput = hWritePipe;
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
ZeroMemory(&pi, sizeof(pi));

TCHAR szCommandLine[] = TEXT("child.exe");

if(!CreateProcess(
    NULL,           // No module name (use command line)
    szCommandLine,  // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    TRUE,           // Set handle inheritance to TRUE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi)            // Pointer to PROCESS_INFORMATION structure
    ) 
{
    // CreateProcess successfully.
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}
else
{
    // CreateProcess failed.
    CloseHandle(hReadPipe);
    CloseHandle(hWritePipe);
    hReadPipe = NULL;
    hWritePipe = NULL;
    ......
}

//
// 3.Read File
//
char buf[100] = { 0 };
DWORD dwBytesRead;
if(!ReadFile(hReadPipe, buf, 100, &dwBytesRead, NULL))
{
    // ReadFile failed.
    ......
}
else
{
    // ReadFile successfully.
    ......
}

//
// 4.Write File
//
char buf[] = "Hello Kitty.";
DWORD dwBytesWritten;
if(!WriteFile(hWritePipe, buf, sizeof(buf), &dwBytesWritten, NULL))
{
    // Write file failed.
}
else
{
    // Write file successfully.
}

二、子进程

//
// 1.获取句柄
//
HANDLE hReadPipe;
HANDLE hWritePipe;
hReadPipe = GetStdHandle(STD_INPUT_HANDLE);
hWritePipe = GetStdHandle(STD_OUTPUT_HANDLE);

//
// 2.Read File
//
char buf[100] = { 0 };
DWORD dwBytesRead;
if(!ReadFile(hReadPipe, buf, 100, &dwBytesRead, NULL))
{
    // ReadFile failed.
    ......
}
else
{
    // ReadFile successfully.
    ......
}

//
// 3.Write File
//
char buf[] = "Hello Kitty.";
DWORD dwBytesWritten;
if(!WriteFile(hWritePipe, buf, sizeof(buf), &dwBytesWritten, NULL))
{
    // Write file failed.
}
else
{
    // Write file successfully.
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值