Solved

Opened an external app using the shell command how do I close it?

Posted on 2004-03-28
4
1,130 Views
Last Modified: 2010-05-01
Can I close another external app from VB. I use the shell command to open it.
0
Comment
Question by:Sparky191
  • 2
4 Comments
 
LVL 4

Assisted Solution

by:xassets
xassets earned 50 total points
ID: 10698377
Use the FindWindow API function to get its hWND

Then send it a WM_CLOSE message using the SendMessage or PostMessage API Function
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 50 total points
ID: 10698396
0
 
LVL 4

Expert Comment

by:xassets
ID: 10698402
Yeah thats a bit more up to date.
0
 
LVL 6

Accepted Solution

by:
___XXX_X_XXX___ earned 150 total points
ID: 10698515
Shell returns to you process ID for created process. You can use this PID later in TerminateProcess to terminate program.

Like this:


In declarations section of your form:

Dim mlngPID As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long


 ' Add two command buttons - Command1 for start Calculator and Command2 for Terminating it.
Private Sub Command1_Click()
mlngPID = Shell("calc.exe")
End Sub


Private Sub Command2_Click()
Dim lngProcess As Long
lngProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, mlngPID)
TerminateProcess lngProcess, 0&
End Sub



Start project, press Command1 - calculator must show, then press command2 - calculator will be terminated.

Enjoy.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

803 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