|
-
Jan 18th, 2007, 02:32 PM
#1
Thread Starter
Fanatic Member
Closing apps opened with Shell
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.
-
Jan 18th, 2007, 03:02 PM
#2
Re: Closing apps opened with Shell
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?
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jan 18th, 2007, 03:32 PM
#3
Re: Closing apps opened with Shell
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.
-
Jan 18th, 2007, 03:53 PM
#4
Re: Closing apps opened with Shell
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jan 18th, 2007, 03:56 PM
#5
Thread Starter
Fanatic Member
Re: Closing apps opened with Shell
Thanks very much for that. Worked first time!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|