?
Solved

Runing external apllication using Shell

Posted on 2003-02-24
7
Medium Priority
?
761 Views
Last Modified: 2010-04-07
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

RONALD - BRASIL

0
Comment
Question by:rdettmann
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Expert Comment

by:Sethi
ID: 8009117
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
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8009284
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 Const PROCESS_QUERY_INFORMATION = 1024
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)
    Do
        '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
        Else
            'let's get outta here
            Exit Sub
        End If
        DoEvents
    Loop
End Sub
0
 
LVL 1

Expert Comment

by:dwphilippe
ID: 8009344
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
  doevents
loop
...

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:rdettmann
ID: 8009809
dwphilippe

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
0
 
LVL 2

Author Comment

by:rdettmann
ID: 8009833
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?
0
 
LVL 1

Accepted Solution

by:
SilentRage earned 400 total points
ID: 8010703
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.
0
 
LVL 2

Author Comment

by:rdettmann
ID: 8010988
I'll try .. thank you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question