flat assembler
Message board for the users of flat assembler.

Index > Windows > problem with pipes [SOLVED]

Author
Thread Post new topic Reply to topic
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 03 Jan 2014, 02:22
I am having trouble redirecting the stdin/stdout of a console program. Following several examples on the internet I have parent.asm and child.asm in the same directory:
parent.asm[CORRECTED]:
Code:
format PE GUI 5.0
entry Start
include 'win32axp.inc'
struct SECURITY_ATTRIBUTES
        nLength                dd ?
        lpSecurityDescriptor   dd ?
        bInheritHandle         dd ?
ends

section '.code' code readable executable
Start:
                        mov  dword[saAttr.nLength],sizeof.SECURITY_ATTRIBUTES
                        mov  dword[saAttr.lpSecurityDescriptor],NULL
                        mov  dword[saAttr.bInheritHandle],TRUE
                     invoke  CreatePipe,g_hChildStd_OUT_Rd,g_hChildStd_OUT_Wr,saAttr,0
                     invoke  CreatePipe,g_hChildStd_IN_Rd,g_hChildStd_IN_Wr,saAttr,0
                        xor  eax,eax
                        mov  ecx,sizeof.PROCESS_INFORMATION
                        lea  edi,[piProcInfo]
                  rep stosb
                        xor  eax,eax
                        mov  ecx,sizeof.STARTUPINFO
                        lea  edi,[siStartInfo]
                  rep stosb
                        mov  dword[siStartInfo.cb],sizeof.STARTUPINFO
                        mov  eax,dword[g_hChildStd_OUT_Wr]
                        mov  dword[siStartInfo.hStdError],eax
                        mov  dword[siStartInfo.hStdOutput],eax
                        mov  eax,dword[g_hChildStd_IN_Rd]
                        mov  dword[siStartInfo.hStdInput],eax
                        mov  dword[siStartInfo.dwFlags],STARTF_USESTDHANDLES;+STARTF_USESHOWWINDOW
                    ;    mov  dword[siStartInfo.wShowWindow],SW_HIDE
                     invoke  CreateProcess,NULL,'child',NULL,NULL,TRUE,0,NULL,NULL,siStartInfo,piProcInfo
                     invoke  WriteFile,[g_hChildStd_IN_Wr],'hello',5,temp, NULL
                        mov  dword[text],'no'
                     invoke  ReadFile,[g_hChildStd_OUT_Rd],text,1024,temp, NULL
                     invoke  MessageBoxA,0,text,0,MB_OK
                     invoke  ExitProcess,0

section '.idata' import data readable writeable
 library kernel32,'KERNEL32.DLL',\
         user32,'USER32.DLL',\
         gdi32,'GDI32.DLL'

include 'api\kernel32.inc'
include 'api\user32.inc'
include 'api\gdi32.inc'

section '.data' data readable writeable
saAttr       SECURITY_ATTRIBUTES
piProcInfo   PROCESS_INFORMATION
siStartInfo  STARTUPINFO
g_hChildStd_OUT_Rd dd ?
g_hChildStd_OUT_Wr dd ?
g_hChildStd_IN_Rd  dd ?
g_hChildStd_IN_Wr  dd ?
temp dd ?
text rb 1024
    
child.asm:
Code:
format PE console
entry Start

include 'win32axp.inc'

section '.code' code readable executable

Start:               invoke  GetStdHandle,STD_OUTPUT_HANDLE
                        mov  [hStdout],eax
                     invoke  GetStdHandle,STD_INPUT_HANDLE
                        mov  [hStdin],eax
                     invoke  ReadFile,[hStdin],text,1024,temp,NULL
                     invoke  WriteFile,[hStdout],'got it',6,temp,NULL
                     invoke  ExitProcess,0

section '.idata' import data readable writeable
library kernel32,'KERNEL32.DLL'
include 'api\kernel32.inc'

section '.data' data readable writeable
hStdin  dd ?
hStdout dd ?
temp    dd ?
text    rb 1024    

If it was working correctly, the message box should have 'got it' in it but instead it just has 'no'. I have copied the examples accurately - what could be going wrong? (corrected version works fine now)


Last edited by tthsqe on 03 Jan 2014, 12:34; edited 3 times in total
Post 03 Jan 2014, 02:22
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4121
Location: vpcmpistri
bitRAKE 03 Jan 2014, 06:36
In parent.asm, put brackets around the handle pointers in the WriteFile/ReadFile to use the handles instead of the memory addresses. {Made the same mistake several times myself.} Correct usage in child.asm.

Nice example of pipe usage.
Post 03 Jan 2014, 06:36
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 03 Jan 2014, 12:33
oops - wish I didn't make that silly mistake. Also, for a 64bit version it seems that
Code:
struct SECURITY_ATTRIBUTES
        nLength                dd ?,?
        lpSecurityDescriptor   dq ?
        bInheritHandle         dd ?
ends      

is the correct structure. Note that nLength takes up 8 bytes.
Post 03 Jan 2014, 12:33
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 03 Jan 2014, 12:49
You can still use 32bit api structures on 64bit windows. Your app will run under Wow64 Wink
Post 03 Jan 2014, 12:49
View user's profile Send private message Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 03 Jan 2014, 12:51
tthsqe wrote:
oops - wish I didn't make that silly mistake. Also, for a 64bit version it seems that
Code:
struct SECURITY_ATTRIBUTES
        nLength                dd ?,?
        lpSecurityDescriptor   dq ?
        bInheritHandle         dd ?
ends      

is the correct structure. Note that nLength takes up 8 bytes.


I think nLenght takes only 4 bytes also at x64. The redundant dword is for aligning the following pointer (the pointer lpSecurityDescriptor is qword and should be aligned at qword boundary)
Post 03 Jan 2014, 12:51
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 05 Jan 2014, 01:36
If lpSecutriyDescriptor - nLength = 4, on 64 bit windows 7, the call to CreatePipe fails. If you want to check out CreatePipe, the address of the securtiy attributes get moved to rsi and the second syscall fails.
Post 05 Jan 2014, 01:36
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 05 Jan 2014, 02:55
tthsqe wrote:
If lpSecutriyDescriptor - nLength = 4, on 64 bit windows 7, the call to CreatePipe fails. If you want to check out CreatePipe, the address of the securtiy attributes get moved to rsi and the second syscall fails.


Also in case you didn't know some APIs that take structures will fail if their memory is not DWORD aligned.
Post 05 Jan 2014, 02:55
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.