Results 1 to 6 of 6

Thread: Another Shell question.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Question

    Anyone know how to ie. start Calc.exe and wait for Calc.exe to close before more code is processed?
    To halt the code while it's "sub"-window is running?

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Sure, add the following code to a code module:
    Code:
    Private 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
    
    Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessID As Long
        dwThreadID As Long
    End Type
    
    Private Declare Function WaitForSingleObject _
     Lib "kernel32" ( _
     ByVal hHandle As Long, _
     ByVal dwMilliseconds As Long) As Long
    
    Private 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
    
    Private Declare Function CloseHandle _
     Lib "kernel32" ( _
     ByVal hObject As Long) As Long
    
    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const INFINITE = -1&
    Private 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
        AppActivate Me.Caption
    End Sub
    Now just call ShellAndWait with the program you want to start as the first argument and optional a windowstyle constant as the second.
    ShellAndWait "Calc.exe" or ShellAndWait "Notepad.exe", vbMaximizedFocus

    Good luck!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Smile

    thx :-)

  4. #4
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83

    Shorter, but wrong ????

    I have this definition

    Declare Function OpenProcess Lib "kernel32.dll" (ByVal _
    dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
    As Long) As Long

    and uses this code

    Code:
    dblProgramTaskId = Shell(strProgramFileName, vbNormalFocus)
    DoEvents
    If dblProgramTaskId = 0 Then
      WriteToErrFile "Faield to run " & strProgramFileName
    Else
      Do Until OpenProcess(SYNCHRONIZE, True, dblProgramTaskId) = 0
        DoEvents
      Loop
    End If
    I works for me but does it have a flaw ?
    Yesterday, all my troubles seemed so far away...
    Help, I need somebody, Help...
    Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Nope no flaws as far as I know. But the SYNCHRONIZE value is only (as far as I know) supported on WinNT and Win2000.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    I think I will try the 2nd solution there. The 1st causes lockup and the reason is pasted here: ( i am using DDE )

    Use caution when calling the wait functions and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. A thread that uses a wait function with no time-out interval may cause the system to become deadlocked. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than WaitForSingleObject.

    Should try to make a version of the function useing WaitForMultipleObjects. That way my DDE proggie dont hang.

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