News:

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

execute another process at exit

Started by dedndave, February 05, 2009, 11:19:18 AM

Previous topic - Next topic

dedndave

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.

BlackVortex

You can do this when handling the WM_CLOSE message, or maybe WM_DESTROY.
And ShellExecute.

MichaelW

Depending on what you are trying to do, you can use one of the CRT exec or spawn functions.
eschew obfuscation

dedndave

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

ToutEnMasm


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

MichaelW

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

eschew obfuscation

jj2007

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

kromag

Thanks for the info; I was looking for this myself!
:thumbu
---
William

dedndave

thank you everyone - launch - very cool