dustybryn
asked on
How to stop an application running from another application
I've got a VB app which uses a shell command to start another vb app. I would also like to add the ability to then close this second app down. Can anyone help?
Many thanks,
Bryn.
Many thanks,
Bryn.
If it is not a DOS windows, you could use FindWindow Api to locate the main window of other program and PostMessage API with parameter got from findwindow and WM_CLOSE constant:
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try using this
Declare:
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, ByVal lpCommandLine As _
String, ByVal lpProcessAttributes As Long, ByVal _
lpThreadAttributes As Long, ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, lpStartupInfo As _
STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) _
As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
'Code in form in this case it starts Calc.exe then closes
'it when you click on the command button
Private Sub Command1_Click()
TerminateProcess proc.hProcess, 0
End Sub
Private Sub Form_Load()
'Initialize the STARTUPINFO structure:
start.cb = Len(start)
'Start the shelled application:
ret& = CreateProcessA(0&, "calc.exe", 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
End Sub
Declare:
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, ByVal lpCommandLine As _
String, ByVal lpProcessAttributes As Long, ByVal _
lpThreadAttributes As Long, ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, lpStartupInfo As _
STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) _
As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
'Code in form in this case it starts Calc.exe then closes
'it when you click on the command button
Private Sub Command1_Click()
TerminateProcess proc.hProcess, 0
End Sub
Private Sub Form_Load()
'Initialize the STARTUPINFO structure:
start.cb = Len(start)
'Start the shelled application:
ret& = CreateProcessA(0&, "calc.exe", 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
End Sub
Hes,
don't use terminateprocess API whitout calling CloseHandle first!
I thint it's like clossing app with a bomb.
don't use terminateprocess API whitout calling CloseHandle first!
I thint it's like clossing app with a bomb.
Richie,
LOL I had the declare for CloseHandle but didn't cut it and paste the call :)
Private Sub Command1_Click()
TerminateProcess proc.hProcess, 0
End Sub
should be
Private Sub Command1_Click()
ret& = CloseHandle(proc.hProcess)
TerminateProcess proc.hProcess, 0
End Sub
LOL I had the declare for CloseHandle but didn't cut it and paste the call :)
Private Sub Command1_Click()
TerminateProcess proc.hProcess, 0
End Sub
should be
Private Sub Command1_Click()
ret& = CloseHandle(proc.hProcess)
TerminateProcess proc.hProcess, 0
End Sub
ASKER
Richie,
I've tried using your code but when it gets to
'PostMessage ret, WM_CLOSE' it stops with the message "Argument not optional" is this just a missing parameter and if so do you know how i can get it going?
I've tried using your code but when it gets to
'PostMessage ret, WM_CLOSE' it stops with the message "Argument not optional" is this just a missing parameter and if so do you know how i can get it going?
ASKER
Richie,
The postmessage call just needed two extra parameters of 0,0 to run.
Cheers,
Dust.
The postmessage call just needed two extra parameters of 0,0 to run.
Cheers,
Dust.
oops!
i wrote it from memory and did not read my own declaration of PostMessage function. Sorry.
Thanks for "A" grade.
i wrote it from memory and did not read my own declaration of PostMessage function. Sorry.
Thanks for "A" grade.