?
Solved

Check to see if batch file is still running

Posted on 2006-06-09
4
Medium Priority
?
267 Views
Last Modified: 2010-04-07
Hi,

I am calling ShellExecute API of shell32.dll in order to execute a batch file with parameters. However, i am trying to halt my program until the batch file finishes running. I have tried several approaches to get the handle of the window in order to loop until the batch file terminates, but it seems like the cmd.exe window is not a normal window and i am unable to do so.

Does  anyone know how to check to see if the window is still open?

Thanks!
0
Comment
Question by:da4thrza
[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
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:rettiseert
ID: 16874306
Try this:


Option Explicit

Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long


Private Sub Form_Load()

    Dim PID As Long

    PID = Shell("c:\x.bat 1")
   
    Do While IsProcessRuning(PID)
        DoEvents
    Loop
   
    MsgBox "Bat ended"
   
End Sub



Public Function IsProcessRuning(PID As Long) As Boolean

   
    Dim hp As Long
    Dim lngExitCode As Long
   
    Const PROCESS_QUERY_INFORMATION As Long = &H400
    Const STILL_ACTIVE = &H103
   
    hp = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, PID)
   
    If GetExitCodeProcess(hp, lngExitCode) Then
        If lngExitCode = STILL_ACTIVE Then
            IsProcessRuning = True
        End If
    End If


End Function
0
 

Author Comment

by:da4thrza
ID: 16874327
Awesome... that worked. Thanks a lot.

The only problem is that the batch file runs in the background. Is there a way to make it the active window in the foreground?
0
 
LVL 13

Accepted Solution

by:
rettiseert earned 500 total points
ID: 16874345
do you need to show the window of the batch file?

then use
shell "c:\bat.bat", vbMaximizedFocus
0
 

Author Comment

by:da4thrza
ID: 16874354
you, my friend, rock! Thanks a lot
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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

718 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