Hi All, 

Currently I am looking at a way to redirect standard output and error from 

What I am finding is that when I call something like "git fetch 
g...@github.com:username/project.git" there is a challenge for the ssh 
passphrase waiting on the console. When I do this via a "CreateProcess" in 
an application and try to redirect the output to another location I don't 
see the output come through the pipe I have setup. Has this been seen 
before and is there a way to fix this? Currently haven't been able to find 
something in the archive, or on google about it. 

The test code I am using is listed below. As a test you can make the 
command anything which returns a large amount of text and this is returned 
to the calling application. E.g. ipconfig. Note that the working directory 
needs to be change to something which exists, and already contains the 
project being fetched. 

##### CODE ######

#define _WIN32_WINNT _WIN32_WINNT_WIN7
#include <windows.h>
#include <stdio.h>
#include <string>
#include <iostream>

void launch(const char * cmdline_in)
    STARTUPINFOA startupInfo; 

    HANDLE stdoutReadHandle = NULL;
    HANDLE stdoutWriteHandle = NULL;

    char cmdline[256];
    char outbuf[32768];
    DWORD bytes_read;
    char tBuf[257];

    DWORD exitcode;

    strcpy_s(cmdline, sizeof(cmdline), cmdline_in);

    memset(&saAttr, 0, sizeof(saAttr));
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
    saAttr.bInheritHandle = TRUE; 
    saAttr.lpSecurityDescriptor = NULL; 

    // Create a pipe for the child process's STDOUT. 
    if (!CreatePipe(&stdoutReadHandle, &stdoutWriteHandle, &saAttr, 5000))
        printf("CreatePipe: %u\n", GetLastError());

    // Ensure the read handle to the pipe for STDOUT is not inherited.
    if (!SetHandleInformation(stdoutReadHandle, HANDLE_FLAG_INHERIT, 0))
        printf("SetHandleInformation: %u\n", GetLastError());

    memset(&startupInfo, 0, sizeof(startupInfo));
    startupInfo.cb = sizeof(startupInfo);
startupInfo.hStdError = stdoutWriteHandle;
    startupInfo.hStdOutput = stdoutWriteHandle;
    startupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    startupInfo.dwFlags |= STARTF_USESTDHANDLES;

    // memset(&processInfo, 0, sizeof(processInfo));  // Not actually 


    if (!CreateProcessA(NULL, cmdline, NULL, NULL, TRUE,
        CREATE_NO_WINDOW, NULL, "C:\\SourceDirectory\\", &startupInfo, 
printf("Create Process Failed %u\n", GetLastError());

    printf("Close Handle\n");

    strcpy_s(outbuf, sizeof(outbuf), "");
    printf("Start of Loop\n");
    for (;;) {
        printf("Just before ReadFile(...)\n");
        if (!ReadFile(stdoutReadHandle, tBuf, 256, &bytes_read, NULL))
            printf("ReadFile: %u\n", GetLastError());
        printf("Just after ReadFile, read %u byte(s)\n", bytes_read);
        if (bytes_read > 0)
            tBuf[bytes_read] = '\0';
            strcat_s(outbuf, sizeof(outbuf), tBuf);

    printf("Output: %s\n", outbuf);

    if (WaitForSingleObject(processInfo.hProcess, INFINITE) != 
        printf("WaitForSingleObject: %u\n", GetLastError());

    if (!GetExitCodeProcess(processInfo.hProcess, &exitcode))
        printf("GetExitCodeProcess: %u\n", GetLastError());

    printf("Exit code: %u\n", exitcode);

    CloseHandle( processInfo.hProcess );
    CloseHandle( processInfo.hThread );


int main(int argc, char** argv)
    launch("C:\\Program Files (x86)\\Git\\bin\\git.exe fetch 
    std::string line;
std::getline(std::cin, line);
    return 0;

You received this message because you are subscribed to the Google Groups "Git 
for human beings" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to git-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to