|
-
Apr 27th, 2005, 12:32 PM
#1
Thread Starter
New Member
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?
-
Apr 27th, 2005, 12:41 PM
#2
Re: How do I terminate a process in VB
Welcome to the forums.
Does putty.exe always have the same window caption?
-
Apr 27th, 2005, 12:45 PM
#3
PowerPoster
Re: How do I terminate a process in VB
can't remember where this originated, but it works for me:
VB Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg _
As Long, ByVal wParam As Long, ByVal lParam As Long) _
As Long
Const WM_SYSCOMMAND = &H112&
Const SC_SCREENSAVE = &HF140&
Public boolCancel As Boolean
' Set the "OnTop Of All Windows"
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Const HWND_BOTTOM = 1
Const HWND_TOP = -1
Private Declare Sub SetWindowPos Lib "user32" (ByVal hWnd As Long, _
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _
ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
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 * 260
End Type
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, 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 Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const PROCESS_TERMINATE = &H1
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Const TH32CS_SNAPPROCESS = &H2
Private Function CheckVersion() As Long
Dim tOS As OSVERSIONINFO
tOS.dwOSVersionInfoSize = Len(tOS)
Call GetVersionEx(tOS)
CheckVersion = tOS.dwPlatformId
End Function
Public Function GetEXEProcessID(ByVal sEXE As String) As Long
Dim aPID() As Long
Dim lProcesses As Long
Dim lProcess As Long
Dim lModule As Long
Dim sName As String
Dim iIndex As Integer
Dim bCopied As Long
Dim lSnapShot As Long
Dim tPE As PROCESSENTRY32
Dim bDone As Boolean
If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
'Windows 9x
'Create a SnapShot of the Currently Running Processes
lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If lSnapShot < 0 Then Exit Function
tPE.dwSize = Len(tPE)
'Buffer the First Processes Info..
bCopied = Process32First(lSnapShot, tPE)
Do While bCopied
'While there are Processes List them..
sName = Left$(tPE.szExeFile, InStr(tPE.szExeFile, Chr(0)) - 1)
sName = Mid(sName, InStrRev(sName, "\") + 1)
If InStr(sName, Chr(0)) Then
sName = Left(sName, InStr(sName, Chr(0)) - 1)
End If
bCopied = Process32Next(lSnapShot, tPE)
If StrComp(sEXE, sName, vbTextCompare) = 0 Then
GetEXEProcessID = tPE.th32ProcessID
Exit Do
End If
Loop
Else
'Windows NT
'The EnumProcesses Function doesn't indicate how many Process there are,
'so you need to pass a large array and trim off the empty elements
'as cbNeeded will return the no. of Processes copied.
ReDim aPID(255)
Call EnumProcesses(aPID(0), 1024, lProcesses)
lProcesses = lProcesses / 4
ReDim Preserve aPID(lProcesses)
For iIndex = 0 To lProcesses - 1
'Get the Process Handle, by Opening the Process
lProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aPID(iIndex))
If lProcess Then
'Just get the First Module, all we need is the Handle to get
'the Filename..
If EnumProcessModules(lProcess, lModule, 4, 0&) Then
sName = Space(260)
Call GetModuleFileNameExA(lProcess, lModule, sName, Len(sName))
If InStr(sName, "\") > 0 Then
sName = Mid(sName, InStrRev(sName, "\") + 1)
End If
If InStr(sName, Chr(0)) Then
sName = Left(sName, InStr(sName, Chr(0)) - 1)
End If
If StrComp(sEXE, sName, vbTextCompare) = 0 Then
GetEXEProcessID = aPID(iIndex)
bDone = True
End If
End If
'Close the Process Handle
CloseHandle lProcess
'If bDone Then Exit For
End If
Next
End If
End Function
Public Function TerminateEXE(ByVal sEXE As String) As Boolean
Dim lPID As Long
Dim lProcess As Long
lPID = GetEXEProcessID(sEXE)
If lPID = 0 Then Exit Function
lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID)
Call TerminateProcess(lProcess, 0&)
Call CloseHandle(lProcess)
TerminateEXE = True
End Function
-
Apr 27th, 2005, 01:11 PM
#4
Thread Starter
New Member
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.
-
Apr 27th, 2005, 01:12 PM
#5
Re: How do I terminate a process in VB
 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:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10
Private Sub cmdCloseApp_Click()
Dim lngCloseIt As Long
lngCloseIt = FindWindow(vbNullString, "Caption Of Window To Be Closed")
PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)
End Sub
-
Apr 27th, 2005, 01:19 PM
#6
Thread Starter
New Member
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.
-
Apr 27th, 2005, 01:21 PM
#7
Re: How do I terminate a process in VB
 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.
-
Apr 27th, 2005, 01:21 PM
#8
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 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 
-
Apr 27th, 2005, 01:23 PM
#9
Re: How do I terminate a process in VB
 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
-
Apr 27th, 2005, 02:31 PM
#10
Thread Starter
New Member
Re: How do I terminate a process in VB
Hay that's Great Guy's
I'll be trying it tonight.
Peace
-
Apr 28th, 2005, 01:49 PM
#11
Thread Starter
New Member
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
-
Apr 28th, 2005, 01:51 PM
#12
Re: How do I terminate a process in VB
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|