|
-
Oct 2nd, 2003, 03:06 PM
#1
Thread Starter
Fanatic Member
Killing All Excel Processes
Patience is a virtue but after searching the pages of the VBF Search, I gave up. I need to kill all "EXCEL.EXE" processes using VB Code. Can someone help me out? Thanks, Jeremy
He who listens well, speaks well.
-
Oct 2nd, 2003, 07:16 PM
#2
Never needed to use it but you might try the KillProcess API
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Oct 3rd, 2003, 09:24 AM
#3
Frenzied Member
Look carefully at the PROCESSENTRY32 UDT.
It has a process handle as well as the name of the .EXE the process is running
Call TerminateProcess() on the handle of each process that has
"Excel.exe" as the .exe name
See:http://www.mvps.org/vbnet/code/syste...pprocesses.htm
Code:
Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH As Long = 260
Public 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 * MAX_PATH
End Type
-
Oct 3rd, 2003, 09:32 AM
#4
If you do a search on VBF for "KILL AND process" then there are loads and loads of posts that show u how to achieve this.
Woka
-
Oct 3rd, 2003, 09:34 AM
#5
Thread Starter
Fanatic Member
Jim,
I have tried something similar but the TerminateProcess() doesn't appear to work. I'll post the code so you might be able to help me see why it doesn't work. It's finding the proper processes but it never terminates.
VB Code:
Const MAX_PATH& = 260
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 * MAX_PATH
End Type
Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId 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 Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Function RemoveApp(myName As String) As Boolean 'funcion que recibe la cadena de la aplicacion a aniquilar - function that receives the name of the application that is going to be killed
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 myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim i As Integer
On Local Error GoTo Finish
appCount = 0
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
RemoveApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Finish:
End Function
Private Sub form_load()
RemoveApp ("EXCEL.EXE")
End Sub
It will find the instances of EXCEL.EXE but when TerminateProcess is called, nothing seems to happen to the EXCEL.EXE processes. I hope you can see why. Thanks, Jeremy
He who listens well, speaks well.
-
Oct 3rd, 2003, 09:42 AM
#6
Thread Starter
Fanatic Member
Originally posted by Wokawidget
If you do a search on VBF for "KILL AND process" then there are loads and loads of posts that show u how to achieve this.
Woka
No kidding! I stated that I had done that in my first post. There was nothing that has worked yet. There was also code that contradicted the API that I found to use. The end result is I have searched VBF, Google and still nothing that works yet. Laters, Jeremy
He who listens well, speaks well.
-
Oct 3rd, 2003, 11:28 AM
#7
Frenzied Member
Try:
Code:
Const PROCESS_TERMINATE = &H0001
SUB KillByTerminate (BYVAL ProcID AS LONG)
LOCAL hProc AS LONG
hProc = OpenProcess(BYVAL PROCESS_TERMINATE, BYVAL 0&,BYVAL ProcID)
IF hProc <> 0 THEN
TerminateProcess BYVAL hProc, BYVAL 0&
CloseHandle hProc
END SUB
-
Oct 3rd, 2003, 11:36 AM
#8
Thread Starter
Fanatic Member
I hate to sound dumb but how would I use that? How does it know to end all Excel.exe processes? Thanks, Jeremy
He who listens well, speaks well.
-
Oct 3rd, 2003, 11:46 AM
#9
Frenzied Member
That's how to call terminateprocess() so it will work for you....
-
Oct 3rd, 2003, 11:52 AM
#10
Thread Starter
Fanatic Member
Oh...LOL...I got you now. Thanks, Jeremy
He who listens well, speaks well.
-
Oct 4th, 2003, 07:22 AM
#11
Dunno if you sorted it, but this is what I use to terminate a process...
Apparently the terminate process code is OS dependant 
Woka
VB Code:
Option Explicit
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
Do
lPID = GetEXEProcessID(sEXE)
If lPID <> 0 Then
lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID)
Call TerminateProcess(lProcess, 0&)
Call CloseHandle(lProcess)
End If
Loop Until lPID = 0
TerminateEXE = True
End Function
Put that in a module then:
VB Code:
Private Sub Form_Load()
TerminateEXE "EXCEL.EXE"
End Sub
Hope that helps.
Last edited by Wokawidget; Feb 13th, 2005 at 12:42 AM.
-
Sep 11th, 2004, 12:04 PM
#12
New Member
-
Sep 12th, 2004, 12:34 PM
#13
If the Excel processes are created by your program and not being closed properly, let us see the code and I'll help sort it out
-
Sep 13th, 2004, 08:09 AM
#14
There is a topic in the VB codebank with an example:
VB - Find and kill running applications
-
Mar 22nd, 2005, 02:17 PM
#15
New Member
Re: Killing All Excel Processes
I cut and pasted Wokawiget's code into my module and it works a treat. There must be a simpler way of doing this but now that code is no longer as memory sensitive as many moons ago, I'm leaving it there - as indecipherable as Chinese to me!
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
|