Results 1 to 6 of 6

Thread: Killing a specific Process ?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Killing a specific Process ?

    Hi,
    I need some code to kill a specific process running...
    I looked up all this forum's posts about killing processes, But I didn't find what I need! ... I need to kill a process by Its extension name, NOT by Its caption or title...!

    Thanx.

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Killing a specific Process ?

    i maybe can,
    but maybe don't want to help you looking at your previous posts on the forum...
    unless you have a very good explanation for wanting to write/delete read-only files and to close processes...

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Re: Killing a specific Process ?

    I totally Agree and respect your opinion
    u see.. I need to make a simple remover tool for Perlovga warm that makes an Autorun for HDs ... etc
    And my other request for read-only ... was bcuz I wanted to edit the boot.ini file (For Personal Use).

    simply thats all!

  4. #4
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Killing a specific Process ?

    To kill it you need to find the process ID or window handle. When killing it by filename problems may occur if you have two processes with the same filename.

    Heres how to get the filename and process ID of all processes running:
    http://vbforums.com/showthread.php?t...hlight=process

    and heres how to kill it by the ID:
    http://en.allexperts.com/q/Visual-Ba...s-VB-its-1.htm

    From those to samples you should be able to create an app that allows you to enter a filename, and have it kill that process.
    Chris

  5. #5
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Killing a specific Process ?

    allright

    srry bout that then...
    let's see

    VB Code:
    1. Public Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    2. Public Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    3. Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    4. Public Declare Function terminateprocess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    5. Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    6. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    7.  
    8. Public Const TH32CS_SNAPPROCESS = &H2
    9. Public Const TH32CS_SNAPheaplist = &H1
    10. Public Const TH32CS_SNAPthread = &H4
    11. Public Const TH32CS_SNAPmodule = &H8
    12. Public Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
    13. Public Const MAX_PATH As Integer = 260
    14.  
    15. Public Type PROCESSENTRY32
    16.  dwSize As Long
    17.  cntUsage As Long
    18.  th32ProcessID As Long
    19.  th32DefaultHeapID As Long
    20.  th32ModuleID As Long
    21.  cntThreads As Long
    22.  th32ParentProcessID As Long
    23.  pcPriClassBase As Long
    24.  dwFlags As Long
    25.  szExeFile As String * MAX_PATH
    26. End Type
    27.  
    28. Public Sub KillProcessById(p_lngProcessId As Long)
    29. On Error Resume Next
    30.  
    31.   Dim lnghProcess As Long
    32.   Dim lngReturn As Long
    33.    
    34.     lnghProcess = OpenProcess(1&, -1&, p_lngProcessId)
    35.     lngReturn = terminateprocess(lnghProcess, 0&)
    36.    
    37.     If lngReturn = 0 Then
    38.         'RetrieveError
    39.     End If
    40.  
    41. End Sub
    42.  
    43. Public Sub KillProcess(ProcessName As String)
    44. On Error Resume Next
    45.  
    46.     Dim LoopI
    47.     Dim Proc                As PROCESSENTRY32
    48.     Dim Snap                As Long
    49.     Dim ExeName             As String
    50.     Dim ext                 As String
    51.    
    52.    
    53.     Snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0)
    54.     Proc.dwSize = Len(Proc)
    55.     LoopI = ProcessFirst(Snap, Proc)
    56.    
    57.     While LoopI <> 0
    58.         ExExist = False
    59.         ExeName = LCase(Proc.szExeFile)
    60.         ext = Split(ProcessName, ".")(1)
    61.         ExeName = Split(ExeName, ext)(0)
    62.         ExeName = ExeName & ext
    63.            
    64.         If ExeName = ProcessName Then
    65.              KillProcessById (Proc.th32ProcessID)
    66.              DoEvents
    67.              Exit Sub
    68.         End If
    69.         LoopI = ProcessNext(Snap, Proc)
    70.     Wend
    71.    
    72.     CloseHandle Snap
    73.  
    74. End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Re: Killing a specific Process ?

    Thanx the182guy, but Its a lil difficult for me to put all that together
    Besides, TheBigB's code is piece of cake

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