Discussion Locked

Execute a file synchronously from VB?

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I want to execute an old dos program from my VB program. I know this can be done with Shell() but I want to execute the program synchronously so I can use the results of this program afterwards. Is there any way to do this? Or is there some way i can make my program wait for the other executable to finish?

5 replies

by msi

Thanks. Didn't know this site by the way. Looks usefull for in the future

by peyison

The code below will do this:

Public Sub ShellAndWait(asCommand As String, asCommandLine As String)
Dim lPid As Long
Dim lHnd As Long
Dim lRet As Long

lPid = Shell(asCommand & " " & asCommandLine)
If lPid <> 0 Then
lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
If lHnd <> 0 Then
lRet = WaitForSingleObject(lHnd, INFINITE)
CloseHandle (lHnd)
End If
End If

End Sub
---------------------
Put the following in the declarations section of a module:

Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFF

-------------------------

To use it, just pass the name of the program you want to execute:

ShellAndWait "c:\mydosapp.exe"

Hope this helps.

This discussion is closed to new replies.