|
-
Jul 14th, 2005, 06:21 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Close Program
How do I close a running application via VB 6?
Thank you,
Sir Loin
-
Jul 14th, 2005, 06:27 PM
#2
Re: Close Program
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.
-
Jul 14th, 2005, 06:31 PM
#3
Thread Starter
Fanatic Member
Re: Close Program
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]
'end this process
ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
End Sub
What process would that kill though?
Thank you,
Sir Loin
-
Jul 14th, 2005, 06:39 PM
#4
Re: Close Program
Try this
VB 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
-
Jul 14th, 2005, 06:41 PM
#5
Re: Close Program
SendMessage can do it as well, right?
-
Jul 14th, 2005, 06:44 PM
#6
Thread Starter
Fanatic Member
Re: Close Program
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
-
Jul 14th, 2005, 06:45 PM
#7
Re: Close Program
 Originally Posted by Jacob Roman
SendMessage can do it as well, right?
PostMessage will close it immediately.
-
Jul 14th, 2005, 06:57 PM
#8
Thread Starter
Fanatic Member
Re: Close Program
What Process does GetCurrentProcess get?
-
Jul 14th, 2005, 07:06 PM
#9
-
Jul 14th, 2005, 07:13 PM
#10
Thread Starter
Fanatic Member
Re: Close Program
Oh, thanks.
So how would I be able to end FireFox.exe?
Thank You,
Sir Loin
-
Jul 14th, 2005, 08:29 PM
#11
Re: Close Program
I think the processname is firefox.exe, so
VB Code:
Shell "tskill firefox.exe"
will work on XP
-
Jul 14th, 2005, 09:53 PM
#12
Thread Starter
Fanatic Member
Re: Close Program
You need the processID to use tskill, how do I get the process ID?
Will tskill only work on WinXP?
Thank You,
Sir Loin
-
Jul 14th, 2005, 10:00 PM
#13
Re: Close Program
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]
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
-
Jul 14th, 2005, 10:08 PM
#14
Re: Close Program
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jul 14th, 2005, 10:10 PM
#15
Thread Starter
Fanatic Member
Re: Close Program
That code just gets the classname, and closes the window, I need to end a process...
-
Jul 14th, 2005, 10:12 PM
#16
Re: Close Program
I was just trying to get the classname, and postmessage closed it. I was trying to use "tskill.exe"
-
Jul 14th, 2005, 10:12 PM
#17
Thread Starter
Fanatic Member
Re: Close Program
Thank you everybody! I got it!
-
Jul 14th, 2005, 10:30 PM
#18
Re: [RESOLVED] Close Program
Its easy to use too 
VB Code:
Private Sub Form_Load()
TerminateEXE "FireFox.exe"
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
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
|