The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: dedndave on February 05, 2009, 11:19:18 AM

Title: execute another process at exit
Post by: dedndave on February 05, 2009, 11:19:18 AM
What I would like to do is, run a second program, as the first one exits.
I know this can be done - as an example, when you terminate Yahoo Messenger, it runs ymsgr_tray.exe (which is just a tray icon).
In the old days (i.e. 16-bit world), we had the exec function.
I do not recall if you could exit the parent process (while the child is still running) or not with that function.
Title: Re: execute another process at exit
Post by: BlackVortex on February 05, 2009, 12:33:47 PM
You can do this when handling the WM_CLOSE message, or maybe WM_DESTROY.
And ShellExecute.
Title: Re: execute another process at exit
Post by: MichaelW on February 05, 2009, 12:53:10 PM
Depending on what you are trying to do, you can use one of the CRT  exec (http://msdn.microsoft.com/en-us/library/431x4c1w(VS.71).aspx) or  spawn (http://msdn.microsoft.com/en-us/library/20y988d2(VS.71).aspx) functions.
Title: Re: execute another process at exit
Post by: dedndave on February 05, 2009, 02:19:47 PM
Guys! Yikes ! - lol
1) I didn't want to read 4 books to get there - lol
2) This is more what I was hoping for as an answer, and I hope it helps other beginners....
This example runs Yahoo Messenger

        include c:\masm32\include\masm32rt.inc

        .data

SUInfo  STARTUPINFO <>
PrcInfo PROCESS_INFORMATION <>

PrcName db      'C:\Program Files\Yahoo!\Messenger\YahooMessenger.exe',0

        .code

start:  call    _main
        invoke  ExitProcess,
                NULL

_main   proc

        invoke  CreateProcess,
                ADDR PrcName,
                NULL,
                NULL,
                NULL,
                NULL,
                CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS,
                NULL,
                NULL,
                ADDR SUInfo,
                ADDR PrcInfo
        ret

_main   endp

        end     start
Title: Re: execute another process at exit
Post by: ToutEnMasm on February 05, 2009, 04:07:03 PM

To make this,run a program and exit,you must closed some handle.

Quote
Execute_Independant_Win proc hexeProg:DWORD ,NomProg:DWORD, Param:DWORD ;la ligne parametre addr
;-------- avec XP, pass only on line in Param
   ;---------- creer le process
   mov startInfo.cb,sizeof STARTUPINFO
   
   invoke GetStartupInfo,ADDR startInfo
   invoke CreateProcess,hexeProg,Param,NULL,NULL,FALSE,\
      NORMAL_PRIORITY_CLASS,NULL,NULL,ADDR startInfo,ADDR processInfo
   .if eax == FALSE
   INVOKE     MessageBox, NULL,SADR("Execute_Independant_Win failed"),\
            SADR("CreateProcess Failed"), MB_YESNO
   .else
      ;lIndependant process
      invoke CloseHandle,processInfo.hProcess
      mov processInfo.hProcess,0
      invoke CloseHandle,processInfo.hThread
      mov processInfo.hThread,0
   .endif
   
FindeExecute_Independant_Win:
   ret
Execute_Independant_Win endp
Title: Re: execute another process at exit
Post by: MichaelW on February 05, 2009, 04:51:12 PM
The CRT functions are very easy to use.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    _P_WAIT   equ 0
    _P_NOWAIT equ 1
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    ;-------------------------------------------------------------------
    ; This will suspend the calling thread until the new process exits.
    ;-------------------------------------------------------------------

    invoke crt__spawnlp, _P_WAIT, chr$("calc.exe"), NULL

    inkey "Press any key to continue..."

    ;-------------------------------------------------------------------
    ; Note in the following that the first argument for the new process
    ; must have the leading space.
    ;-------------------------------------------------------------------

    ;------------------------------------------------------------
    ; This will launch the new process and return to the caller.
    ;------------------------------------------------------------

    invoke crt__spawnlp, _P_NOWAIT,
                         chr$("notepad.exe"),
                         chr$(" \masm32\tproc.txt"), NULL

    inkey "Press any key to continue..."

    ;--------------------------------------------------------
    ; Here the new process will replace the calling process.
    ;--------------------------------------------------------

    invoke crt__execlp,  chr$("notepad.exe"),
                         chr$(" \masm32\makelibs.bat"), NULL

    ;--------------------------------------------
    ; This will execute only if the above fails.
    ;--------------------------------------------
   
    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

