Re: How to tell the end of a shell



inquirer wrote:
I have a VB6 program which runs shell which runs another program which in turn creates a file. As soon as I call the shell, I run a subroutine which essentially pauses the main program for a few seconds whilst the new file is created. I then use VB to open Word and read the newly created file.

This works fine when running from my hard disk but when I try to run it from a USB memory stick, the shell program takes several minutes to create the file.

I need to be able to pause the main program until the shell program has finished so I can then open the new file in Word.

Is there any way I can find out when the shell program has finished creating the new file?

Thanks
Chris

Do a search on ShellAndWait, or use something like the following...


Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

' Shell & Wait...
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
'Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const STATUS_PENDING = &H103&
Private Const PROCESS_QUERY_INFORMATION = &H400


Public Function ShellAndWait( _
sShell As String, _
Optional ByVal eWindowStyle As VBA.VbAppWinStyle = vbNormalFocus, _
Optional ByRef sError As String, _
Optional ByVal lTimeOut As Long = 2000000000) As Boolean
'On Error GoTo errHandler

Dim ProcessID As Long
Dim hProcess As Long
Dim lRet As Long
'Dim lTimeStart As Long
Dim bSuccess As Boolean
Dim i As Long

bSuccess = False

ProcessID = Shell(sShell, eWindowStyle)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)

If (hProcess = 0) Then
sError = "This program could not determine whether the process started. Please watch the program and check it completes."
' Only fail if there is an error - this can happen when the program completes too quickly.
Else
bSuccess = True
'lTimeStart = timeGetTime()

Do
i = i + 1

' Get the status of the process
GetExitCodeProcess hProcess, lRet

' Sleep during wait to ensure the other process gets processor slice:
DoEvents

Sleep 1000

'If (timeGetTime() - lTimeStart > lTimeOut) Then
' ' Too long!
' sError = "The process has timed out."
' lRet = 0
' bSuccess = False
'End If

If i > 5 Then
lRet = 0
bSuccess = False
Debug.Assert False
End If
Loop While lRet = STATUS_PENDING

End If

endHandler:
ShellAndWait = bSuccess

Exit Function
errHandler:
'ErrorIn "modActivate.ShellAndWait(sShell,eWindowStyle,sError,lTimeOut)", Array(sShell, _
' eWindowStyle, sError, lTimeOut)
End Function


Usage:

If ShellAndWait(sExeFile, vbNormalFocus) Then
'Process has completed, so...
Else
'Process failed.
End If


Hope this helps.
.



Relevant Pages

  • How to getting an owner of file
    ... Private Declare Function GetSecurityDescriptorOwner Lib ... ByVal lpSystemName As String, _ ... bSuccess = GetFileSecurity(szfilename, ... Dim fldrPathName, fname, fowner As String ...
    (microsoft.public.vb.winapi)
  • Re: Reading IE browser contents?
    ... The Document object represents the loaded page. ... "Microsoft Shell Controls and Automation" ... Dim ie2 As InternetExplorer ... windows AND IE windows. ...
    (microsoft.public.vb.general.discussion)
  • Re: task list
    ... Shell returns a process id you can use to identify the running process. ... Private Declare Function GetExitCodeProcess Lib "kernel32" (_ ... Public Function Process_Still_ActiveAs Boolean ... Dim lExitCode As Long ...
    (microsoft.public.vb.winapi)
  • Re: Running Excel
    ... I've gotten away from the SHELL command and am stuck with what appears to be ... Private Sub Command32_Click ... Dim xlWorkbook As Excel.Workbook ... > When you use Shell, you lose all subsequent control of the shelled program. ...
    (microsoft.public.access.modulesdaovba)
  • RE: how to get vba to wait until a shell command has completed
    ... Private Declare Function GetExitCodeProcess Lib "kernel32" (_ ... Catff.bat is called from Word using the shell command, ... ' Run sample batch file in MS-DOS window. ...
    (microsoft.public.word.vba.general)