Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i'm running a shell program through VB GUI.
the problem is that i would like GUI to "hang" till shell program fifnishes, but i dont know how to do it.....THANKS

You need the CreateProcess API followed by the WaitForSingleObject API
The CreateProcess runs your application then the WaitForSingleObject monitors the thread and does not return till the thread is closed effectivly bringing the VB app to a standstill.
The following will give you an idea of how it works. You will need to read the API help files to find out what all the different paramters do, particulary the PROCESS_INFORMATION and STARTUPINFO structures.
Public Sub ExecCmd(CmdLine As String)
Dim Proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
Dim Ret As Boolean
' Initialize the STARTUPINFO structure:
Start.cb = Len(Start)' Start the shelled application:
If WinType <> 0 Then
Start.dwFlags = WinType
Start.wShowWindow = WinHide
End If
Ret = CreateProcessA(0&, CmdLine, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, Start, Proc)
If WinType <> 0 Then
Ret = WaitForSingleObject(Proc.hProcess, INFINITE)
Ret = CloseHandle(Proc.hProcess)
Ret = CloseHandle(Proc.hThread)
End If
End SubStuart

'RECOMMENDED METHOD FOR SYNCHRONOUS SHELLING
'http://www.blaiseusers.org/ibucpdfs/2001/Cashwell--IBUC_Paper.pdf'IN ORDER TO USE THE IWSHSHELL OBJECT, YOU NEED
' TO HAVE A REFERENCE IN VISUAL BASIC TO:
' WSHOM.OCX
'In Menu:
' Select: Project -> References...
' Check "Windows Script Host Object Model"'WshShell.Run (strCommand, [intWindowStyle], [blnWaitOnReturn])
' Parameters:
' strCommand
' command to execute
' intWindowStyle
' This is the value to which the wShowWindow element is set in
' the STARTUPINFO structure for the new process. Values are:
' Name Value Meaning
' SW_HIDE 0 Hides the window and activates another window.
' SW_SHOWNORMAL 1 Activates and displays a window.
' SW_SHOWMINIMIZED 2 Activates as a minimized window.
' SW_SHOWMAXIMIZED 3 Activates as a maximized window.
' SW_SHOWNOACTIVATE 4 Displays a window in its most recent size
' and position.
' SW_SHOW 5 Activates the window and displays it in its
' current size and position.
' SW_MINIMIZE 6 Minimizes the specified window and activates
' the next top-level window in the Z order.
' SW_SHOWMINNOACTIVE 7 Displays the window as a minimized window.
' SW_SHOWNA 8 Displays the window in its current state.
' SW_RESTORE 9 Activates and displays the window.
' blnWaitOnReturn
' False = Imedidate return 0 (default)
' True = Wait for application and return error codePrivate Sub Command1_Click()
'For storing the manipula/dep command string
Dim strCommand As String
'For controlled shelling to manipula/dep
Dim WshShell As IWshShell
'Create the wshshell object
Set WshShell = CreateObject("Wscript.Shell")
'Create the variable to receive the Return value from wshshell
Dim lngAppReturnValue As Long
'Run the manipula/dep command string, telling it to use a maximized
'window (3) and set the boolean parameter to 'True' so wshshell will
'wait until the process is finished before proceeding to the next
'line of code.
strCommand = "Notepad test.txt"
lngAppReturnValue = WshShell.Run(strCommand, 3, True)
End Sub

Read the through the document mentioned in the previous post. Seems this method was devised to overcome some shortomings in the Blaise programme that it referes to. The reccomended way is only when shelling to that particular programme.
It also uses Windows Scripting Host - something that may not be avaialable on every computer as it is often deleted as it is a security risk.
WSH is slow, very slow. There is nothing in WSH that connot be achieved with native functions in VB and Windows that are considerably faster.
Stick with the API. It does take a bit more effort, but is faster, reliable and guranteed to work on every Windows computer.
Stuart

I'm trying to do something simple with a shell - just calling a batch file, and when it finishes, return to the vb code.
I think i need a code like this one, but i don't know how to use it, because i'm new to programming.
I would appreciate if you could help me. At the moment my code says something like this:
Dim retvaL
retvaL = Shell("c:\123.bat", 0)
MsgBox "Done!"
But i dont know what i have to change this to to use your function.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |