The MASM Forum

General => The Workshop => Topic started by: clamicun on January 05, 2017, 02:35:40 AM

Title: ShellExecute
Post by: clamicun on January 05, 2017, 02:35:40 AM
INVOKE ShellExecute,0,offset fOpen,offset file2execute,offset param,NULL,SW_HIDE   

On success ShellExecute  returns  > 32

But what I need is the exitcode of the executed file.
Is there a possibility ?     
Title: Re: ShellExecute
Post by: nidud on January 05, 2017, 02:48:44 AM
deleted
Title: Re: ShellExecute
Post by: hutch-- on January 05, 2017, 02:53:11 AM
These are the return values.

0  The operating system is out of memory or resources.
ERROR_FILE_NOT_FOUND  The specified file was not found.
ERROR_PATH_NOT_FOUND  The specified path was not found.
ERROR_BAD_FORMAT  The .exe file is invalid (non-Win32® .exe or error in .exe image).
SE_ERR_ACCESSDENIED  The operating system denied access to the specified file. 
SE_ERR_ASSOCINCOMPLETE  The file name association is incomplete or invalid.
SE_ERR_DDEBUSY  The DDE transaction could not be completed because other DDE transactions were being processed.
SE_ERR_DDEFAIL  The DDE transaction failed.
SE_ERR_DDETIMEOUT  The DDE transaction could not be completed because the request timed out.
SE_ERR_DLLNOTFOUND  The specified dynamic-link library was not found. 
SE_ERR_FNF  The specified file was not found. 
SE_ERR_NOASSOC  There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable.
SE_ERR_OOM  There was not enough memory to complete the operation.
SE_ERR_PNF  The specified path was not found.
SE_ERR_SHARE  A sharing violation occurred.
Title: Re: ShellExecute
Post by: clamicun on January 05, 2017, 03:11:03 AM
nidud ... thank you ... I check it out

hutch ... thank you ... like I said > 32 on success or the described errcodes 
Title: Re: ShellExecute
Post by: hutch-- on January 05, 2017, 03:20:26 AM
Yes, sorry about that, you can set the exit code as an argument for ExitProcess() and to get it from another app nidud has given you the answer, from the console a batch file with get the error level.
Title: Re: ShellExecute
Post by: adeyblue on January 05, 2017, 04:42:46 AM
You can use ShellExecuteEx with the SEE_MASK_NOCLOSEPROCESS flag (It's something like that, I'm going from memory), it's basically the same as ShellExecute except you fill a structure instead of passing arguments. Then use GetExitCodeProcess as mentioned above, before closing the handle when you're done.

There isn't a way to get it just using ShellExecute sincec it just starts the process and forgets about it.
Title: Re: ShellExecute
Post by: clamicun on January 06, 2017, 04:17:46 AM
adeyblue, I am working on this-

INVOKE GetExitCodeProcess,ShExecInfo.hProcess,offset exitcode   ;handle from ShellExecuteEx
The handle must have the PROCESS_QUERY_INFORMATION ???
You have an idea ?
Title: Re: ShellExecute
Post by: Vortex on January 06, 2017, 05:30:18 AM
Hi clamicun,

Walking the table of processes, you can get the ID of the process matching the executable name in question. The attachment contains a demo project. Terminating the process SimpleWnd.exe, you will see a message box displaying the exit code.

include GetProcessExitCode.inc

.data

fname   db 'SimpleWnd.exe',0
title1  db 'ExitCode',0
msg     db 'SimpleWnd = %d',0

.data?

sei             SHELLEXECUTEINFO <?>
hProcessSnap    dd ?
pe32            PROCESSENTRY32 <?>
pID             dd ?
ExitCode        dd ?
buffer          db 64 dup(?)

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses esi edi

    mov     esi,OFFSET sei
    xor     eax,eax
    mov     SHELLEXECUTEINFO.cbSize[esi],\
            SIZEOF SHELLEXECUTEINFO
           
    mov     SHELLEXECUTEINFO.fMask[esi],\
            SEE_MASK_NOCLOSEPROCESS
           
    mov     SHELLEXECUTEINFO.hwnd[esi],eax
    mov     SHELLEXECUTEINFO.lpFile[esi],OFFSET fname
    mov     SHELLEXECUTEINFO.lpParameters[esi],eax
    mov     SHELLEXECUTEINFO.lpDirectory[esi],eax
    mov     SHELLEXECUTEINFO.nShow[esi],SW_SHOWNORMAL
    mov     SHELLEXECUTEINFO.hInstApp[esi],eax

    invoke  ShellExecuteEx,esi

    lea     edi,pe32
    invoke  CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,0
    mov     hProcessSnap,eax
    mov     pe32.dwSize,SIZEOF(PROCESSENTRY32)
    invoke  Process32First,eax,edi
@@:
    lea     eax,PROCESSENTRY32.szExeFile[edi]

    invoke  lstrcmpi,eax,ADDR fname
    test    eax,eax
    jz      get_pID

    invoke  Process32Next,hProcessSnap,edi
    test    eax,eax
    jnz     @b

get_pID:

    push    PROCESSENTRY32.th32ProcessID[edi]
    pop     pID

    invoke  CloseHandle,hProcessSnap

    invoke  OpenProcess,PROCESS_QUERY_INFORMATION,0,pID
    invoke  CloseHandle,eax

    invoke  WaitForSingleObject,SHELLEXECUTEINFO.hProcess[esi],\
            INFINITE

    invoke  GetExitCodeProcess,SHELLEXECUTEINFO.hProcess[esi],\
            ADDR ExitCode
           
    invoke  wsprintf,ADDR buffer,ADDR msg,ExitCode
    invoke  MessageBox,0,ADDR buffer,ADDR title1,MB_OK

    ret
   
main ENDP

END start
Title: Re: ShellExecute
Post by: clamicun on January 07, 2017, 08:42:20 AM
vorex, thanks,
looks good, but I can't assemble it, because I don't know where I find Pelles lib.
Title: Re: ShellExecute
Post by: clamicun on January 07, 2017, 08:47:37 AM
ok.
found Pelles site.
Title: Re: ShellExecute
Post by: clamicun on January 07, 2017, 10:28:25 AM
Vortex,
yes runs well.
Thanks