Last active
March 2, 2022 14:12
-
-
Save matu3ba/f348ef37a67112e39611d9baa741dedc to your computer and use it in GitHub Desktop.
parentchild_pipe on windows in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
zig cc -target x86_64-windows parent.c -o parent.exe | |
zig cc -target x86_64-windows child.c -o child.exe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<windows.h> // windows.h | |
#define BUFFER_SIZE 25 | |
int main(void){ | |
HANDLE read_handle; | |
CHAR buffer[BUFFER_SIZE]; | |
DWORD read; | |
/* getting the read handle of the pipe */ | |
read_handle = GetStdHandle(STD_INPUT_HANDLE); | |
/* the child reads from the pipe */ | |
if (ReadFile(read_handle, buffer, BUFFER_SIZE, &read, NULL)) | |
printf("child read %s", buffer); | |
else | |
fprintf(stderr, "Error reading from pipe"); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<windows.h> // windows.h | |
#define BUFFER_SIZE 25 | |
int main(void){ | |
HANDLE read_handle, write_handle; | |
STARTUPINFO si; | |
PROCESS_INFORMATION pi; | |
char message[BUFFER_SIZE] = "Greetings"; | |
DWORD written; | |
// security attributes to allow pipes to be inherited | |
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE}; | |
// docs show zeroing out of startup and process information | |
// https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/ProcThread/creating-processes.md | |
ZeroMemory(&si, sizeof(si)); | |
ZeroMemory(&pi, sizeof(pi)); | |
if (!CreatePipe(&read_handle, &write_handle, &sa, 0)) { | |
fprintf(stderr, "Create Pipe Failed"); return 1; | |
} | |
GetStartupInfo(&si); // start info struct for child process | |
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | |
// redirect stdin to read end of pipe | |
si.hStdInput = read_handle; | |
// replace all handles all of the handles instead of connecting to them | |
si.dwFlags = STARTF_USESTDHANDLES; | |
SetHandleInformation(write_handle, HANDLE_FLAG_INHERIT, 0); | |
CreateProcess(NULL, "child.exe", NULL, NULL, TRUE, /* inherit handles */ 0, NULL, NULL, &si, &pi); | |
CloseHandle(read_handle); // close unused end of pipe | |
if(!WriteFile(write_handle, message, BUFFER_SIZE, &written, NULL)) | |
fprintf(stderr, "Error writing to pipe."); | |
WaitForSingleObject(pi.hProcess,INFINITE); //wait for the child to exit | |
CloseHandle(pi.hProcess); | |
CloseHandle(pi.hThread); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment