Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- HANDLE child_STDOUT_read; //read handle to child process' rerouted STDOUT pipe
- HANDLE child_STDOUT_write; //write handle to -||-
- HANDLE child_STDIN_read; //read handle to child process' rerouted STDIN pipe
- HANDLE child_STDIN_write; //write handle to -||-
- { //set up pipes for communication with child process
- SECURITY_ATTRIBUTES security;
- security.nLength = sizeof(SECURITY_ATTRIBUTES);
- security.bInheritHandle = true; //allow the child process to inherit any of the handles at all
- security.lpSecurityDescriptor = NULL;
- //may include a success check to catch creation errors ( if(!CreatePipe(..){error} )
- CreatePipe(&child_STDIN_read, &child_STDIN_write, &security, 0); //creates pipe with child_STDIN_read and child_STDIN_write as read and write handles
- SetHandleInformation(child_STDIN_write, HANDLE_FLAG_INHERIT, 0); //prevents inheritance of the STDIN_write handle access (child shouldnt be able to write to its own STDIN)
- CreatePipe(&child_STDOUT_read, &child_STDOUT_write, &security, 0); //creates pipe with child_STDOUT_read and child_STDOUT_write as read and write handles
- SetHandleInformation(child_STDOUT_read, HANDLE_FLAG_INHERIT, 0); //prevents inheritance of the STDOUT_write handle access (child shouldnt be able to read its own STDOUT)
- }
- PROCESS_INFORMATION Process_Info; //contains information about the launched child process
- { //set up child process
- TCHAR path_to_child[] = TEXT("Path\\to\\child\\"); //!!BEWARE OF SPACES IN PATH!!, can also just be the name of the executable or module (gnuplot), but thats prone to tampering.
- TCHAR command_to_execute[] = TEXT("child.exe -param"); //!!BEWARE OF SPACES IN PATH!!, executable to start and parameters, can include full path
- //theres a lot of overlap between the two. first one is more suited to programs that are part of the local folder hierarchy/program suite. second one is more suited to execute system wide modules like ls, cat, gnuplot, etc
- STARTUPINFO Start_Info; //contains information about how to launch the process
- BOOL child_creation_success = false;
- ZeroMemory(&Process_Info, sizeof(PROCESS_INFORMATION)); //initialise a PROCESS_INFORMATION instance with zeroes
- ZeroMemory(&Start_Info, sizeof(STARTUPINFO)); //initialise Start_info with all zero
- Start_Info.cb = sizeof(STARTUPINFO);
- Start_Info.hStdError = child_STDOUT_write; //replaces the STDEerr pipe of the child process with the STDOUT pipe defined in the parent process
- Start_Info.hStdOutput = child_STDOUT_write; //replaces the STDOUT pipe of the child process with the STDOUT pipe defined in the parent process(both STDERR and STDOUT appear in the pipe the parent process reads from)
- Start_Info.hStdInput = child_STDIN_read; //replaces the STDIN pope of the child process with the STDOUT pipe defined in the parent process
- Start_Info.dwFlags |= STARTF_USESTDHANDLES; //enables pipe replacement
- child_creation_success = CreateProcess(
- path_to_child, //as outlined above
- command_to_execute, //as outlined above
- NULL, //process security attributes
- NULL, //primary thread security attributes
- TRUE, //If this parameter is TRUE, each inheritable handle in the calling process is inherited by the new process. If the parameter is FALSE, the handles are not inherited.
- 0, //defines process execution priorities
- NULL, //defines user environment to use, NULL uses same environment as parent
- NULL, //defines directory to run in, NULL uses same directory as parent
- &Start_Info, //pointer to startupinfo
- &Process_Info //receives PROCESS_INFORMATION
- );
- if (!child_creation_success)
- {
- //do some error handling
- }
- }
- //Use ReadFile and WriteFile to access the pipe handles
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement