// Process.h
#pragma once
#include<iostream>
#include<windows.h>
#define BuffSize 1024
#define pipeName "\\\\.\\pipe\\consolePipe"
using namespace std;
class Process {
public:
HANDLE hPipe = INVALID_HANDLE_VALUE;
unsigned long writeLength = 0, readLength = 0;
Process() {}
int createNewProcess(const char* fileName) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&si, sizeof(si));
int flag = 0;
flag = CreateProcess(fileName, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (flag == 0) {
cout << "create process error: " << GetLastError() << endl;
return -1;
}
return 0;
}
int initPipe() {
hPipe = CreateNamedPipe(pipeName,
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE,
1,
BuffSize, BuffSize,
0, NULL
);
if (hPipe == INVALID_HANDLE_VALUE) {
cout << "server create Pipe error: " << GetLastError() << endl;
return -1;
}
return 0;
}
int serverConnPipe() {
int flag = ConnectNamedPipe(hPipe, NULL);
// The second argument is set to NULL to block execution, otherwise there will be chaining problems
if (flag == 0) {
flag = GetLastError();
if (flag != ERROR_IO_PENDING) {
cout << "server Pipe connect error: " << GetLastError() << endl;
}
return -1;
}
return 0;
}
int disConnect() {
if (hPipe != INVALID_HANDLE_VALUE)
DisconnectNamedPipe(hPipe);
return 0;
}
int clientConnPipe() {
int flag = WaitNamedPipe(pipeName, NMPWAIT_WAIT_FOREVER);
if (flag == 0)
cout << "client Pipe wait failed: " << GetLastError() << endl;
hPipe = CreateFile(pipeName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hPipe == INVALID_HANDLE_VALUE) {
cout << "client create Pipe error: " << GetLastError() << endl;
hPipe = NULL;
return -1;
}
return 0;
}
int wPipe(const char* s) {
int flag = WriteFile(hPipe, s, strlen(s) + 1, &writeLength, NULL);
if (flag == 0)
cout << "Write failure: " << GetLastError() << endl;
else
cout << "Write successfully\n";
return 0;
}
int rPipe(char* buff) {
memset(buff, NULL, BuffSize);
int flag = ReadFile(hPipe, buff, BuffSize, &readLength, NULL);
if (flag == 0) {
cout << "read pipe error: " << GetLastError() << endl;
return -1;
}
return 0;
}
};
Server端
// server.cpp
#include"Process.h"
#include<iostream>
int main() {
Process proc;
char buff[1024];
proc.initPipe();
while (true) {
int flag = proc.serverConnPipe();
if (flag == 0)
flag = proc.rPipe(buff);
if (flag == 0) {
cout << buff;
proc.disConnect();
}
}
return 0;
}
Client端
// client.cpp
// Process 1
#include"Process.h"
#include<iostream>
int main() {
Process proc;
proc.clientConnPipe();
proc.wPipe("Process 1 writes to the pipeline\n");
cout << "Process 1 is finished executing" << endl;
proc.disConnect();
proc.createNewProcess("path\\to\\Client2.exe");
Sleep(3000);
proc.clientConnPipe();
proc.wPipe("Process 1 writes to the pipeline again\n");
proc.disConnect();
getchar();
return 0;
}
// Client2.cpp Clinet2.exe
// Process 2
#include"Process.h"
#include<iostream>
int main() {
Process proc;
proc.clientConnPipe();
proc.wPipe("Process 2 writes to the pipeline\n");
proc.disConnect();
}