|
-
Feb 21st, 2005, 01:25 AM
#1
Thread Starter
Lively Member
Kill Application
I want to kill an application. For example internet explorer. However the windows caption is always different so i would like to kill it via it's filename (i.e iexplore.exe).
Help appreciated thanks
-
Feb 21st, 2005, 01:34 AM
#2
Re: Kill Application
VB Code:
Option Explicit
Option Compare Text 'IMPORTANT (assuming you do not want the exe name to be case sensitive)
Private Declare Function OpenProcess Lib "KERNEL32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "KERNEL32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetExitCodeProcess Lib "KERNEL32.dll" (ByVal hProcess As Long, ByRef lpExitCode As Long) As Long
Private Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal hProcess As Long, ByRef lphModule As Any, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "PSAPI.DLL" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Private Const PROCESS_VM_READ = 16
Private Const PROCESS_TERMINATE As Long = (&H1)
Private Const PROCESS_QUERY_INFORMATION As Long = (&H400)
Private Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.dll" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
Private Const TH32CS_SNAPPROCESS = &H2
Private Const MAX_PATH As Long = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
'********************************
'Created by Mark Gordon (msg555)
'Date December 4th, 2004
'
'Returns if the Function was succesfull
' EXEName - Name of the EXE of the process. It could also be a dll or other file
'********************************
Public Function TerminateEXE(ByVal EXEName As String) As Boolean
Dim hSnap As Long
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If hSnap Then
Dim Proc32 As PROCESSENTRY32, BackSlash As Long, Zero As Long, hName As String
Proc32.dwSize = Len(Proc32)
If Process32First(hSnap, Proc32) Then
Do
'Do Some Parsing
Zero = InStr(Proc32.szExeFile, Chr(0))
If Zero Then
hName = Left(Proc32.szExeFile, Zero - 1)
Else
hName = Proc32.szExeFile
End If
BackSlash = InStrRev(hName, "\")
If BackSlash Then hName = Mid(hName, BackSlash + 1)
If hName = EXEName Then
'We have a match. TERMINATE
Dim hProc As Long, hExitCode As Long
hProc = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, 1, Proc32.th32ProcessID)
GetExitCodeProcess hProc, hExitCode
If TerminateProcess(hProc, hExitCode) Then TerminateEXE = True
CloseHandle hProc
CloseHandle hSnap
Exit Function
End If
Loop While Process32Next(hSnap, Proc32)
End If
End If
CloseHandle hSnap
End Function
-
Feb 21st, 2005, 01:38 AM
#3
Re: Kill Application
You can use Wokawidgit's code to terminate a runnng process.
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 
-
Feb 21st, 2005, 01:44 AM
#4
Re: Kill Application
Hey Deepak, that code wont work for a Windows 98 system. Terminating a process is
dependent on the OS.
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 
-
Feb 21st, 2005, 01:48 AM
#5
Re: Kill Application
eh.. I don't have Win 98 System. K then BefunMunkToloGen u should try Woka's code.
Rob can u help me here
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
|