Writing to the STDIN Process via Windows Handsets

I am trying to create a function that will spawn an instance of a program, and then pass some data to its STDIN, and then read the process output using C ++. I looked at the MSDN example located here , which is rather confusing for me, and when I try to use this example, I get some nasty error codes and it will not work.

    HANDLE hWriteOUT, hReadOUT, hWriteIN, hReadIN;
    SECURITY_ATTRIBUTES saPipe = {0};
    PROCESS_INFORMATION procInfo = {0};
    STARTUPINFO procSi;
    DWORD dwWritten, dwRead;
    char buf[512];

    saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
    saPipe.bInheritHandle = TRUE;
    saPipe.lpSecurityDescriptor= NULL;
    CreatePipe(&hReadOUT, &hWriteOUT, &saPipe, 0);
    SetHandleInformation(hReadOUT, HANDLE_FLAG_INHERIT, 0);
    CreatePipe(&hReadIN, &hWriteIN, &saPipe, 0);
    SetHandleInformation(hReadIN, HANDLE_FLAG_INHERIT, 0);

    ZeroMemory(&procSi, sizeof(STARTUPINFO));
    procSi.cb = sizeof(STARTUPINFO);
    procSi.hStdError = hWriteOUT;
    procSi.hStdOutput = hWriteOUT;
    procSi.hStdInput = hReadIN;
    procSi.dwFlags |= STARTF_USESTDHANDLES;

    CreateProcess(NULL, "cmd", NULL, NULL, TRUE, 0, NULL, NULL, &procSi, &procInfo);
    //Gives me an error code of 18 but returns a 1 when a 0 indicates failure.

    WriteFile(hWriteIN, "notepad", sizeof("notepad"), &dwWritten, NULL);
    cout << GetLastError();  //This gives me error code 18 (ERROR_NO_MORE_FILES)
    ReadFile(hReadOUT, buf, 512, &dwRead, NULL);
    cout << buf;  //This prints "Microsoft Windows [version 6.1.7601]
    CloseHandle(hWriteIN);

"" cmd.exe, . , , . , ReadFile() , , , , (, ) , CMD. , , , ! (CMD "Microsoft Windows...\n ... \n C:\Users\Foo > ...\n", `ReadFile() )

+3
2

. , , , :

1) ENTER ( "\n" ) , cmd.exe. , CreateProcess, , " cmd/c" "cmd".

2) cmd.exe, , , . cmd.exe, ; , , , "" .

3) ReadFile , .

4) - , stdin stdout. , , ?

5) , , ( GetLastError) , . , , , , , , .

+5

, , - . , SetHandleInformation(hReadOUT, HANDLE_FLAG_INHERIT, 0); SetHandleInformation(hReadIN, HANDLE_FLAG_INHERIT, 0); . , , , cmd pipe, .

+2

All Articles