Results 1 to 12 of 12

Thread: How do I terminate a process in VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    How do I terminate a process in VB

    I have Googled and Yahooed this Question for Over a week now with NO LUCK. My question is this....
    I've already figured out how, by using API's Start a Exe file then once it's started I can Terminate it, that much I understand.

    What I can't find anywhere in any forum or on google, is How can terminate an already existing executable.

    Let's say a program is started from the run when I boot up my computer. It's a simple program that we'll call putty.exe.

    How do I write a VB 6 App that can find My putty.exe program that's already running and terminate it.

    Like I said earlier, I have learned how to Start a Program, then grab the info about the program I need to Terminate it, then Use those values to terminate it...but terminating a program that is already running when the computer is booted up I can not Figure out...

    Kinda like I need to find the running processes, ya know when you hit "ctrl, alt + del" the task manager displays then list of running apps, I want to find one of those apps and terminate it.

    Anyone know how to do this?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do I terminate a process in VB

    Welcome to the forums.

    Does putty.exe always have the same window caption?

  3. #3
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: How do I terminate a process in VB

    can't remember where this originated, but it works for me:

    VB Code:
    1. Option Explicit
    2.     Private Declare Function SendMessage Lib "user32" _
    3.     Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg _
    4.     As Long, ByVal wParam As Long, ByVal lParam As Long) _
    5.     As Long
    6.    
    7.     Const WM_SYSCOMMAND = &H112&
    8.     Const SC_SCREENSAVE = &HF140&
    9.  
    10. Public boolCancel As Boolean
    11. ' Set the "OnTop Of All Windows"
    12. Const SWP_NOSIZE = &H1
    13. Const SWP_NOMOVE = &H2
    14. Const SWP_NOACTIVATE = &H10
    15. Const SWP_SHOWWINDOW = &H40
    16. Const HWND_BOTTOM = 1
    17. Const HWND_TOP = -1
    18.  
    19. Private Declare Sub SetWindowPos Lib "user32" (ByVal hWnd As Long, _
    20.   ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _
    21.   ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    22.  
    23.  
    24.  
    25. Private Type PROCESSENTRY32
    26.   dwSize As Long
    27.   cntUsage As Long
    28.   th32ProcessID As Long
    29.   th32DefaultHeapID As Long
    30.   th32ModuleID As Long
    31.   cntThreads As Long
    32.   th32ParentProcessID As Long
    33.   pcPriClassBase As Long
    34.   dwFlags As Long
    35.   szExeFile As String * 260
    36. End Type
    37.  
    38. Private Type OSVERSIONINFO
    39.   dwOSVersionInfoSize As Long
    40.   dwMajorVersion As Long
    41.   dwMinorVersion As Long
    42.   dwBuildNumber As Long
    43.   dwPlatformId As Long
    44.   szCSDVersion As String * 128
    45. End Type
    46.  
    47. Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    48. Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    49. Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
    50. Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    51. Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    52. 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
    53. Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    54. Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    55. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    56. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    57.  
    58. Private Const PROCESS_TERMINATE = &H1
    59. Private Const VER_PLATFORM_WIN32_WINDOWS = 1
    60. Private Const PROCESS_QUERY_INFORMATION = 1024
    61. Private Const PROCESS_VM_READ = 16
    62. Private Const TH32CS_SNAPPROCESS = &H2
    63.  
    64. Private Function CheckVersion() As Long
    65.   Dim tOS As OSVERSIONINFO
    66.   tOS.dwOSVersionInfoSize = Len(tOS)
    67.   Call GetVersionEx(tOS)
    68.   CheckVersion = tOS.dwPlatformId
    69. End Function
    70.  
    71. Public Function GetEXEProcessID(ByVal sEXE As String) As Long
    72.   Dim aPID() As Long
    73.   Dim lProcesses As Long
    74.   Dim lProcess As Long
    75.   Dim lModule As Long
    76.   Dim sName As String
    77.   Dim iIndex As Integer
    78.   Dim bCopied As Long
    79.   Dim lSnapShot As Long
    80.   Dim tPE As PROCESSENTRY32
    81.   Dim bDone As Boolean
    82.  
    83.   If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
    84.     'Windows 9x
    85.     'Create a SnapShot of the Currently Running Processes
    86.     lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    87.     If lSnapShot < 0 Then Exit Function
    88.     tPE.dwSize = Len(tPE)
    89.     'Buffer the First Processes Info..
    90.     bCopied = Process32First(lSnapShot, tPE)
    91.     Do While bCopied
    92.       'While there are Processes List them..
    93.       sName = Left$(tPE.szExeFile, InStr(tPE.szExeFile, Chr(0)) - 1)
    94.       sName = Mid(sName, InStrRev(sName, "\") + 1)
    95.       If InStr(sName, Chr(0)) Then
    96.         sName = Left(sName, InStr(sName, Chr(0)) - 1)
    97.       End If
    98.       bCopied = Process32Next(lSnapShot, tPE)
    99.       If StrComp(sEXE, sName, vbTextCompare) = 0 Then
    100.         GetEXEProcessID = tPE.th32ProcessID
    101.         Exit Do
    102.       End If
    103.     Loop
    104.    
    105.   Else
    106.     'Windows NT
    107.     'The EnumProcesses Function doesn't indicate how many Process there are,
    108.     'so you need to pass a large array and trim off the empty elements
    109.     'as cbNeeded will return the no. of Processes copied.
    110.     ReDim aPID(255)
    111.     Call EnumProcesses(aPID(0), 1024, lProcesses)
    112.     lProcesses = lProcesses / 4
    113.     ReDim Preserve aPID(lProcesses)
    114.    
    115.     For iIndex = 0 To lProcesses - 1
    116.       'Get the Process Handle, by Opening the Process
    117.       lProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aPID(iIndex))
    118.       If lProcess Then
    119.         'Just get the First Module, all we need is the Handle to get
    120.         'the Filename..
    121.         If EnumProcessModules(lProcess, lModule, 4, 0&) Then
    122.           sName = Space(260)
    123.           Call GetModuleFileNameExA(lProcess, lModule, sName, Len(sName))
    124.           If InStr(sName, "\") > 0 Then
    125.             sName = Mid(sName, InStrRev(sName, "\") + 1)
    126.           End If
    127.           If InStr(sName, Chr(0)) Then
    128.             sName = Left(sName, InStr(sName, Chr(0)) - 1)
    129.           End If
    130.           If StrComp(sEXE, sName, vbTextCompare) = 0 Then
    131.             GetEXEProcessID = aPID(iIndex)
    132.             bDone = True
    133.           End If
    134.         End If
    135.         'Close the Process Handle
    136.         CloseHandle lProcess
    137.         'If bDone Then Exit For
    138.       End If
    139.     Next
    140.   End If
    141. End Function
    142.  
    143. Public Function TerminateEXE(ByVal sEXE As String) As Boolean
    144.   Dim lPID As Long
    145.   Dim lProcess As Long
    146.  
    147.   lPID = GetEXEProcessID(sEXE)
    148.   If lPID = 0 Then Exit Function
    149.   lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID)
    150.   Call TerminateProcess(lProcess, 0&)
    151.   Call CloseHandle(lProcess)
    152.  
    153.   TerminateEXE = True
    154. End Function

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    Re: How do I terminate a process in VB

    Yes Putty always has the Same Window Caption, by window caption you mean the title in the top of the window, correct? Like the title in the top left of the Man I can't believe the responce of this forum, Thanks everyone.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do I terminate a process in VB

    Quote Originally Posted by dr3vi1
    Yes Putty always has the Same Window Caption, by window caption you mean the title in the top of the window, correct? Like the title in the top left of the Man I can't believe the responce of this forum, Thanks everyone.
    Yep, thats what I mean. This should fix you up.
    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    2. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    4. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    5. ByVal lParam As Long) As Long
    6.  
    7. Private Const WM_CLOSE = &H10
    8.  
    9. Private Sub cmdCloseApp_Click()
    10. Dim lngCloseIt As Long
    11. lngCloseIt = FindWindow(vbNullString, "Caption Of Window To Be Closed")
    12. PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)
    13. End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    Re: How do I terminate a process in VB

    Thanks Hack!

    I'll give it a try tonight when I get home from work.
    Much appreciated. I'll keep you posted.

    Question though, will the "Caption Of Window To Be Closed" string, terminate any and all app's with the same window caption?

    Peace.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do I terminate a process in VB

    Quote Originally Posted by dr3vi1
    Thanks Hack!

    I'll give it a try tonight when I get home from work.
    Much appreciated. I'll keep you posted.

    Question though, will the "Caption Of Window To Be Closed" string, terminate any and all app's with the same window caption?

    Peace.
    It will terminate the app that possess that caption and the handle found by the FindWindow call. Each running window has its own, unique handle.

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

    Re: How do I terminate a process in VB

    If its not the main window of the app it may not quit the program.
    You may also try using the message WM_QUIT to the window.
    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
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do I terminate a process in VB

    Quote Originally Posted by RobDog888
    If its not the main window of the app it may not quit the program.
    You may also try using the message WM_QUIT to the window.
    RobDog888 is correct. Occassionally WM_QUIT is necessary to use.

    If you find the need to use that, it is declared as follows:

    Private Const WM_QUIT = &H12

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    Re: How do I terminate a process in VB

    Hay that's Great Guy's
    I'll be trying it tonight.

    Peace

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    Re: How do I terminate a process in VB

    Sorry I must have done something wrong Hack, it only terminated it's self, I had a buddy of mine write an app that does it...It works great, I'll post the code up here tonight or tommorow, once I verify the code works for vb 6 not dot .net.

    But thanks for all your help guys!

    Have a good one Hack, Muddy & RobDog888

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do I terminate a process in VB

    Quote Originally Posted by dr3vi1
    Sorry I must have done something wrong Hack, it only terminated it's self, I had a buddy of mine write an app that does it...It works great, I'll post the code up here tonight or tommorow, once I verify the code works for vb 6 not dot .net.

    But thanks for all your help guys!

    Have a good one Hack, Muddy & RobDog888
    Yes, please do post the code as I have used the routine I posted many many times without issues (and on a variety of OS's). I will look forward to seeing what you have.

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