|
-
Jun 26th, 2003, 03:28 PM
#1
Thread Starter
Fanatic Member
How can you terminate a process by its name?
anyone know?
such as ..
vb6.exe
explorer.exe
svchost.exe
etc
-
Jun 26th, 2003, 03:54 PM
#2
Registered User
Here goes, noobie
VB Code:
Public Function KillAppByName(MyName As String) As Boolean 'kills applications by name
Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim AppKill As Long
Dim i As Integer
Dim lProcHnd As Long
Dim hWnd As Long
On Local Error GoTo ErrTrap
If winVersion = "WNT3" Or winVersion = "WNT4" Then Exit Function
Const TH32CS_SNAPPROCESS As Long = 2&
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
If Right$(szExename, Len(MyName)) = LCase$(MyName) Then
lProcHnd = OpenProcess(PROCESS_TERMINATE, 0&, uProcess.th32ProcessID)
AppKill = TerminateProcess(lProcHnd, exitCode)
Call CloseHandle(lProcHnd)
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Exit Function
End Function
-
Jun 26th, 2003, 03:57 PM
#3
Registered User
Declarations:
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Const PROCESS_TERMINATE = &H1
Private Const SC_CLOSE = &HF060&
Private Const WM_SYSCOMMAND = &H112
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
-
Jun 26th, 2003, 04:02 PM
#4
Lively Member
Re: How can you terminate a process by its name?
Originally posted by n00bie
anyone know?
such as ..
vb6.exe
explorer.exe
svchost.exe
etc
First off, you'll need to know the Apps title. Use the FindWindow API to get the handle of the app. Next, PostMessage API to the app WM_CLOSE. Both of these APIs and examples of how to use them are at www.allapi.net.
-
Jun 26th, 2003, 04:03 PM
#5
Registered User
My function simply needs the name of the program, such as word.exe,...
-
Jun 26th, 2003, 05:29 PM
#6
Thread Starter
Fanatic Member
-
Jun 26th, 2003, 09:57 PM
#7
Thread Starter
Fanatic Member
-
Jun 26th, 2003, 10:12 PM
#8
I thought I'd posted this before, but couldn't find it, anyway...
Try the following in a Standard Code Module:
VB Code:
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
lRet = 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
Example Usage:
VB Code:
TerminateEXE "Notepad.exe"
-
Jun 27th, 2003, 02:13 AM
#9
Fanatic Member
Both our friends, Davos and Aaron Young, have written code that will do the trick, but they are for different platforms. While Davos's code was for Win9x platforms, Aron's code is only for NT based platforms and will not work on Win 9x since Win 9x platforms do not support PSAPI.dll (Process Status API). However, Davos's code will work on both Win 9x as well as Win 2000.
-
Jun 27th, 2003, 02:24 AM
#10
Registered User
Thanks for this. I did not realise.
-
Jun 27th, 2003, 03:31 PM
#11
The code I posted will work on both Win9x and NT based platforms.
-
Jul 12th, 2003, 06:32 PM
#12
Would this link help with what you are trying to do...?
http://www.vbforums.com/showthread.p...55#post1480755
Woka
-
Feb 27th, 2004, 05:30 PM
#13
Hyperactive Member
Thanks Aaron Young,
Works great!
-
Feb 28th, 2004, 07:36 PM
#14
Member
When I try this code on Windows 98, it causes a crash on the line:
lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID)
or possibly the next line. The process does not terminate. I'm using this on an ActiveX EXE btw - need to close it during an install. Works great on WinXP though. Any ideas?
-
Dec 21st, 2004, 09:06 PM
#15
Member
Re: How can you terminate a process by its name?
The documentation of TerminateProcess says:
Use it only in extreme circumstances.
The terms terminate and kill should be used carefully; they usually imply unconditional and immediate removal from the system, as is done by TerminateProcess, which should be avoided. Many problems can be caused by TerminateProcess, including loss of data.
Any time someone says they want to terminate a process but they are not clear about the necessity to actually terminate a process, I ask for clarification.
The first solution that should be tried/suggested is to simply send a WM_CLOSE message to the application's main window. The WM_CLOSE message is essentially the equivalent to clicking the "X" at the top right of the window.
Use of TerminateProcess should be the last resort. It should never be used if there is another way to get the application to end.
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
|