Results 1 to 4 of 4

Thread: Shellexecute end?

  1. #1

    Thread Starter
    Hyperactive Member Zaphod64831's Avatar
    Join Date
    Mar 2000
    Posts
    268

    Question

    I know someone out there knows this, because I've seen it myself somewhere...

    How can I detect when a program I've shelled with ShellExecute has finished?

    I need something that loops, if possible, so that I can keep it within a single form's sub to ease things along. Something along the lines of:

    Code:
    Do While (StillExecuting)
    DoEvents
    Loop
    So that I can put in the code to execute after it's finished immediately after the loop.

    Thanks in advance.
    Email: [email protected]

    Home Page: www.olemac.net/~hutch

    I'm bored, VERY bored, and I got bored with my sig. So I changed it to this.

  2. #2
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    Try taking a look at this (I've never used it, but heard it works):

    http://www.planet-source-code.com/vb...txtCodeId=1102

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Here you go:
    Code:
    Public Type STARTUPINFO
        cb As Long
        lpReserved As String
        lpDesktop As String
        lpTitle As String
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End Type
    
    Public Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessID As Long
        dwThreadID As Long
    End Type
    
    Public Declare Function WaitForSingleObject _
     Lib "kernel32" ( _
     ByVal hHandle As Long, _
     ByVal dwMilliseconds As Long) As Long
    
    Public Declare Function CreateProcess _
     Lib "kernel32" Alias "CreateProcessA" ( _
     ByVal lpApplicationName As Long, _
     ByVal lpCommandLine As String, _
     ByVal lpProcessAttributes As Long, _
     ByVal lpThreadAttributes As Long, _
     ByVal bInheritHandles As Long, _
     ByVal dwCreationFlags As Long, _
     ByVal lpEnvironment As Long, _
     ByVal lpCurrentDirectory As Long, _
     lpStartupInfo As STARTUPINFO, _
     lpProcessInformation As PROCESS_INFORMATION) As Long
    
    Public Declare Function CloseHandle _
     Lib "kernel32" ( _
     ByVal hObject As Long) As Long
    
    Public Const NORMAL_PRIORITY_CLASS = &H20&
    Public Const INFINITE = -1&
    Public Const STARTF_USESHOWWINDOW = &H1
    
    Public Sub ShellAndWait(sCmd As String, Optional WindowStyle As VbAppWinStyle = vbNormalFocus)
        Dim udtStart As STARTUPINFO
        Dim udtProc As PROCESS_INFORMATION
        
        udtStart.cb = Len(udtStart)
        udtStart.dwFlags = STARTF_USESHOWWINDOW
        udtStart.wShowWindow = WindowStyle
        CreateProcess 0&, sCmd, 0&, 0&, 1&, _
         NORMAL_PRIORITY_CLASS, 0&, 0&, udtStart, udtProc
        WaitForSingleObject udtProc.hProcess, INFINITE
        CloseHandle udtProc.hProcess
    End Sub
    Just call the ShellAndWait sub and pass the command line as the first argument and optionally the state for the window.
    Code:
    ShellAndWait "Notepad.exe", vbMaximizedFocus
    If you want to call a DOS application please do so by calling the COMMAND.COM or CMD.EXE with the /C parameter for the application to "self close".
    Code:
    Dim sCmd As String
    
    sCmd = Environ("COMSPEC") & " /C "
    sCmd = sCmd & "c:\MyBatFile.Bat"
    ShellAndWait sCmd, vbHide
    Good luck!

  4. #4

    Thread Starter
    Hyperactive Member Zaphod64831's Avatar
    Join Date
    Mar 2000
    Posts
    268
    Thanks, Joacim, your seemed real good but I got an error while running it.

    I ended up using reeset's, which turned out to work just fine.

    Thanks both
    Email: [email protected]

    Home Page: www.olemac.net/~hutch

    I'm bored, VERY bored, and I got bored with my sig. So I changed it to this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width