Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1145
  • Last Modified:

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

Can I close another external app from VB. I use the shell command to open it.
0
Sparky191
Asked:
Sparky191
  • 2
3 Solutions
 
xassetsCommented:
Use the FindWindow API function to get its hWND

Then send it a WM_CLOSE message using the SendMessage or PostMessage API Function
0
 
xassetsCommented:
Yeah thats a bit more up to date.
0
 
___XXX_X_XXX___Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now