;=======================================================================
; Based on example by Antonis Kyprianou
;=======================================================================
; v. 3.0
;=======================================================================
proc CreatePipeAndExecuteX,.command
;-----------------------------------------------------------------------
    invoke LocalAlloc,0,32768
    test eax,eax
    jz  @F
    push eax
    invoke lstrcpy,eax,[.command]
    stdcall CreatePipeAndExecute,[esp]
    pop  ecx
    push eax
    push edx
    invoke LocalFree,ecx
    pop edx
    pop eax
    ret    
@@:
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if defined MUI_ADDIN
    UIMessage [hMain],AllocMemoryError,0,0
else
    invoke MessageBox,[hMain],AllocMemoryError,0,0
end if    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    mov eax,-1
    mov edx,eax
    ret
;-----------------------------------------------------------------------
endp
;=======================================================================
;
;=======================================================================
proc CreatePipeAndExecute,.command 
;---------------------------------------------------------------------
    local .sat:         SECURITY_ATTRIBUTES
    local .StartupInfo: STARTUPINFO
    local .ProcessInfo: PROCESS_INFORMATION
    local .hRead:       dd ?
    local .hWrite:      dd ?
    local .BytesRead:   dd ?
;=======================================================================
    push edi
;---------------------------------------------------------------------
    invoke SendMessage,[hOut],EM_REPLACESEL,FALSE,szCr
    invoke SendMessage,[hOut],EM_REPLACESEL,FALSE,[.command]
    invoke SendMessage,[hOut],EM_REPLACESEL,FALSE,szCr
    invoke SendMessage,[hOut],EM_REPLACESEL,FALSE,szCr
    invoke SendMessage,[hOut],EM_SCROLLCARET,0,0
;---------------------------------------------------------------------
;   Creating pipe
;-----------------------------------------------------------------------
    mov [.sat + SECURITY_ATTRIBUTES.nLength],sizeof.SECURITY_ATTRIBUTES
    mov [.sat + SECURITY_ATTRIBUTES.lpSecurityDescriptor],0
    mov [.sat + SECURITY_ATTRIBUTES.bInheritHandle],TRUE
    lea eax,[.hRead]
    lea ecx,[.hWrite]
    lea edi,[.sat]
    invoke CreatePipe,eax,ecx,edi,0
    test eax,eax
    jnz @F
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if defined MUI_ADDIN
    GetUIstring CreatePipeError,u_buff
    invoke SendMessage,[hOut],EM_REPLACESEL,FALSE,u_buff
else
    invoke SendMessage,[hOut],EM_REPLACESEL,FALSE,CreatePipeError
end if    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.cpe_error:
    invoke LoadCursor,0,IDC_ARROW
    invoke SetCursor,eax
    mov eax,-1
    mov edx,eax
    pop edi
    ret
@@: 
;---------------------------------------------------------------------
;   Creating process
;-----------------------------------------------------------------------
    lea edi,[.StartupInfo]
    mov ecx,sizeof.STARTUPINFO
    xor eax,eax
    rep stosb
    mov [.StartupInfo + STARTUPINFO.cb],sizeof.STARTUPINFO
    mov eax,[.hWrite]
    mov [.StartupInfo + STARTUPINFO.hStdOutput],eax
    mov [.StartupInfo + STARTUPINFO.hStdError],eax
    mov [.StartupInfo + STARTUPINFO.dwFlags],\
                             STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW
    mov [.StartupInfo + STARTUPINFO.wShowWindow],SW_HIDE
    lea eax,[.StartupInfo]
    lea ecx,[.ProcessInfo]
    mov edi,[.command]
    invoke CreateProcess,0,edi,0,0,TRUE,0,0,0,eax,ecx
    test eax,eax
    jnz @F    
    invoke CloseHandle,[.hRead]
    invoke CloseHandle,[.hWrite]
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if defined MUI_ADDIN
    GetUIstring CreateProcessError,u_buff
    invoke SendMessage,[hOut],EM_REPLACESEL,FALSE,u_buff
else
    invoke SendMessage,[hOut],EM_REPLACESEL,FALSE,CreateProcessError
end if    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    jmp .cpe_error
@@:    
;---------------------------------------------------------------------
;   Reading from pipe
;-----------------------------------------------------------------------
    invoke CloseHandle,[.hWrite]
    invoke CloseHandle,[.ProcessInfo + PROCESS_INFORMATION.hThread]
@@:
    invoke PeekNamedPipe,[.hRead],0,0,0,0,0
    test eax,eax
    jz @F
    lea eax,[.BytesRead]
    invoke ReadFile,[.hRead],edi,32000,eax,0
    test eax,eax
    jz @F
    mov ecx,[.BytesRead]
    mov byte ptr edi + ecx,0
    invoke OemToChar,edi,edi
    invoke SendMessage,[hOut],EM_REPLACESEL,FALSE,edi
    jmp @B
@@:    
    invoke CloseHandle,[.hRead]
    mov eax,[.ProcessInfo + PROCESS_INFORMATION.hProcess]
    push eax
    invoke GetExitCodeProcess,eax,esp
    invoke CloseHandle,[.ProcessInfo + PROCESS_INFORMATION.hProcess]
    pop eax
    xor edx,edx
    pop edi
    ret
;-----------------------------------------------------------------------
endp
;=======================================================================
