News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

pipe input output problem

Started by white scorpion, February 01, 2005, 09:51:52 PM

Previous topic - Next topic

white scorpion

Hi All,

i've been trying to get the pipe Api's to work, but unfortunately it doesn't :(

the read part is easy, but the write part keeps hanging.

here's the code i have so far:

.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

.DATA

CommandLine db "cmd.exe",0
buffer2     db "dir",0
.CODE
start:

call MyProc

invoke ExitProcess,0



MyProc PROC

    LOCAL rect:RECT
    LOCAL hRead:DWORD
    LOCAL hWrite:DWORD
    LOCAL hRead2:DWORD
    LOCAL hWrite2:DWORD
    LOCAL startupinfo:STARTUPINFO
    LOCAL pinfo:PROCESS_INFORMATION
    LOCAL buffer[1024]:byte
    LOCAL bytesRead:DWORD
    LOCAL bytesWritten:DWORD
    LOCAL hdc:DWORD
    LOCAL sat:SECURITY_ATTRIBUTES

mov sat.nLength,sizeof SECURITY_ATTRIBUTES
mov sat.lpSecurityDescriptor,NULL
mov sat.bInheritHandle,TRUE
invoke CreatePipe,addr hRead,addr hWrite,addr sat,NULL
invoke CreatePipe,addr hRead2,addr hWrite2,addr sat,NULL
mov startupinfo.cb,sizeof STARTUPINFO
invoke GetStartupInfo,addr startupinfo
mov eax, hWrite
mov startupinfo.hStdOutput,eax
mov startupinfo.hStdError,eax
mov eax,hRead2
mov startupinfo.hStdInput,eax
mov startupinfo.dwFlags, STARTF_USESHOWWINDOW+ STARTF_USESTDHANDLES
mov startupinfo.wShowWindow,SW_HIDE
invoke CreateProcess, NULL, addr CommandLine, NULL, NULL, TRUE, NULL, NULL, NULL, addr startupinfo, addr pinfo

invoke CloseHandle,hWrite
.while 1
    .while TRUE
         invoke RtlZeroMemory,addr buffer,1024
        invoke ReadFile,hRead,addr buffer,1023,addr bytesRead,NULL
        .if eax==NULL
          .break
        .endif
        invoke StdOut,addr buffer
     .endw
    .while TRUE
        invoke WriteFile,hWrite2,addr buffer2,sizeof buffer2,addr bytesWritten,NULL
        .IF eax==NULL
            .break
        .endif
    .endw 
.endw       
invoke ExitProcess,0
invoke CloseHandle,hRead
invoke CloseHandle,hWrite
ret

MyProc ENDP

end start





thanks in advance for your help.

btw, the biggest piece of this code is from iczelion's tutorial about pipes, but that tutorial is only a one way pipe, well i managed to do that, but like i said, a 2 way communication is difficult....

akyprian

Hi,

You have to specify startupinfo.hStdInput (hRead2) too.

Regards,

Antonis

white scorpion

Hi,

thanks for your reply, but i already tried that as well, but it still won't work. i've altered above code like you said, but if you run it it still won't accept input :(


akyprian

Attached is a working WinAsm Studio project. It is not perferct but it has what you need plus a lot more.  I hope it helps.

Antonis

[attachment deleted by admin]

white scorpion

Thanks a lot, that will help me solve my problem ;-)

only one thing you should include:
checking for "exit", since it doesn't work well with the command prompt so you need to capture it yourself to close up.


akyprian

I thought I had checked for exit  ::) I have defined it at least ( szExit in EzPrompt.inc)  :green

Feel free to do anything you want with the code. If you make any considerable improvements, please let us all know

Antonis

white scorpion

well, thanks for the offer of using your code, but i personally prefer writing my own programs :)
everything i am distributing so far i have completely written myself and i would like to keep it like that :p

akyprian


white scorpion

almost got it working, but not completely....

can anyone please shed a light on what i'm doing wrong?


.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\advapi32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\advapi32.lib


.DATA
AppName     db "White Scorpion Security",0
pipefail    db "Unable to create Pipe.",0
AppToSpawn  db "cmd.exe",0

