-
March 30th, 2000, 10:22 AM
#1
Waiting for exe to finish using 'shell( , )'
hi all,
i have a need to execute an external console application, redirect its output to file, and then readback and process this file.
I'm doing this using VB's 'Shell' command, but i have the following problems:
1. the app is run asynchronously, i.e. i dont know when this app finishes so i can get the file.
2. during the period the app is running, it is (minimized) visible in the taskbar, how to avoid this?
Any help is appreciated.
thanx to all.
-
March 30th, 2000, 10:43 AM
#2
Re: Waiting for exe to finish using 'shell( , )'
you can use the following code and call the ShellWait function like this
shellwait("notepad.exe", SW_HIDE) to hide the window (not a good idea with notepad.
Code:
option Explicit
private Declare Function CloseHandle Lib "kernel32" _
(byval hObject as Long) as Long
private Declare Function WaitForSingleObject Lib "kernel32" _
(byval hHandle as Long, byval dwMilliseconds 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 Const SW_HIDE = 0
private Const SW_SHOW = 5
private Const STANDARD_RIGHTS_REQUIRED = &HF0000
private Const SYNCHRONIZE = &H100000
Const PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or _
SYNCHRONIZE Or &HFFF
Const WAIT_TIMEOUT = &H102
public Sub ShellWait(byval FileName as string, optional WindowStyle as Variant)
Dim idProc as Long
Dim hProc as Long
idProc = VBA.Shell(FileName, WindowStyle)
hProc = OpenProcess(PROCESS_ALL_ACCESS, false, idProc)
If hProc <> 0 then
Do While WaitForSingleObject(hProc, 100) = WAIT_TIMEOUT
DoEvents
Loop
CloseHandle hProc
End If
End Sub
private Sub Command1_Click()
Call ShellWait("notepad.exe", SW_SHOW)
End Sub
Last edited by Cimperiali; July 22nd, 2004 at 05:13 PM.
Reason: Adding Code tag
-
March 30th, 2000, 11:05 AM
#3
[thank you]
very, very nice indeed.
thank you for this extraordinary piece of coding.
-
October 17th, 2002, 03:20 AM
#4
urgent
wh y i got an error in
If hProc <> 0 then
can anyone help me....thanx
-
October 17th, 2002, 03:54 PM
#5
Don't know if this is a translation error but
'
If hProc <> 0 then
'
should read
Code:
If hProc <> 0 then
if hProc (less than sign)(Greater than sign) 0 then
-
October 18th, 2002, 08:58 AM
#6
Execute & Wait
Well, here another solution for the Execution of an exe that waits until the execution is finished:
Code:
'----------------------------------
Call ExecuteAndWait ("c:\test.exe")
'----------------------------------
Private Sub ExecuteAndWait(cmdline As String)
Dim NameOfProc As PROCESS_INFORMATION
Dim NameStart As STARTUPINFO
Dim X As Long
NameStart.cb = Len(NameStart)
X = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
X = CloseHandle(NameOfProc.hProcess)
End Sub
Also, place the following code to a module:
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
Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Global Const NORMAL_PRIORITY_CLASS = &H20&
Global Const INFINITE = -1&
Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Declare Function CreateProcessA Lib "kernel32" (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
That's all.
Vangelis
Last edited by Cimperiali; July 22nd, 2004 at 05:15 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|