Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-03-28
4
Medium Priority
?
1,142 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 200 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 200 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 600 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

Hire Technology Freelancers with Gigs

Work with 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.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses

783 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