Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Shell asynchronously/ synchronously

I am using the shell command to run a program from my app BUT i want my app to wait untill the program has ended befor i carry on.

in VB 4 there was a way of setting the shells asynchronously/ synchronously state but in VB6 this has been removed!

so how do i make it wait!?!?!

any help would be cool thanks.

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : I am using the shell command to run a program from my app BUT i want my app to wait untill the program has ended befor i carry on.
    :
    : in VB 4 there was a way of setting the shells asynchronously/ synchronously state but in VB6 this has been removed!
    :
    : so how do i make it wait!?!?!
    :
    : any help would be cool thanks.
    :

    This requires API's. The CreateProcess and WaitForSingle object API's are used.
    I have an example (not home-made):
    [code]
    'This program needs a common dialog box, named CDBox
    ' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
    ' and select Microsoft Common Dialog control)
    Const INFINITE = &HFFFF
    Const STARTF_USESHOWWINDOW = &H1
    Private Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
    End Enum
    Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
    End Type
    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 Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
    End Type
    Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
    End Type
    Private Enum enPriority_Class
    NORMAL_PRIORITY_CLASS = &H20
    IDLE_PRIORITY_CLASS = &H40
    HIGH_PRIORITY_CLASS = &H80
    End Enum
    Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Private Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
    Dim pclass As Long
    Dim sinfo As STARTUPINFO
    Dim pinfo As PROCESS_INFORMATION
    'Not used, but needed
    Dim sec1 As SECURITY_ATTRIBUTES
    Dim sec2 As SECURITY_ATTRIBUTES
    'Set the structure size
    sec1.nLength = Len(sec1)
    sec2.nLength = Len(sec2)
    sinfo.cb = Len(sinfo)
    'Set the flags
    sinfo.dwFlags = STARTF_USESHOWWINDOW
    'Set the window's startup position
    sinfo.wShowWindow = start_size
    'Set the priority class
    pclass = Priority_Class
    'Start the program
    If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, _
    0&, WorkDir, sinfo, pinfo) Then
    'Wait
    WaitForSingleObject pinfo.hProcess, dwMilliseconds
    SuperShell = True
    Else
    SuperShell = False
    End If
    End Function
    Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    'Set the dialog's title
    CDBox.DialogTitle = "Choose an EXEC-File ..."
    'Error when canceled
    CDBox.CancelError = True
    'Set the dialog's filter
    CDBox.Filter = "EXEC-Files (*.exe)|*.exe|All files (*.*)|*.*"
    'Show the 'Open File'-dialog
    CDBox.ShowOpen
    'Execute the program
    SuperShell CDBox.filename, Left$(CDBox.filename, Len(CDBox.filename) - Len(CDBox.FileTitle)), 0, SW_NORMAL, HIGH_PRIORITY_CLASS
    End
    End Sub
    [/code]
    Sorry for the added width, but I am too lazy to format the code... ;-)

    Greets...
    Richard

  • varaxorvaraxor Posts: 1Member
    All the samples of code, I've seen so far use Windows API.

    However, if you want to use code that don't call API as me you can do it with simple VB code :
    [code]
    Sub ShellAndWait(PathName As String, _
    Optional WindowsStyle As VbAppWinStyle = vbMinimizedFocus)
    Dim ShellCmdPath As String
    Dim FileNumber As Long
    Dim template As String
    Dim i As Integer

    ' Generate a temporary filename
    i = 0
    Do
    ShellCmdPath = Hex(i) & ".bat"
    i = i + 1
    Loop While Len(Dir(ShellCmdPath)) > 0

    ' Prepare batch file content
    template = "@echo off" & vbCrLf

    ' Action to execute
    template = template & PathName & vbCrLf

    ' Remove created batch file when action has been executed
    template = template & "del " & ShellCmdPath

    ' Create batch file
    FileNumber = FreeFile
    Open ShellCmdPath For Output As #FileNumber
    Print #FileNumber, template
    Close #FileNumber

    ' Execute batch file containing action
    Shell ShellCmdPath, WindowsStyle

    ' Wait for asynchronous command to finish
    While Len(Dir(ShellCmdPath)) > 0
    DoEvents
    Wend
    End Sub
    [/code]

Sign In or Register to comment.