How do I close a running application via VB 6?
Thank you,
Sir Loin
Printable View
How do I close a running application via VB 6?
Thank you,
Sir Loin
The SendMessage API should do it using the WM_CLOSE constant. I believe your also gonna need the FindWindow API to find the hWnd of the app to close.
That would just close the window, I want to unload the entire program itself.
I found this code on AllAPI.net
VB Code:
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long) Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long Private Declare Function GetCurrentProcess Lib "kernel32" () As Long Private Sub Form_Load() 'KPD-Team 1999 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] 'end this process ExitProcess GetExitCodeProcess(GetCurrentProcess, 0) End Sub
What process would that kill though?
Thank you,
Sir Loin
Try thisVB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ ByVal lParam As Long) As Long Private Const WM_CLOSE = &H10 Private Sub cmdCloseApp_Click() Dim lngCloseIt As Long lngCloseIt = FindWindow(vbNullString, "Caption Of Window To Be Closed") PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0) End Sub
SendMessage can do it as well, right?
That would close a window, I want to kill the entire process.
For Example, when you open the Windows Task Manager, and go to the "Processes" tab, I want to kill a process via VB 6.
Thank You,
Sir Loin
PostMessage will close it immediately.Quote:
Originally Posted by Jacob Roman
What Process does GetCurrentProcess get?
Oh, thanks.
So how would I be able to end FireFox.exe?
Thank You,
Sir Loin
I think the processname is firefox.exe, so
will work on XPVB Code:
Shell "tskill firefox.exe"
You need the processID to use tskill, how do I get the process ID?
Will tskill only work on WinXP?
Thank You,
Sir Loin
I got the classname, and was going to try tskill, but found that this app closed it, or at least prompted me that I was about to close 2 windows.
VB Code:
Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Const SW_SHOWNORMAL = 1 Const WM_CLOSE = &H10 Const WM_ACTIVATE = &H6 Const gcClassnameMSWord = "OpusApp" Const gcClassnameMSExcel = "XLMAIN" Const gcClassnameMSIExplorer = "IEFrame" Const gcClassnameMSVBasic = "wndclass_desked_gsk" Const gcClassnameNotePad = "Notepad" Const gcClassnameMyVBApp = "ThunderForm" Private Sub Form_Load() 'KPD-Team 1998 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String 'Ask for a Window title Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match") 'Search the window WinWnd = FindWindow(vbNullString, Ret) If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub 'Show the window ShowWindow WinWnd, SW_SHOWNORMAL 'Create a buffer lpClassName = Space(256) 'retrieve the class name RetVal = GetClassName(WinWnd, lpClassName, 256) 'Show the classname MsgBox "Classname: " + Left$(lpClassName, RetVal) 'Post a message to the window to close itself PostMessage WinWnd, WM_CLOSE, 0&, 0& ' PostMessage WinWnd, WM_ACTIVATE, 0&, 0& End Sub
PostMessage will send the message to the windows message queue and return immediatly and SendMessage will send the
message to the windows message queue and wait for it to process it and then return. Other then that they are the same.
You can not be sure that if you close a form it will close the entire app. The better solution is to Terminate the Process.
Or you could just use firefox as it doesnt take much to corrupt it. ;)
That code just gets the classname, and closes the window, I need to end a process...
I was just trying to get the classname, and postmessage closed it. I was trying to use "tskill.exe"
Thank you everybody! I got it! :)
Its easy to use too :D
VB Code:
Private Sub Form_Load() TerminateEXE "FireFox.exe" End Sub