Title: Re: execute another process at exit
Post by: jj2007 on February 05, 2009, 07:13:04 PM
You can also use the standard Win32 API functions via the Launch macro. Usage:

Launch "notepad.exe \masm32\qeditor.ini"      ; simple, returns immediately, Notepad stays open
Launch "notepad.exe \masm32\qeditor.ini", SW_MAXIMZE   ; returns immediately, Notepad stays open maximized
Launch "notepad.exe \masm32\qeditor.ini", SW_MAXIMZE, -1 ; waits until Notepad is being closed
Launch "notepad.exe \masm32\qeditor.ini", SW_MAXIMZE, 5000 ; returns after 5 seconds

.nolist
include \masm32\include\masm32rt.inc

ShellWait PROTO: DWORD, :DWORD, :DWORD

Launch MACRO cmd:REQ, showSW:=<SW_NORMAL>, timeout:=<0>
LOCAL ShowMode
  ShowMode equ <showSW>
  ifidn ShowMode, <SW_HIDE>
ShowMode equ <SW_HIDE or 65536>
  endif
  invoke ShellWait, reparg(cmd), ShowMode, timeout
ENDM
; ---------------------------------------------------------------

.code
start:
; executable and commandline [, ShowMode] [, Timeout in ms]
Launch "notepad.exe \masm32\qeditor.ini", SW_SHOWNA, 5000
.if eax
MsgBox 0, "Finished! Try again?", "Launch:", MB_YESNO
.if eax==IDYES
Launch "notepad.exe" ; the simplest form
.endif
.else
MsgBox 0, "There was a problem, sorry", "Launch:", MB_OK
.endif

exit ; short form of invoke ExitProcess, 0

; ---------------------------------------------------------------
ShellWait proc cmdline:DWORD, SW_x:DWORD, timeout:DWORD
LOCAL ExCode:DWORD ; exit code, returned in ecx
LOCAL sinfo:STARTUPINFO
LOCAL pinfo:PROCESS_INFORMATION
  invoke GetStartupInfo, addr sinfo ; fill the structure
  mov eax, SW_x
  .if eax
mov sinfo.dwFlags, STARTF_USESHOWWINDOW ; these two are
mov sinfo.wShowWindow, ax ; optional but useful
  .endif
  xor ecx, ecx ; using ecx as NULL saves some bytes
  mov ExCode, ecx ; exit code valid only if eax!=0
  invoke CreateProcess, ecx, ; no pathname
  cmdline, ; the only bit that REALLY counts...!
  ecx, ecx, ecx, ; lpProcessAttributes, lpThreadAttributes, bInheritHandles
  ecx, ecx, ecx, ; xx_PRIORITY_CLASS, lpEnvironment, lpCurrentDirectory
  addr sinfo, ; read from STARTUPINFO
  addr pinfo ; write to PROCESS_INFORMATION
  .if eax
mov eax, pinfo.hProcess ; wow, we got a handle! Actually, we got two...
.if eax
invoke WaitForSingleObject, eax, timeout ; specify milliseconds here
invoke GetExitCodeProcess, pinfo.hProcess, addr ExCode
invoke CloseHandle, pinfo.hProcess
invoke CloseHandle, pinfo.hThread
mov eax, pinfo.hProcess
.endif
  .endif
  mov ecx, ExCode
  ret
ShellWait endp
END start
Title: Re: execute another process at exit
Post by: kromag on February 05, 2009, 07:54:44 PM
Thanks for the info; I was looking for this myself!
:thumbu
---
William
Title: Re: execute another process at exit
Post by: dedndave on February 07, 2009, 12:19:40 AM
thank you everyone - launch - very cool