.DATA?
startinfo   STARTUPINFO <>
secat       SECURITY_ATTRIBUTES <>
secdes      SECURITY_DESCRIPTOR <>
procinfo    PROCESS_INFORMATION <>
newstdin    DWORD ?
newstout    DWORD ?
readout     DWORD ?
writein     DWORD ?
bytesRead   DWORD ?
bytesWrit   DWORD ?
avail       DWORD ?
exitcode    DWORD ?
buffer      db 1024 dup (?)


.CODE

start:



invoke InitializeSecurityDescriptor,addr secdes,SECURITY_DESCRIPTOR_REVISION
invoke SetSecurityDescriptorDacl,addr secdes,TRUE,NULL,FALSE

mov secat.lpSecurityDescriptor,offset secdes
mov secat.nLength,sizeof SECURITY_ATTRIBUTES
mov secat.bInheritHandle,TRUE

invoke CreatePipe,addr newstdin,addr writein,addr secat,0
.IF eax!=TRUE
    invoke MessageBox,NULL,addr pipefail,addr AppName,MB_OK
    invoke ExitProcess,1
.ENDIF   

invoke CreatePipe,addr readout,addr newstout,addr secat,0
.IF eax!=TRUE
    invoke CloseHandle,newstdin
    invoke CloseHandle,writein
    invoke MessageBox,NULL,addr pipefail,addr AppName,MB_OK
    invoke ExitProcess,1
.ENDIF 

invoke GetStartupInfo,addr startinfo

mov startinfo.dwFlags,STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW
mov startinfo.wShowWindow,SW_HIDE
mov eax,[newstout]
mov startinfo.hStdOutput,eax
mov startinfo.hStdError,eax
mov eax,[newstdin]
mov startinfo.hStdInput,eax

invoke CreateProcess,NULL,addr AppToSpawn,NULL,NULL,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,addr startinfo,addr procinfo
.IF eax!=TRUE
    invoke CloseHandle,newstdin
    invoke CloseHandle,writein
    invoke CloseHandle,readout
    invoke CloseHandle,newstout
    invoke ExitProcess,1
.ENDIF       

.WHILE 1
    invoke GetExitCodeProcess,procinfo.hProcess,offset exitcode
    .IF eax ==STILL_ACTIVE
        .BREAK
    .ENDIF
    xor eax,eax
    mov bytesRead,eax
    invoke PeekNamedPipe,readout,addr buffer,1023,offset bytesRead,addr avail,NULL
    .IF bytesRead!=0       
        .IF avail >1023
            .WHILE bytesRead>=1023
                invoke ReadFile,readout,addr buffer,1023,addr bytesRead,NULL
               
                invoke StdOut,addr buffer
                mov eax,offset buffer
                mov ecx,sizeof buffer
                zeromem2:
                mov byte ptr [eax],0
                inc eax
                dec ecx
                test ecx,ecx
                jnz zeromem2
            .ENDW
        .ELSE
            invoke ReadFile,readout,addr buffer,1023,addr bytesRead,NULL
            invoke StdOut,addr buffer
        .ENDIF
    .ENDIF
    mov eax,offset buffer
    mov ecx,sizeof buffer
    zeromem:
    mov byte ptr [eax],0
    inc eax
    dec ecx
    test ecx,ecx
    jnz zeromem
    invoke StdIn,addr buffer,sizeof buffer
    invoke lstrlen,addr buffer
    mov ecx,eax
    sub ecx,2
    mov eax,offset buffer
    add eax,ecx
    mov byte ptr [eax],13
    mov byte ptr [eax+1],10
    mov byte ptr [eax+2],0
    invoke WriteFile,writein,addr buffer,ecx,addr bytesWrit,NULL
   
    mov eax,offset buffer
    mov ecx,sizeof buffer
    zeromem1:
    mov byte ptr [eax],0
    inc eax
    dec ecx
    test ecx,ecx
    jnz zeromem1
.ENDW
         

invoke CloseHandle,procinfo.hThread
invoke CloseHandle,procinfo.hProcess
invoke CloseHandle,newstdin
invoke CloseHandle,writein
invoke CloseHandle,readout
invoke CloseHandle,newstout

invoke ExitProcess,0

end start



thanks in advance ;)