Results 1 to 9 of 9

Thread: Terminating A Process And Deleting It

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    208

    Terminating A Process And Deleting It

    Ok i want to make a program that detects a process e.g: "virus.exe" and it killis it and then deletes it from the computer.

    But the big question is HOW?

  2. #2
    Hyperactive Member StreaksAthlete's Avatar
    Join Date
    Sep 2004
    Posts
    348

    Thumbs up Re: Terminating A Process And Deleting It

    To Kill the Process; First, Paste this code into A Module:

    VB Code:
    1. Type PROCESSENTRY32
    2.     dwSize As Long
    3.     cntUsage As Long
    4.     th32ProcessID As Long
    5.     th32DefaultHeapID As Long
    6.     th32ModuleID As Long
    7.     cntThreads As Long
    8.     th32ParentProcessID As Long
    9.     pcPriClassBase As Long
    10.     dwFlags As Long
    11.     szexeFile As String * 260
    12. End Type
    13. '-------------------------------------------------------
    14. Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
    15. ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
    16.  
    17. Declare Function ProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, _
    18. uProcess As PROCESSENTRY32) As Long
    19.  
    20. Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, _
    21. uProcess As PROCESSENTRY32) As Long
    22.  
    23. Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" ( _
    24. ByVal lFlags As Long, lProcessID As Long) As Long
    25.  
    26. Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, _
    27. ByVal uExitCode As Long) As Long
    28.  
    29. Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    30.  
    31.  
    32. '-------------------------------------------------------
    33. Public Sub KillProcess(NameProcess As String)
    34. Const PROCESS_ALL_ACCESS = &H1F0FFF
    35. Const TH32CS_SNAPPROCESS As Long = 2&
    36. Dim uProcess  As PROCESSENTRY32
    37. Dim RProcessFound As Long
    38. Dim hSnapshot As Long
    39. Dim SzExename As String
    40. Dim ExitCode As Long
    41. Dim MyProcess As Long
    42. Dim AppKill As Boolean
    43. Dim AppCount As Integer
    44. Dim i As Integer
    45. Dim WinDirEnv As String
    46.        
    47.        If NameProcess <> "" Then
    48.           AppCount = 0
    49.  
    50.           uProcess.dwSize = Len(uProcess)
    51.           hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    52.           RProcessFound = ProcessFirst(hSnapshot, uProcess)
    53.  
    54.           Do
    55.             i = InStr(1, uProcess.szexeFile, Chr(0))
    56.             SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    57.             WinDirEnv = Environ("Windir") + "\"
    58.             WinDirEnv = LCase$(WinDirEnv)
    59.        
    60.             If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess) Then
    61.                AppCount = AppCount + 1
    62.                MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    63.                AppKill = TerminateProcess(MyProcess, ExitCode)
    64.                Call CloseHandle(MyProcess)
    65.             End If
    66.             RProcessFound = ProcessNext(hSnapshot, uProcess)
    67.           Loop While RProcessFound
    68.           Call CloseHandle(hSnapshot)
    69.        End If
    70.  
    71. End Sub

    THEN put this code in the event you want to kill the process:

    VB Code:
    1. KillProcess ("virus.exe")
    Last edited by StreaksAthlete; Sep 24th, 2006 at 06:46 PM.
    Unity Programs™ - Downloads To Make Your Computer Life Easier - UnityPrograms.com, check it out
    My small company, I need feedback.


    If someone's post was useful, don't forget to Rate It by clicking the Rate This Post link under the avatar.
    If this post has been resolved. Please mark it as Resolved by going through the Thread Tools above and clicking on the Mark Thread Resolved option.
    We ride together, We die together, .NET for life. - Working With Visual Studio.NET 2005

    Application Deployment, General Developer Forum, Visual Basic .NET

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    208

    Re: Terminating A Process And Deleting It

    dose this delete it to?

  4. #4
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Re: Terminating A Process And Deleting It

    Quote Originally Posted by Gunner54
    dose this delete it to?
    No, it only terminates the process. It does not delete the file with it. You would need to use the KILL function if you wanted to delete the file after it has been terminated:
    VB Code:
    1. Kill sFileName
    Last edited by Fedhax; Sep 25th, 2006 at 10:33 AM. Reason: VB Code Correction

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    208

    Re: Terminating A Process And Deleting It

    so i do

    VB Code:
    1. Call Kill "virus.exe"

  6. #6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    208

    Re: Terminating A Process And Deleting It

    btw what if i put
    VB Code:
    1. Kill "C:\virus.exe"
    when their hard drive is ->
    Code:
    D:\
    but i didnt know?

    in other words if i made it
    VB Code:
    1. Kill "C:\virus.exe"
    but on someone elses PC they didnt have a
    Code:
    C:\
    they had a
    Code:
    D:\
    would it still work?

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

    Re: Terminating A Process And Deleting It

    You need to get the file and path location of the running process. Then you can kill it after its terminated. I think its the .ModuleName property of the process that contains the full path and filename.
    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    208

    Re: Terminating A Process And Deleting It

    ................... :S i do not understans XD ok i think i will be fine

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