News:

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

CreateProcess

Started by ragdog, January 06, 2008, 03:43:59 PM

Previous topic - Next topic

ragdog

I have a little problem with createprocess and wait until the process is terminated!
   
But that was with WaitForSingleObject,ProcessInfo.hProcess,INFINITE or not?



_CreateProcess Proc lpszTarget:DWORD
      invoke ZeroMemory,addr StartupInfo,sizeof STARTUPINFO
      invoke ZeroMemory,addr ProcessInfo,sizeof PROCESS_INFORMATION
     
      invoke CreateProcess,NULL,lpszTarget,NULL,NULL, \
                      FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,addr StartupInfo,addr ProcessInfo
.if !eax
                         invoke MessageBox,0,CTEXT("Process not startet"),0,MB_OK
                             jmp @F
  .else
     invoke WaitForSingleObject,ProcessInfo.hProcess,INFINITE
                     invoke MessageBox,0,CTEXT("Process is closed"),0,MB_OK
.endif
   @@:
         invoke CloseHandle,ProcessInfo.hProcess
         invoke CloseHandle,ProcessInfo.hThread
              ret
_CreateProcess endp


greets
ragdog

akane

You did forget to set StartupInfo.cb to sizeof(STARTUPINFO). Zeroing ProcessInfo is not neccesary, and the @@ label should be after the last CloseHandle.
In your code when the process fails to start, youre calling CloseHandle with invalid parameters.

ragdog

thanks for the information :U

greets
ragdog

Vortex

Hi ragdog,

Here is the Shell subroutine from the GeneSys project for the same task :

    .486                      ; create 32 bit code
    .model flat, stdcall      ; 32 bit memory model
    option casemap :none      ; case sensitive

    include \masm32\include\windows.inc
    include \masm32\include\kernel32.inc

    .code

;----------------------------------------------------------------------------;
; This is a translation of a shell script originally written by Edgar Hansen ;
; in GoASM code. Translation by Paul E. Brennick  <pebrennick@verizon.net>   ;
;                                                                            ;
; Shell:      This function will run an executable and wait until it has     ;
;             finished before continuing. The function provides for a time   ;
;             out for a maximum wait period.                                 ;
; Parameters: lpfilename = The fully qualified path to an executable to run  ;
;             dwTimeOut = The amount of time in milliseconds to wait, -1 for ;
;             no timeout                                                     ;
; Returns:    0 if successful, STATUS_TIMEOUT if the timeout has elapsed     ;
;             -1 if there was an error creating the process                  ;
;                                                                            ;
;----------------------------------------------------------------------------;

Shell PROC lpfilename:DWORD, dwTimeOut:DWORD
    LOCAL   Sh_st_info :STARTUPINFO
    LOCAL   Sh_pr_info :PROCESS_INFORMATION

    mov     DWORD PTR [Sh_st_info.cb], SIZEOF STARTUPINFO
    invoke  GetStartupInfoA, ADDR Sh_st_info
    mov     DWORD PTR [Sh_st_info.lpReserved], 0
    invoke  CreateProcess, 0, [lpfilename], 0, 0, 0, 0, 0, 0, \
            ADDR Sh_st_info, ADDR Sh_pr_info
    test    eax, eax
    jz ERROR
    invoke  WaitForSingleObject, [Sh_pr_info.hProcess], [dwTimeOut]
    push    eax
    invoke  CloseHandle, [Sh_pr_info.hThread]
    invoke  CloseHandle, [Sh_pr_info.hProcess]
    pop     eax
    test    eax, eax
    RET
ERROR:
    xor     eax, eax
    sub     eax, 1
    RET
Shell ENDP

END

ragdog

 :bg Vortex Thank you! I have found exactly that in this board and thus solved my probelm :U