• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 765
  • Last Modified:

Runing external apllication using Shell

Hey Guys, I'm trying run a externall apllication with Shell command.

How can I control whe this application is terminated?

I'm creating a program to run some ms-dos commands, but putins shells this aplications running at the same time.

I wanna run one apllication after other. How I can do it?

I tried use a While in ther Var (sintaxe: var=shell command) but this variable is used when the application beggin, bnut not whe the external application end.

Thank you


1 Solution
There are two of ways by which you can do this.

First Method
Use the Shell Function In VB. Here is an example:
Shell "C:\WINNT\system32\notepad.exe " & strFileName, vbMaximizedFocus
Specify the textfile name with full path in strFileName.

Second Method
Use ShellExecute API.
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
ShellExecute Me.hwnd, "Open", filePath, 0&, 0&, SW_SHOWNORMAL
He wasn't asking how to execute a process.  He was asking how to wait until the process terminated before proceeding.  For some reason, there's documentation saying that the Shell function has a "Wait" argument where you can specify to wait until the process closed.  But in my VB6 I don't have such an option.  So here's another option:

Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Sub Form_Load()
    ShellWait "C:\winnt\notepad.exe"
    ShellWait "C:\winnt\notepad.exe"
End Sub
Private Sub ShellWait(Path As String)
Dim hProcess As Long, hOpenProcess As Long

    hProcess = Shell("C:\winnt\notepad.exe", vbNormalFocus)
        'Open a handle to the process requesting query permissions
        hOpenProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, hProcess)
        'If we were successful
        If hOpenProcess <> 0 Then
            'close the handle
            CloseHandle hOpenProcess
            'let's get outta here
            Exit Sub
        End If
End Sub
Hi Ronald,

What you can do is before you run your shell command, create a fill. At the end of your batch file you delete the file (delete execute.tmp). In your VB program you continu when the file is delete.

StartProgram "c:\copy.bat"
Do Until EndProgram

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

rdettmannAuthor Commented:

I can't use this because the command line is variable, and i don't wanna crate a personalized bath file all time to run script

SilentRage, I'll try it ..

Thank you
rdettmannAuthor Commented:
When I use a shell (var=shell command) the var receive a process id? Can I check this ID to know if is running?

SilentRage,  is that in your example?
yes... and no.  var=Shell.  var IS the process ID.  It's a number if you successfully executed the program.  It's 0 if you failed.  The way that I detected if the program is still running is by trying to "Open" the process again, using that ID that you got from shell.  If I fail to open the process, then I assume it's shut down, and proceed with life.

The code works, if you just add it to a new project.  You can see it in action, and know that it works.
rdettmannAuthor Commented:
I'll try .. thank you

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now