If you open another application by using Shell (or ShellExecute) - I want to make sure that, when I close my VB app, the application I opened closes as well.
How can I do this please?
Thanks for any help.
Printable View
If you open another application by using Shell (or ShellExecute) - I want to make sure that, when I close my VB app, the application I opened closes as well.
How can I do this please?
Thanks for any help.
I thought maybe the Long returned from either could be used to the close it by using PostMessage API. But its not the same long as FindWindow returns so it doesnt work.
What app are you starting?
Try this:
VB Code:
Option Explicit 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 ' Private Const PROCESS_TERMINATE As Long = (&H1) ' Dim hProcess As Long '------------------------------------------------------------------ Sub Shell2(strCommand As String, WindowStyle As VbAppWinStyle) Dim lProcessId As Long 'Shell and get the PID lProcessId = Shell(strCommand, WindowStyle) ' ' Get the process handle hProcess = OpenProcess(PROCESS_TERMINATE, False, lProcessId) End Sub '------------------------------------------------------------------ Private Sub Command1_Click() Shell2 "D:\Windows\system32\cmd.exe", vbNormalFocus If hProcess <> 0 Then Command1.Enabled = False End Sub '------------------------------------------------------------------ Private Sub Form_Unload(Cancel As Integer) 'Close the opened process If hProcess <> 0 Then TerminateProcess hProcess, 1 End If End Sub
found the original code here. Modified it for your need.
Nice!
im saving that one
Thanks very much for that. Worked first time!