Results 1 to 5 of 5

Thread: Kill Application

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    107

    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

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Kill Application

    VB Code:
    1. Option Explicit
    2. Option Compare Text 'IMPORTANT (assuming you do not want the exe name to be case sensitive)
    3.  
    4. Private Declare Function OpenProcess Lib "KERNEL32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    5. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    6. Private Declare Function TerminateProcess Lib "KERNEL32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    7. Private Declare Function GetExitCodeProcess Lib "KERNEL32.dll" (ByVal hProcess As Long, ByRef lpExitCode As Long) As Long
    8. 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
    9. 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
    10. Private Const PROCESS_VM_READ = 16
    11. Private Const PROCESS_TERMINATE As Long = (&H1)
    12. Private Const PROCESS_QUERY_INFORMATION As Long = (&H400)
    13.  
    14. Private Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.dll" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    15. Private Declare Function Process32First Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
    16. Private Declare Function Process32Next Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
    17. Private Const TH32CS_SNAPPROCESS = &H2
    18. Private Const MAX_PATH As Long = 260
    19. Private Type PROCESSENTRY32
    20.     dwSize As Long
    21.     cntUsage As Long
    22.     th32ProcessID As Long
    23.     th32DefaultHeapID As Long
    24.     th32ModuleID As Long
    25.     cntThreads As Long
    26.     th32ParentProcessID As Long
    27.     pcPriClassBase As Long
    28.     dwFlags As Long
    29.     szExeFile As String * MAX_PATH
    30. End Type
    31.  
    32. '********************************
    33. 'Created by Mark Gordon (msg555)
    34. 'Date December 4th, 2004
    35. '
    36. 'Returns if the Function was succesfull
    37. ' EXEName - Name of the EXE of the process. It could also be a dll or other file
    38. '********************************
    39. Public Function TerminateEXE(ByVal EXEName As String) As Boolean
    40. Dim hSnap As Long
    41. hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    42.  
    43. If hSnap Then
    44.     Dim Proc32 As PROCESSENTRY32, BackSlash As Long, Zero As Long, hName As String
    45.     Proc32.dwSize = Len(Proc32)
    46.     If Process32First(hSnap, Proc32) Then
    47.         Do
    48.             'Do Some Parsing
    49.             Zero = InStr(Proc32.szExeFile, Chr(0))
    50.             If Zero Then
    51.                 hName = Left(Proc32.szExeFile, Zero - 1)
    52.             Else
    53.                 hName = Proc32.szExeFile
    54.             End If
    55.             BackSlash = InStrRev(hName, "\")
    56.             If BackSlash Then hName = Mid(hName, BackSlash + 1)
    57.              
    58.             If hName = EXEName Then
    59.                 'We have a match. TERMINATE
    60.                 Dim hProc As Long, hExitCode As Long
    61.                 hProc = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, 1, Proc32.th32ProcessID)
    62.                 GetExitCodeProcess hProc, hExitCode
    63.                 If TerminateProcess(hProc, hExitCode) Then TerminateEXE = True
    64.                 CloseHandle hProc
    65.                 CloseHandle hSnap
    66.                 Exit Function
    67.             End If
    68.              
    69.         Loop While Process32Next(hSnap, Proc32)
    70.     End If
    71. End If
    72.  
    73. CloseHandle hSnap
    74. End Function

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    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
  •  



Click Here to Expand Forum to Full Width