Pipe is readable only when WinSCP has finished

Advertisement

Mercury
Guest

Pipe is readable only when WinSCP has finished

I use WinSCP as a part of a larger program. It calls WinSCP.com in such a manner:

    SECURITY_ATTRIBUTES sa;
      // Security attributes
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = true;

    SECURITY_DESCRIPTOR sd;
    if (IsWinNT())
    {
        InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
        SetSecurityDescriptorDacl(&sd, true, NULL, false);
        sa.lpSecurityDescriptor = &sd;
    }

    // Stdout/stderr pipe
    // read = outside, write = inside
    CreatePipe(&hStdOutOutside, &hStdOutInside, &sa, 1024*10);

    // Stdin pipe
    // read = inside, write = outside
    CreatePipe(&hStdInInside, &hStdInOutside, &sa, 1024*10);

    // Startup info
    STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    si.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.hStdOutput = hStdOutInside;
    si.hStdError = hStdOutInside;
    si.hStdInput = hStdInInside;
    si.wShowWindow = SW_HIDE;

    // cmdline
    xchar* cmdl = new xchar[1024]; //xstrlen(aFname)+xstrlen(aCmdLine) + 10];
    xsprintf(cmdl, "\"%s\" %s", aFname, aCmdLine);

    // process info
    PROCESS_INFORMATION pi;

    CreateProcessA(
            NULL,           // lpApplicationName
            cmdl,           // lpCommandLine
            NULL,           // lpProcessAttributes
            NULL,           // lpThreadAttributes
            true,           // bInheritHandles
            0,              // dwCreationFlags
            NULL,           // lpEnvironment
            NULL,           // lpCurrentDirectory
            &si,            // lpStartupInfo
            &pi );          // lpProcessInformation

To read the output, I use such an instruction...

    unsigned long nbr, nbr2, nba, nbl;

    PeekNamedPipe(
            hStdOutOutside, aBuf, aLength,
            &nbr, &nba, &nbl);

    if (nbr>0)
    {
        ReadFile(
            hStdOutOutside, aBuf, nbr, &nbr2, NULL);
        return nbr2;
    } else return 0;

The output appears on my end of pipe only after WinSCP has finished => the program cannot interact with WinSCP (it can only feed predefined scripts). Other programs (for example, cmd.exe) seem to be OK.

What to do? Maybe, wrong code?

Reply with quote

Advertisement

Mercury
Guest

I don't use script. I try to pass these commands using stdin:

open someconnection
close
exit

(no real interaction by now, but there'll be.)

Reply with quote

Mercury
Guest

The same happens to files. I write

WinSCP.com /script=somelongscript.sc >file.txt

F3 - the file is empty
F3 - it's empty again!!
F3 - empty!!!
After some tries, all the information appears in file.txt in one piece.

Reply with quote

Advertisement

You can post new topics in this forum