Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Win 32: Monitor Process with ShellExecuteEx

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Claudio DalZovo
    replied
    If I understand well in this example the pause waiting for the shell to complete is in WaitForSingleObjectEx.
    While execution is trapped in this function, dialog message loop is active ?
    (which is what I need).

    Thanks


    ------------------

    Leave a comment:


  • Michael Mattias
    started a topic Win 32: Monitor Process with ShellExecuteEx

    Win 32: Monitor Process with ShellExecuteEx

    Code:
    #IF 0
    ' NAME  : monshex.bas
    ' AUTHOR: Michael Mattias Racine WI
    ' DATE  : 6/21/2001
    ' PURPOSE: Demonstration of process monitoring using ShellExecute in lieu of CreateProcess
    '  The author read in a Usenet newsgroup thus:
     ' [using ] RMCOBOL, cannot figure out if I can interface to the dos command shell
     ' to use certain operating system features....
     ' I have a client going for Windows Certification with his software. In order to
     '  get [Microsoft] "Platinum" certification you can't use CreateProcess in your application.
     '  The "preferred" way according to MS is to use the ShellExecute API."
     '  The only problem I have with that is that there seems to be no way to
     ' "wait" for a process to complete before returning to the spawning process.
     '  With CreateProcess I can use a "monitoring" call to wait for the process to complete before continuing.
    ' =====================================================================================
    ' USAGE: Change the program and file names in procedure 'DoTheShellEx,' compile and run
    ' =====================================================================================
    ' COPYRIGHT STATUS: Placed in the public domain by the author 6/21/01
    #ENDIF
    
    
    #COMPILE EXE
    #REGISTER NONE
    #DIM ALL
    #INCLUDE "win32api.INC"
    
    ' TEXT MESSAGES FROM Win32API  'GetLastError' calls
    DECLARE FUNCTION SystemErrorMessageText (BYVAL ECode AS LONG) AS STRING
    FUNCTION SystemErrorMessageText (BYVAL ECode AS LONG) AS STRING
      LOCAL Buffer AS ASCIIZ * 255
      FormatMessage %FORMAT_MESSAGE_FROM_SYSTEM, BYVAL %NULL, ECode, %NULL, buffer, SIZEOF(buffer), BYVAL %NULL
      FUNCTION = FORMAT$(ECode, "#####") & Buffer
    END FUNCTION
    
    %IDC_OPEN_DOCUMENT  = 101   ' control IDs
    %IDC_OPEN_PROGRAM   = 102
    %IDC_PRINT_DOCUMENT = 103
    %IDC_QUIT           = 104
    
    DECLARE FUNCTION DoTheShellEx (BYVAL hWnd AS LONG, BYVAL ControlId AS LONG) AS LONG
    
    CALLBACK FUNCTION DialogProc () AS LONG
      LOCAL stat AS LONG
      IF CBCTLMSG= %BN_CLICKED THEN
                  SELECT CASE CBCTL
                         CASE %IDC_OPEN_DOCUMENT, %IDC_OPEN_PROGRAM, %IDC_PRINT_DOCUMENT
                              stat = DoTheShellEx (CBHNDL, CBCTL)
                              setfocus CBHNDL
                          CASE %IDC_QUIT
                               DIALOG END CBHNDL
                  END SELECT
      END IF
                   
    END FUNCTION
    
    FUNCTION WINMAIN (BYVAL hInstance     AS LONG, _
                      BYVAL hPrevInstance AS LONG, _
                      lpCmdLine           AS ASCIIZ PTR, _
                      BYVAL iCmdShow      AS LONG) AS LONG
    
       LOCAL Stat AS LONG, hDlg AS LONG
       
       DIALOG NEW 0, "ShellExecute Process Monitoring",,, 240, 120,, TO hDlg
       CONTROL ADD BUTTON, hDlg, %IDC_OPEN_DOCUMENT,  "Open Doc"   , 20, 40, 60,16
       CONTROL ADD BUTTON, hDlg, %IDC_OPEN_PROGRAM,   "Open Program", 90, 40, 60,16
       CONTROL ADD BUTTON, hDlg, %IDC_PRINT_DOCUMENT, "Print Doc"   , 170, 40, 60,16
       CONTROL ADD BUTTON, hDlg, %IDC_QUIT, "Quit"                  , 80, 100, 60,14
       
       DIALOG SHOW MODAL hDlg, CALL DialogProc
    
    END FUNCTION
    
    FUNCTION DoTheShellEx (BYVAL hwnd AS LONG, BYVAL ControlID AS LONG) AS LONG
    
       LOCAL SEI AS ShellExecuteInfo, Stat AS LONG, E AS LONG
       
       LOCAL hWaitProcess AS LONG
       LOCAL lpVerb AS ASCIIZ * 20, lpParameters AS ASCIIZ * 20, lpDirectory AS ASCIIZ * 20, lpFile AS ASCIIZ * %MAX_PATH
       LOCAL TimeOut2 AS LONG   ' "timeout" is another un-hilited "reserved" word which is truly only reserved
                                '  in the context of a TCP or UDP verb and can only be found with "search."
                                '  The error message if you use 'timeout' as a variable name is
                                '  "statement expected," which is a useless error message.
       
       SELECT CASE ControlId
              CASE %IDC_OPEN_DOCUMENT
                  lpVerb = "Open"
                  lpParameters   = ""
                  lpFile = "C:\TestShExec.txt"
                  Timeout2 = 30 * 1000            ' 30 secs
               CASE %IDC_PRINT_DOCUMENT
                  lpVerb = "PRINT"
                  lpFile = "C:\TestShExec.txt"
                  lpParameters = ""
                  Timeout2 = 60 * 1000            ' 60 secs
               CASE  %IDC_OPEN_PROGRAM
                 'Windows Program
                 lpVerb = "Open"
                 lpFile = "notepad.exe"
                lpParameters = "C:\TestShExec.txt"
                 ' DOS PROGRAM
                 lpFile ="C:\Software_Development\Pb-Dos\Exe\horse.exe"
                 lpParameters = ""
                 Timeout2 = 30 * 1000            ' 30 secs
       END SELECT
    
       SEI.cbSize       = SIZEOF(SEI)
       SEI.fmask        = %SEE_MASK_NOCLOSEPROCESS
       SEI.hWnd         = hWnd
       SEI.lpVerb       = VARPTR(lpVerb)
       SEI.LpFile       = VARPTR(lpFile)
       SEI.lpParameters = VARPTR (lpParameters)
       SEI.lpDirectory  = %NULL
       SEI.nShow        = %SW_SHOW      ' should be zero for a document file PER MSDN DOC.
                                        ' MSDN DOC IS WRONG, use SW_SHOW!
       SEI.hInstApp     = 0             ' updated by function
       SEI.lpIdList     = %NULL         ' here down to hprocess ignored unless appropriate mask included in fmask
       SEI.lpClass      = %NULL
       SEI.hkeyClass    = %NULL
       SEI.dwHotKey     = %NULL
       SEI.item         = %NULL
       SEI.hProcess     = 0             ' will be updated by ShellExecuteEx
    
       IF ISTRUE ShellExecuteEx(SEI) THEN       ' function succeeded and returned
            hWaitProcess = SEI.hProcess
            Stat = WaitForSingleObjectEx (hWaitProcess, Timeout2, 0)
            SELECT CASE Stat
                  CASE -1&
                       E = GetLastError
                       MSGBOX "Wait Failed:" & SystemErrorMessageText (E)
                  CASE %WAIT_OBJECT_0
                       MSGBOX "Wait returned from " & lpVerb & " on process completion"
                  CASE %WAIT_TIMEOUT
                       MSGBOX "Wait returned from " & lpVerb & " on Timeout"
                  CASE ELSE
                       MSGBOX "Unexpected Return, code=" & STR$(Stat)
            END SELECT
            CloseHandle hWaitProcess
      ELSE
          E = GetLastError
          MSGBOX "ShellExecuteEx failed:" & SystemErrorMessageText(E)
      END IF
    
    
    END FUNCTION
    ------------------
    Michael Mattias
    Racine WI USA
    [email protected]
Working...
X