Advanced Shell In Visual Basic


I frequent the various Microsoft-sponsored VB news groups and from time to time, I've seen posts requesting help with the Shell command. Some people want to pause their application until the program that they spawned no longer is running. Presumably, they are writing a helper app, or a training tool, or a data processing gizmo that depends upon the spawned service to create some information.

Other people are spawning DOS applications. Generally, they want this to be transparent to the end user, and they really don't want anyone to know that a DOS application is doing something. Maybe it needs to be hidden so that the user can't interact with the DOS app.

Regardless of the reason, waiting for and hiding spawned applications is something that you may find yourself needing to do. The Shell command isn't especially well suited for the task.

The SuperShell example that I have written addresses all of these needs and then some. Your application will be frozen until the wait time that you have set has elapsed. You can start the spawned application hidden, minimized, maximized, or normally. And finally, you can set the task priority for the program, giving the spawned application more priority (or speed) than it would have otherwise.

If you select HIDDEN as your window style, you will NOT be able to shut the spawned application down. Be careful when using a hidden window style and an INFINITE or long wait time.

I originally wrote the SuperShell command to take Optional auguments. As it turns out, optional auguments seem to have problems when more than one is sent, so I removed this support. Perhaps an update of VB 5 will address this problem.

Option Explicit

Public Const INFINITE = &HFFFF
'STARTINFO constants
Public Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
End Enum

        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
End Type

        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 Byte
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
End Type

        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type
Public Enum enPriority_Class
End Enum
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _
        (ByVal lpApplicationName As String, ByVal lpCommandLine As String, _
        lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As _
        SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal _
        dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory _
        As String, lpStartupInfo As STARTUPINFO, lpProcessInformation _
Private Declare Function WaitForSingleObject Lib "kernel32" _
        (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Function SuperShell(ByVal App As String, ByVal WorkDir As String, _ 
        dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class _
        As enPriority_Class) As Boolean

    Dim pclass As Long
    Dim sinfo As STARTUPINFO
    'Not used, but needed
    sec1.nLength = Len(sec1)
    sec2.nLength = Len(sec2)
    sinfo.cb = Len(sinfo)
    sinfo.dwFlags = STARTF_USESHOWWINDOW
    sinfo.wShowWindow = start_size
    pclass = Priority_Class
    If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, _
        0&, WorkDir, sinfo, pinfo) Then
        WaitForSingleObject pinfo.hProcess, dwMilliseconds
        SuperShell = True
        SuperShell = False
    End If

End Function

Supershell returns TRUE if it successfully spawned the application. If you have a wait time defined, you aren't going to get notification until after the spawned application terminates or the sleep time (dwMilliseconds) elapses. As a result, you may want to set a variable with the current time and check it when you return. Also, you will probably want to shutdown all timers and similar controls that can be sent messages generated outside of your application.

If you want to wait forever, set dwMillisecond to INFINITE (a public constant). Again, I caution you not to do that while spawning a hidden application, not unless you are sure that it will return control back to your program.

Finally, I should mention that you should hide the calling application. As you will soon discover, if you wait for a task to conclude, your program will not even repaint, which gets very ugly on-screen. Looks kinda cool though I doubt that users will appreciate it!

If you are interested into delving a little deeper into the CreateProcess function, you will find that with a little work you can add several other things which I elected not to implement. For instance, you can specify the exact location and size of the spwned application, down to the pixel.

Originally written by Tim Kilgore.

If you enjoyed this post, subscribe for updates (it's free)


Nice code

Shame when i copy it i had to write more code to remove the line numbers.........


I use Excel to remove it ;-)

I use Excel to remove it ;-)

Using SuperShell


The SuperShell seems awesome, but how do I use it? This is what I have done so far:

1- I pasted your code in a seperate module.

2- I inserted the following code where I need to execute my batch file

    Dim bolShell As Boolean
    bolShell = SuperShell(App.Path & "\ftp.bat", App.Path, INFINITE, SW_NORMAL, HIGH_PRIORITY_CLASS)
    txtStatus.Text = bolShell

bolShell returns True. However, to command prompt screen just flashes and the ftp.bat is not executed.

The commands within ftp.bat should take a good 45seconds to execute. Plus, for testing purposes, at the end of it I have a "pause", so the command prompt screen should freeze until I press any key.

Ideally, I need to run my ftp.bat without pause, and do not run any more VB6 commands until ftp.bat completes.

Any help will be greatly awppreciated.

Private Sub

Private Sub ShellAndWait(ByVal program_name As String, _
Optional ByVal window_style As VbAppWinStyle = vbNormalFocus, _
Optional ByVal max_wait_seconds As Long = 0)
Dim lngProcessId As Long
Dim lngProcessHandle As Long
Dim datStartTime As Date
Const WAIT_TIMEOUT = &H102
Const SYNCHRONIZE As Long = &H100000
' Start the program.
On Error GoTo ShellError
lngProcessId = Shell(program_name, window_style)
On Error GoTo 0


' Wait for the program to finish.
' Get the process handle.
lngProcessHandle = OpenProcess(SYNCHRONIZE, 0, lngProcessId)
If lngProcessHandle <> 0 Then
datStartTime = Now
If WaitForSingleObject(lngProcessHandle, 250) <> WAIT_TIMEOUT Then
Exit Do
End If
If max_wait_seconds > 0 Then
If DateDiff("s", datStartTime, Now) > max_wait_seconds Then Exit Do
End If
CloseHandle lngProcessHandle
End If
Exit Sub
End Sub

Thanks !!

The ShellAndWait function works perfectly.
Just you need to add the prototype of the functions used in it.

Thanks a lot to the person who has posted it.........
Thanks a lot vb6.........

Possible fix for FTP.bat

BAT files need a command interperter loaded to execute properly.

Try something like this:

bolShell = SuperShell("cmd.exe /C " & App.Path & "\ftp.bat", App.Path, INFINITE, SW_NORMAL, HIGH_PRIORITY_CLASS)

good luck

I find it so much easier to

I find it so much easier to do this...

Create a batch file to run the process.
In the next line of the batch file, use the echo command to create a .txt file


dir /b /s /a-d /on c:\ >c:\test.ini (Creates an .ini file)
echo File created! >c:\test.txt (Creates test.txt after the .ini file is created)

Run the batch file via shell in your program, then make your program loop until it can find the .txt file.

Try this:

Make this .bat file and run it...

echo hello world >c:\test.txt

When you run it, notepad opens up, and only when you close notepad, does the text file get created.

Very cool! Thanks Sluggs for

Very cool! Thanks Sluggs for the great idea and for contributing back to the community here. Keep it up!