redirecting input and output from cmd

  c++, io, ipc, winapi, windows

Basically just want to open cmd with the createprocess function and redirect the input and output streams and then just write to the input stream and print the output stream to the console, but my code just doesn’t work lol. Every time I run it, it gets stuck at the ReadFile function with which I want to read from the output stream.

#include <windows.h>
#include <stdio.h>


#pragma warning(disable : 4996)


int main()
{


    //Variables for CreateProcess
    STARTUPINFO sui;
    PROCESS_INFORMATION pi;

    //Variables for CreatePipe
    HANDLE ph_cmd_stdout_write = NULL;
    HANDLE ph_cmd_stdout_read = NULL;
    HANDLE ph_cmd_stdin_write = NULL;
    HANDLE ph_cmd_stdin_read = NULL;

    SECURITY_ATTRIBUTES saAttr;

    //other Variables
    char* Buf2 = (char*)malloc(4000);
    char Buf[20];

    DWORD dwWrittenByt = NULL;

    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    saAttr.bInheritHandle = TRUE;
    saAttr.lpSecurityDescriptor = NULL;

    //creating pipe for the output stream
    if (!CreatePipe(&ph_cmd_stdout_read, &ph_cmd_stdout_write, &saAttr, 0)) {
        printf("CreatePipe for cmd output  --FAILED n");
        return 1;
    }
    

    //creating pipe for the input stream
    if (!CreatePipe(&ph_cmd_stdin_read, &ph_cmd_stdin_write, &saAttr, 0)) {
        printf("CreatePipe for cmd input  --FAILED n");
        return 1;
    }


    //setting up pi and sui
    memset(&pi, 0, sizeof(PROCESS_INFORMATION));
    memset(&sui, 0, sizeof(sui));

    sui.cb = sizeof(sui);
    sui.hStdError = ph_cmd_stdout_write;
    sui.hStdOutput = ph_cmd_stdout_write;
    sui.hStdInput = ph_cmd_stdin_read;
    sui.dwFlags = STARTF_USESTDHANDLES;



    //creating cmd process
    TCHAR commandLine[256] = L"cmd.exe";
    if (!CreateProcess(NULL, commandLine, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &sui, &pi)) {
        CloseHandle(ph_cmd_stdin_read);
        CloseHandle(ph_cmd_stdin_write);
        CloseHandle(ph_cmd_stdout_read);
        CloseHandle(ph_cmd_stdout_write);

        printf("Create Proces Failed n");
        
        return 1;
    }


      //the party where I want to write and read from the created pipes
    while (1) {
        
        memset(Buf, 0, sizeof(Buf));
        memset(Buf2, 0, sizeof(Buf2));

        printf("input turn n");
        scanf("%s", Buf);

        //Terminates cmd process if input equals quit
        if (strncmp(Buf, "quit n", 4) == NULL) {
            printf("process ; %d n", pi.hProcess);
            printf("thread ; %d n", pi.hThread);
            if (TerminateProcess(pi.hProcess, 0) != NULL) {

                printf("Terminate Process succeed n");
            }
            else {

                printf("Terminate Process did NOT succeed n");
            }

            CloseHandle(pi.hProcess);
            CloseHandle(pi.hThread);
            free(Buf2);
            break;
        }
        
        //here i want to write to the input pipe
        printf("write File Turn n");
        if (WriteFile(ph_cmd_stdin_write, Buf, sizeof(Buf), dwWrittenByt, NULL) == NULL) {
            printf("WriteFile turn n");
        }
        //here i want to read from the output pipe
        printf("ReadFile turn n");
        if (ReadFile(ph_cmd_stdout_read, Buf2, 4000, dwWrittenByt, NULL) == NULL) {
            printf("ReadFile error n");
        }

        puts(Buf2);

    }

    printf("end of the programm n");
    return 0;
}

Source: Windows Questions

LEAVE A COMMENT