Running an external program from an ASM running program [?]

Started by frktons, September 16, 2010, 01:30:28 PM

Previous topic - Next topic

frktons

Quote from: Tedd on September 16, 2010, 08:44:52 PM
Checking the API documentation is a useful skill. Things become much clearer when you actually try them out.

INVOKE CreateProcess,DWORD PTR PtrFileName,0,0,0,0,0,0,0,ADDR StartUpInfo,ADDR ProcessInfo
;return value is a handle to the created process (assuming it succeeds)

mov hProcess,eax   ;save it for future use

INVOKE WaitForSingleObject, hProcess,-1

;your program now waits for the process to finish,
;returning to this point when it has exited



Well I guess you didn't put this code inside my example and assemble/run it, otherwise you'd have
noticed that nothing has changed adding these instructions.  ::)

Frank
Mind is like a parachute. You know what to do in order to use it :-)

Tedd

Speaking of checking the documentation.. :dazzled: (The return value isn't hProcess..)


INVOKE CreateProcess,DWORD PTR PtrFileName,0,0,0,TRUE,0,0,0,ADDR StartUpInfo,ADDR ProcessInfo

mov eax,[ProcessInfo.hProcess]
INVOKE WaitForSingleObject, eax,-1

;your program now waits for the process to finish,
;returning to this point when it has exited

Also, setting bInheritHandles=TRUE means the process uses the parent's console.


Working example attached.
No snowflake in an avalanche feels responsible.

frktons

Quote from: Tedd on September 16, 2010, 09:05:03 PM
Speaking of checking the documentation.. :dazzled: (The return value isn't hProcess..)


INVOKE CreateProcess,DWORD PTR PtrFileName,0,0,0,TRUE,0,0,0,ADDR StartUpInfo,ADDR ProcessInfo

mov eax,[ProcessInfo.hProcess]
INVOKE WaitForSingleObject, eax,-1

;your program now waits for the process to finish,
;returning to this point when it has exited

Also, setting bInheritHandles=TRUE means the process uses the parent's console.


This looks far more better  :U

The content of the parent console is cleared by the child process.
If I want to retain the old content what should I set?
bInheritHandles=FALSE ?
But I already pass 0 = FALSE to CreateProcess.

Maybe I should save the content somewhere and restore it when
returning back from the called process?

  ::)
Mind is like a parachute. You know what to do in order to use it :-)

jj2007

Quote from: Tedd on September 16, 2010, 09:05:03 PM
Also, setting bInheritHandles=TRUE means the process uses the parent's console.

What exactly does that mean? I have tried to produce this but can't see a difference:
include \masm32\MasmBasic\MasmBasic.inc ; Press F6 to assemble & link
Init
PrintLine "Ciao daddy"
Exit
end start

include \masm32\MasmBasic\MasmBasic.inc
Init
PrintLine "Ciao baby"
Launch "TheChild.exe", SW_SHOW, 100 ; do not inherit handles
PrintLine "Ciao baby"
Launch "TheChild.exe", SW_SHOW, 101 ; do inherit handles
PrintLine "Ciao baby"
Exit
end start

Output:
Ciao baby
Ciao daddy
Ciao baby
Ciao daddy
Ciao baby


So in both cases the child's output appears in the parent's console window...
::)

frktons

Probably you have to compile both as /SUBSYSTEM:WINDOW
to see any difference, otherwise the child process inherit the parent's console.

This is my best guess for the time being

And moreover, it works this way  :P

Frank
Mind is like a parachute. You know what to do in order to use it :-)

frktons

This is the complete program that works fine calling GUI programs
and console ones, provided that all the programs involved, caller and called
programs, are assembled and linked with the /SUBSYSTEM:CONSOLE
option.

Frank
Mind is like a parachute. You know what to do in order to use it :-)

Antariy

As far as I know - we can use CREATE_NEW_CONSOLE flag in dwCreationFlags (6th param of CreateProcess, starting from 1) - to make child with its own console.



Alex

frktons

Quote from: Antariy on September 16, 2010, 10:24:49 PM
As far as I know - we can use CREATE_NEW_CONSOLE flag in dwCreationFlags (6th param of CreateProcess, starting from 1) - to make child with its own console.
Alex

Hi Alex, nice to see that your english is much better now.  :U

Yoy mean this:

INVOKE CreateProcess,DWORD PTR PtrFileName,0,0,0,0,1,0,0,ADDR StartUpInfo,ADDR ProcessInfo
?
or something different?

Frank
Mind is like a parachute. You know what to do in order to use it :-)

Antariy

Hi, Frank!

No, my English is not good - just when I spent time to phrases construction - they are looks better :)
When I'm online - I hurry - my English is really AWESOME.

I meant this:

INVOKE CreateProcess,DWORD PTR PtrFileName,0,0,0,0,CREATE_NEW_CONSOLE,0,0,ADDR StartUpInfo,ADDR ProcessInfo


Flags not always is triggers - usually they are set of bits - i.e. set of flags. If some flag have type BOOL - it have only two meanings: true - not null, and false - null. If flag is not BOOL-type - this can mean anything under windows :) Need to read info about API in this case. Try to find win32api.hlp - dowload it from PB site, for example.

EDITED: two mystypeing cam->can and "phrases" :)



Alex

frktons

Quote from: Antariy on September 16, 2010, 10:35:55 PM
Hi, Frank!

No, my English is not good - just when I spent time to phares construction - they are looks better :)
When I'm online - I hurry - my English is really AWESOME.

I meant this:

INVOKE CreateProcess,DWORD PTR PtrFileName,0,0,0,0,CREATE_NEW_CONSOLE,0,0,ADDR StartUpInfo,ADDR ProcessInfo


Flags not always is triggers - usually they are set of bits - i.e. set of flags. If some flag have type BOOL - it have only two meanings: true - not null, and false - null. If flag is not BOOL-type - this cam mean anything under windows :) Need to read info about API in this case. Try to find win32api.hlp - dowload it from PB site, for example.

Alex


Good! This flag makes compiling with /SUBSYSTEM:WINDOWS not necessary for called progs.

Much better this way  :U

I did read it into the winapi.hlp, but I didnt know how to use it. Now it is more clear.

Thanks

Frank

Mind is like a parachute. You know what to do in order to use it :-)

jj2007

From my testing, it seems that
- bInheritHandles can be zero
- dwCreationFlags decides on a new console with CREATE_NEW_CONSOLE (so Alex was right :U)
- both parent and child must be created as console apps.

frktons

Quote from: jj2007 on September 17, 2010, 09:52:01 AM
From my testing, it seems that
- bInheritHandles can be zero
- dwCreationFlags decides on a new console with CREATE_NEW_CONSOLE (so Alex was right :U)
- both parent and child must be created as console apps.

I agree with the first two, for the third, try again.  :P

My example is compiled with /SUBSYSTEM:WINDOWS and runs an external exe that can be
both Console or GUI mode. They must not be created as console apps, both of them
can be any combination of console/GUI, and work together the same, according to my tests.

Frank
Mind is like a parachute. You know what to do in order to use it :-)