Results 1 to 7 of 7

Thread: how to synchronously "shell"

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    7
    I am using shell() to run another application.
    But how I can know that application has terminated or not.
    Or is there a method I can call another application and wait until it's terminated?

    Thx.
    lupus

  2. #2
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    I posted this a few days ago, but here it is again (slightly modified):

    Wait until an app is loaded

    Code:
    Private Const SYNCHRONIZE As Long = &H100000
    Private Const INFINITE As Long = &HFFFF
    
    Private Declare Function WaitForInputIdle _
                    Lib "user32" _
                    (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CloseHandle _
                    Lib "kernel32" _
                    (ByVal hObject As Long) As Long
    
    Private Declare Function OpenProcess _
                    Lib "kernel32" _
                    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
                    ByVal dwProcessId As Long) As Long
    
    Private Sub cmdRunApp_Click()
       Dim hProcessID As Long
       Dim hProcess As Long
       Dim sShellPath As String
       
       sShellPath = "C:\WINDOWS\NOTEPAD.EXE"
       
       hProcessID = Shell(sShellPath, vbNormalFocus)
       
       If hProcessID <> 0 Then
          hProcess = OpenProcess(SYNCHRONIZE, 0, hProcessID)
          
          If hProcess <> 0 Then
             Call WaitForInputIdle(hProcess, INFINITE)
             Call CloseHandle(hProcess)
          End If
       
       End If
    
    End Sub
    Wait until an app is closed

    Code:
    Private Const SYNCHRONIZE As Long = &H100000
    Private Const INFINITE As Long = &HFFFF
    
    
    Private Declare Function WaitForSingleObject _
                    Lib "kernel32" _
                    (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CloseHandle _
                    Lib "kernel32" _
                    (ByVal hObject As Long) As Long
    
    Private Declare Function OpenProcess _
                    Lib "kernel32" _
                    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
                    ByVal dwProcessId As Long) As Long
    
    Private Sub cmdRunApp_Click()
       Dim hProcessID As Long
       Dim hProcess As Long
       Dim sShellPath As String
       
       sShellPath = "C:\WINDOWS\NOTEPAD.EXE"
       
       hProcessID = Shell(sShellPath, vbNormalFocus)
       
       If hProcessID <> 0 Then
          hProcess = OpenProcess(SYNCHRONIZE, 0, hProcessID)
          
          If hProcess <> 0 Then
             Call Me.Hide
             App.TaskVisible = False
             Call WaitForSingleObject(hProcess, INFINITE)
             Call CloseHandle(hProcess)
             App.TaskVisible = True
             Call Me.Show
          End If
       
       End If
    
    End Sub
    Keep in mind the above code is for 32-bit environments, if you're using 16-bit let me know and I'll post the code for it.

    [Edited by SonGouki on 04-06-2000 at 10:59 AM]

  3. #3
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Hello SonGouki!

    What exactly does the code above do and how would you run it? I tried copying it into a form, although it is still missing some API. I just like to know when I would use something like this.

    Thanks SonGouki

    Chemically Formulated As:
    Dr. Nitro

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    7
    Thanks. It works (after some modification. )

  5. #5
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    Sorry guys, I just edited the code now... I had forgotten to take out the "api" prefixes in front of the calls (my own coding standard). It should work now with no problems.

    Nitro, if you want to see what it does put a MsgBox with vbSystemModal at the end of the cmdRunApp_Click procedure and change the sShellPath to start something that takes time, like Word. Also, I guess I should of mentioned this last time, don't try to step through this code! Also, you'll see what it does better if you compile it into an executable and run that instead of running it in the VB IDE.

    Sorry 'bout the confusion.

  6. #6
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444

    SEARCH WORKS!!!!!!!!!!!!!!!!1111

    I have tried the following code and I am getting an error that I can not create activeX object.
    Code:
    Private Const SYNCHRONIZE As Long = &H100000
    Private Const INFINITE As Long = &HFFFF
    
    Private Declare Function WaitForInputIdle _
                    Lib "user32" _
                    (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CloseHandle _
                    Lib "kernel32" _
                    (ByVal hObject As Long) As Long
    
    Private Declare Function OpenProcess _
                    Lib "kernel32" _
                    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
                    ByVal dwProcessId As Long) As Long
    
    Private Sub Form_Load()
        Dim hProcessID As Long
        Dim hProcess As Long
        Dim sShellPath As String
        Dim Xl As Excel.Application
       hProcessID = Shell("c:\program files\microsoft office\office\excel.exe", vbMinimizedFocus)
       If hProcessID <> 0 Then
          hProcess = OpenProcess(SYNCHRONIZE, 0, hProcessID)
          If hProcess <> 0 Then
             Call WaitForInputIdle(hProcess, INFINITE)
             Call CloseHandle(hProcess)
          End If
       End If
    Set Xl = GetObject(, "excel.application")
    MsgBox Xl.Caption
    End Sub
    I thought that the api calls should hold my program up until the shelled app finished loading. What am I doing wrong?
    I am so skeptical, I can hardly believe it!
    PS I am not a 'hyperactive member' I am a cool, calm, and collected member

  7. #7
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444
    let me ask the following questions.
    shell gives me the PID as a retval
    can I find the handle of the programs window with this PID?
    with the handle of the main window can I capture the main forms activate event?
    I would think that when the activate event is triggered the application is finished loading.
    would this be a logical approach?
    if so, how can I capture the form_activate event if I only know the handle and PID?
    thank you for your time and have a good day
    I am so skeptical, I can hardly believe it!
    PS I am not a 'hyperactive member' I am a cool, calm, and collected member

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