windows命名管道_草上爬的博客-CSDN博客_windows命名管道
匿名管道
#include "QtWidgetsApplication1.h"
#include <QtWidgets/QApplication>
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
using namespace std;
HANDLE hRead;
HANDLE hWrite;
#define PARENT_READ
#if 0
int test()
{
HANDLE hParentRead, hParentWrite, hChildRead, hChildWrite; //创建4个句柄
STARTUPINFO si = { 0 }; //启动信息结构体
si.cb = sizeof(si);
PROCESS_INFORMATION pi = { 0 }; //进程信息结构体
DWORD dwWritedBytes = 0;
DWORD dwReadedBytes = 0;
DWORD dwBytesRead = 0;
DWORD dwTotalBytesAvail = 0;
DWORD dwBytesLeftThisMessage = 0;
SECURITY_ATTRIBUTES sa = { 0 }; //安全属性描述符
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE; //设置句柄可继承
//创建管道1. 父进程读 -> 子进程写入
BOOL bRet = CreatePipe(&hParentRead,
&hChildWrite,
&sa,
0);
//创建管道2. 子进程读->父进程写.
bRet = CreatePipe(&hChildRead,
&hParentWrite,
&sa,
0);
//这里将子进程写重定向到 stdout中. 子进程读取重定向到stdinput中
si.hStdInput = hChildRead;
si.hStdOutput = hChildWrite;
si.dwFlags = STARTF_USESTDHANDLES; //设置窗口隐藏启动
bRet = CreateProcess(NULL,
L"cmd.exe", //创建cmd进程.默认寻找cmd进程.
NULL,
NULL,
TRUE,
CREATE_NO_WINDOW,
NULL,
NULL,
&si,
&pi);
char szBuffer[15] = "calc ";
WriteFile(hParentWrite, szBuffer, 15, NULL, 0);//使用writeFile操作管道,给cmd发送数据命令.
return 0;
}
#endif
BOOL CreateChildProcess() {
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
if (!CreatePipe(&hRead, &hWrite, &sa, 0)) {
printf("Failed to create pipe.");
return FALSE;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
#ifdef PARENT_READ //ParentRead
//si.hStdInput = GetStdHandle(STD_OUTPUT_HANDLE); // Child process read from this handle rather than std input handle
//si.hStdOutput = hWrite; // Child process also outputs content to parent's window
si.hStdInput = hWrite; // Child process read from this handle rather than std input handle
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); // Child process also outputs content to parent's window
#else
si.hStdInput = hRead; // Child process read from this handle rather than std input handle
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); // Child process also outputs content to parent's window
#endif
if (FALSE == CreateProcess(L"./child.exe",
NULL,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&si,
&pi)) {
auto err = GetLastError();
CloseHandle(hRead);
CloseHandle(hWrite);
return FALSE;
}
else {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
return TRUE;
}
#define BUFFER_SIZE 4096
void SendData() {
char data[BUFFER_SIZE] = { "nihao"};
while (true) {
scanf_s("%s", data, BUFFER_SIZE);
DWORD dwWrite = 0;
#if 0
if (!WriteFile(hWrite, data, strnlen_s(data, BUFFER_SIZE) + 1, &dwWrite, NULL)) {
printf_s("Failed to write to child process.");
}
#else
ReadFile(hRead, data, BUFFER_SIZE, &dwWrite, NULL);
if (strcmp(data, "quit") == 0) {
break;
}
printf_s("child process: %s", data);
#endif
if (strcmp(data, "quit") == 0) {
break;
}
}
}
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QtWidgetsApplication1 w;
if (CreateChildProcess()) {
SendData();
}
w.show();
return a.exec();
}
#include "child.h"
#include <QtWidgets/QApplication>
#include <windows.h>
#include <iostream>
using namespace std;
#define BUFFER_SIZE 4096
int main(int argc, char *argv[])
{
/*QApplication a(argc, argv);
child w;
w.show();*/
auto hRead = GetStdHandle(STD_INPUT_HANDLE); // This has been replaed by pipe read handle
char data[BUFFER_SIZE] = { 0 };
DWORD dwRead = 0;
while (true) {
#if 0
ReadFile(hRead, data, BUFFER_SIZE, &dwRead, NULL);
if (strcmp(data, "quit") == 0) {
break;
}
#else
Sleep(10 * 1000);
DWORD dwWrite = 0;
strcpy(data, "child data");
if (!WriteFile(hRead, data, strnlen_s(data, BUFFER_SIZE) + 1, &dwWrite, NULL)) {
printf_s("Failed to write to child process.");
MessageBoxA(0, data, "Failed to write to child process.", MB_OK);
}
//Sleep(10 * 1000);
#endif
printf_s("child process: %s", data);
// MessageBoxA(0, data, "子进程接收到的数据", MB_OK);
MessageBoxA(0, data, "子进程发送到的数据", MB_OK);
}
return 0;
// return a.exec();
}