The MASM Forum

General => The Campus => Topic started by: Landsfiskalen on January 14, 2015, 10:23:05 PM

Title: Problem with CreateProcess (using Pipe)
Post by: Landsfiskalen on January 14, 2015, 10:23:05 PM
Hi all!
I've got a problem with CreateProcess. I want to start an external program and redirect the output to an edit control. This is the code I've got from looking at Iczelion's tutorial #21.


     sui:STARTUPINFO,
           pi: PROCESS_INFORMATION,
           hRead:DWORD,
           hWrite:DWORD,
           bytesRead:DWORD,
           sat:SECURITY_ATTRIBUTES

           ...

            mov sat.nLength,sizeof sat
            mov sat.lpSecurityDescriptor,NULL
            mov sat.bInheritHandle,TRUE
            invoke CreatePipe,ADDR hRead,ADDR hWrite,ADDR sat,0
            .if eax == 0
            invoke MessageBox,hWnd,ADDR AppName, ADDR szCreatePipeError,MB_ICONERROR or MB_OK
            .else
    invoke RtlZeroMemory,ADDR sui,sizeof sui
    invoke RtlZeroMemory,ADDR pi,sizeof pi
    mov sui.cb,sizeof sui
    invoke GetStartupInfo,ADDR sui
    xor eax,eax
    mov sui.lpReserved,eax
    mov eax,hWrite   
    mov sui.hStdOutput,eax
                mov sui.hStdError,eax
                mov sui.dwFlags,STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES
                mov sui.wShowWindow,SW_HIDE
                invoke CreateProcess,NULL,ADDR CommandLine,NULL,NULL,TRUE,0,NULL,NULL,ADDR sui,ADDR pi
                .if eax == 0
                    invoke MessageBox,hWnd,ADDR szCreateProcessError,ADDR AppName,MB_ICONERROR or MB_OK
                .else
                    invoke CloseHandle,hWrite
                    .while TRUE
                        invoke RtlZeroMemory,ADDR buffer,1024
                        invoke ReadFile,hRead,ADDR buffer,1023,ADDR bytesRead,NULL
                        .if eax == 0
                            .break
                        .endif
                        invoke SendMessage,hConsoleEdit,EM_SETSEL,-1,0
                        invoke SendMessage,hConsoleEdit,EM_REPLACESEL,FALSE,ADDR buffer
                    .endw
                .endif
                invoke CloseHandle,hRead
           .endif


It manages to create the Pipe, but the call to CreateProcess returns 0. Any help would be greatly appreciated.
Title: Re: Problem with CreateProcess (using Pipe)
Post by: jj2007 on January 14, 2015, 11:49:48 PM
Quote from: Landsfiskalen on January 14, 2015, 10:23:05 PM
the call to CreateProcess returns 0.

Not only. It also returns an error code that is accessible via GetLastError. Besides, it might be helpful to see the values of parameters that you are passing to the call. Use pushad ... print str$(x), 13, 10, ... popad to do that.
Title: Re: Problem with CreateProcess (using Pipe)
Post by: dedndave on January 15, 2015, 04:10:10 AM
i'm not sure i would use GetStartupInfo to fill the STARTUPINFO structure
try commenting that line out

you zero the structure, then it would seem you fill in the appropriate fields
anything put in by GetStartupInfo is likely to muddle the situation

you fill in hStdOutput and hStdError, but not hStdInput
this may be correct - i've not played with pipes, much
mov sui.hStdOutput,eax
mov sui.hStdError,eax


you haven't shown us what "CommandLine" holds   :P
make sure the EXE file can be found

it only takes one little thing to stop it from working - lol
unfortunately, the information returned by GetLastError
is likely to be something like "Invalid Parameter", which isn't really helpful
Title: Re: Problem with CreateProcess (using Pipe)
Post by: dedndave on January 15, 2015, 04:16:31 AM
oh - hWrite is a local variable
make it global   :t
Title: Re: Problem with CreateProcess (using Pipe)
Post by: Landsfiskalen on January 15, 2015, 07:47:50 AM
Thanks for the input lads! Gonna check it out tomorrow.
Title: Re: Problem with CreateProcess (using Pipe)
Post by: Landsfiskalen on January 16, 2015, 04:17:30 AM
It was the commandline parameter that was malformed. When I used GetLastError I got a "File not found" exception. Thanks for the help!  :t