Solved

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

Posted on 2004-03-28
4
1,128 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 47
How to create a duplicate finder Application 9 92
Windows 10 start screen issues 9 47
using web browser with BING 40 86
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

708 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