Program Not Terminating

Posted on 2001-06-20
Last Modified: 2010-05-02
I made a VB Program which uses Shell Function to invoke some another program .That another Program is terminated automatically...

If i run this VB Exe on WinNt then the program is running fine.. it invokes that another program.. run it in the background.. close that program.. and then come out from the VB Exe...

But i run this same VB Exe on Win2000 Server the program invokes that another program... and it come out from the VB Exe leaving that program in the Background....

So Pl tell why it is not terminating that another program automatically.. Need to do some settings....or write something special in VB Program

Besides the terminating the task performed by that another program goes fine....
Question by:rajakhanna

Expert Comment

ID: 6210274
i am using the following code on nt to control another program. I did not try on 2000.

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Sub ShellIBI(ByVal lvsShell As String, _
                    ByVal lvsFocusWindows As String)
  Dim lvlProcessID As Long
  Dim lvlProcessHhd As Long
  Dim lvtStarted As Date
  Dim lasWindows() As String
  Dim i As Long
  Dim lvfFound As Boolean
  Dim lvfTimeout As Boolean
  Dim lvlhWnd As Long
  Dim lvlTemp As Long
  Dim lvlTimes As Long
  Dim lvfFirstWindow As Boolean
  lvlProcessID = Shell(lvsShell , vbMinimizedNoFocus)
  'get process handle
  lvlProcessHhd = OpenProcess(SYNCHRONIZE, True, lvlProcessID)
  'wait for terminate process here
  lvlTemp = WaitForSingleObject(lvlProcessHhd, 50)
  Do While lvlTemp <> 0
    lvlTemp = WaitForSingleObject(lvlProcessHhd, 50)
    Sleep 50  
End Sub
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6210297

Expert Comment

ID: 6210540
Use the WSH Run instead of Shell. The Run has a Wait parameter, which will cause the program to halt until the called program terminates.

Include a reference to Windows Script Host Object Model and use code like this:

    Dim oShell As New IWshShell_Class
    oShell.Run sCommand, 1, True

The third parameter in oShell.Run specifies that this code should not continue until sCommand is finished.

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


Accepted Solution

mariab_ earned 180 total points
ID: 6210771

try using ShellExecute I have tried in Win2000 and NT4 - it works just fine!

Private 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
Private Const SW_SHOWMINNOACTIVE = 7 ' just for sample you could use another constant - 0 for example


strProgramName = "d:\games\brood_war\brood.exe" ' :))

ShellExecute 0, "open", strProgamName, "", "", SW_SHOWMINNOACTIVE

LVL 44

Expert Comment

ID: 6213024
Yeah agree with mariab. You are better off using API calls when doing this instead of using the VB funcitions.
LVL 49

Expert Comment

ID: 7139583
Hi rajakhanna,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept mariab_'s comment(s) as an answer.

rajakhanna, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 7173279
Comment from expert accepted as answer

E-E Moderator

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now