Results 1 to 2 of 2

Thread: Loop until end

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    Leeds, UK
    Posts
    75

    Loop until end

    I have this piece of code.

    ShellExecute Me.hwnd, vbNullString, "c:\mybat.bat", _
    vbNullString, "C:\", SW_SHOWNORMAL

    I want this to loop until it has ended.

    Any ideas ?

  2. #2
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378
    hmmm...I think you need to shell the application and wait till it has finished. I've used some code before to do that and it works pretty fine without any noticeably memory leaks.

    Try this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type STARTUPINFO
    4.   cb As Long
    5.   lpReserved As String
    6.   lpDesktop As String
    7.   lpTitle As String
    8.   dwX As Long
    9.   dwY As Long
    10.   dwXSize As Long
    11.   dwYSize As Long
    12.   dwXCountChars As Long
    13.   dwYCountChars As Long
    14.   dwFillAttribute As Long
    15.   dwFlags As Long
    16.   wShowWindow As Integer
    17.   cbReserved2 As Integer
    18.   lpReserved2 As Long
    19.   hStdInput As Long
    20.   hStdOutput As Long
    21.   hStdError As Long
    22. End Type
    23.  
    24. Private Type PROCESS_INFORMATION
    25.   hProcess     As Long
    26.   hThread      As Long
    27.   dwProcessId  As Long
    28.   dwThreadID   As Long
    29. End Type
    30.  
    31. Private Type SECURITY_ATTRIBUTES
    32.   nLength               As Long
    33.   lpSecurityDescriptor  As Long
    34.   bInheritHandle        As Long
    35. End Type
    36.  
    37. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
    38.                            hHandle As Long, ByVal dwMilliseconds As Long) As Long
    39.  
    40. Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _
    41.                           (ByVal lpApplicationName As String, _
    42.                            ByVal lpCommandLine As String, _
    43.                            lpProcessAttributes As SECURITY_ATTRIBUTES, _
    44.                            lpThreadAttributes As SECURITY_ATTRIBUTES, _
    45.                            ByVal bInheritHandles As Long, _
    46.                            ByVal dwCreationFlags As Long, _
    47.                            lpEnvironment As Any, _
    48.                            ByVal lpCurrentDirectory As String, _
    49.                            lpStartupInfo As STARTUPINFO, _
    50.                            lpProcessInformation As PROCESS_INFORMATION) As Long
    51.  
    52. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
    53.                             ByVal uExitCode As Long) As Long
    54.  
    55. Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, _
    56.                           phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, _
    57.                           ByVal nSize As Long) As Long
    58.  
    59. Private Declare Function CloseHandle Lib "kernel32" _
    60.                           (ByVal hObject As Long) As Long
    61.  
    62. Private Declare Function GetExitCodeProcess Lib "kernel32" _
    63.                           (ByVal hProcess As Long, lpExitCode As Long) As Long
    64.  
    65. Private Const STARTF_USESTDHANDLES As Long = &H100&
    66. Private Const STARTF_USESHOWWINDOW As Long = &H1&
    67.  
    68. Private Const SW_HIDE As Long = 0&
    69. Private Const SW_SHOWNORMAL = 1
    70. Private Const SW_SHOWMINIMIZED = 2
    71. Private Const SW_SHOWMAXIMIZED = 3
    72. Private Const SW_SHOWNOACTIVATE = 4
    73. Private Const SW_SHOW = 5
    74. Private Const SW_MINIMIZE = 6
    75. Private Const SW_SHOWMINNOACTIVE = 7
    76. Private Const SW_SHOWNA = 8
    77. Private Const SW_RESTORE = 9
    78.  
    79. Private Const WAIT_ABANDONED = &H80
    80. Private Const WAIT_FAILED = &HFFFFFFFF
    81. Private Const WAIT_OBJECT_O = &H0
    82. Private Const WAIT_TIMEOUT = &H102
    83.  
    84. Private Const NORMAL_PRIORITY_CLASS = &H20
    85. Private Const INFINITE = -1&
    86.  
    87. Private Sub RunCommand(ByVal lcStrCommandLine As String)
    88.  
    89.   Dim ProcInfo            As PROCESS_INFORMATION
    90.   Dim startinfo           As STARTUPINFO
    91.   Dim lcLngRetVal         As Long
    92.   Dim retval              As Long
    93.   Dim SA                  As SECURITY_ATTRIBUTES
    94.   Dim hRead               As Long 'the handle to the read end of the pipe
    95.   Dim hWrite              As Long 'the handle to the write end of the pipe
    96.   Dim lcStrErrPrefix      As String
    97.   Dim lcStrErrMsg         As String
    98.    
    99.   On Error GoTo ErrHandler
    100.    
    101.   'set up security attributes structure
    102.   With SA
    103.     .nLength = Len(SA)
    104.     .bInheritHandle = 1& 'inherit, needed for this to work
    105.     .lpSecurityDescriptor = 0&
    106.   End With
    107.  
    108.   'create our anonymous pipe an check for success
    109.   retval = CreatePipe(hRead, hWrite, SA, 0&)
    110.   If retval = 0 Then
    111.     Debug.Print "CreatePipe Failed"
    112.     Exit Sub
    113.   End If
    114.  
    115.   ' Initialize the STARTUPINFO structure:
    116.   With startinfo
    117.     .cb = Len(startinfo)
    118.     .dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
    119.     .wShowWindow = SW_HIDE
    120.     .hStdOutput = hWrite
    121.     .wShowWindow = SW_HIDE
    122.   End With
    123.  
    124.   ' Start the shelled application:
    125.   lcLngRetVal = CreateProcess(vbNullString, Chr(34) & lcStrCommandLine & Chr(34), SA, SA, 1&, _
    126.                 NORMAL_PRIORITY_CLASS, 0&, vbNullString, startinfo, ProcInfo)
    127.  
    128.   If lcLngRetVal = 0 Then
    129.     lcStrErrMsg = "Could not execute the DOS command: " & lcStrCommandLine
    130.     Debug.Print lcStrErrMsg
    131.     Exit Sub
    132.   End If
    133.  
    134.   ' Wait for the shelled application to finish:
    135.   If WaitForSingleObject(ProcInfo.hProcess, INFINITE) <> WAIT_OBJECT_O Then
    136.     TerminateProcess ProcInfo.hProcess, 0
    137.     TerminateProcess ProcInfo.hThread, 0
    138.   End If
    139.  
    140.   Call CloseHandle(ProcInfo.hProcess)
    141.   Call CloseHandle(ProcInfo.hThread)
    142.   Debug.Print "Process Terminated"
    143.  
    144.   Exit Sub
    145.  
    146. ErrHandler:
    147.   Debug.Print Err.Description
    148. End Sub

    usage:

    VB Code:
    1. call RunCommand "c:\mybat.bat"
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

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