Results 1 to 15 of 15

Thread: Killing All Excel Processes

  1. #1

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    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.

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    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.

  3. #3
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    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

  4. #4

  5. #5

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    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:
    1. Const MAX_PATH& = 260
    2. Private Type PROCESSENTRY32
    3.     dwSize As Long
    4.     cntUsage As Long
    5.     th32ProcessID As Long
    6.     th32DefaultHeapID As Long
    7.     th32ModuleID As Long
    8.     cntThreads As Long
    9.     th32ParentProcessID As Long
    10.     pcPriClassBase As Long
    11.     dwFlags As Long
    12.     szexeFile As String * MAX_PATH
    13. End Type
    14.  
    15. Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
    16. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
    17. Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    18. Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    19. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    20. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    21.  
    22. 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
    23. Const PROCESS_ALL_ACCESS = 0
    24. Dim uProcess As PROCESSENTRY32
    25. Dim rProcessFound As Long
    26. Dim hSnapshot As Long
    27. Dim szExename As String
    28. Dim exitCode As Long
    29. Dim myProcess As Long
    30. Dim AppKill As Boolean
    31. Dim appCount As Integer
    32. Dim i As Integer
    33. On Local Error GoTo Finish
    34. appCount = 0
    35. Const TH32CS_SNAPPROCESS As Long = 2&
    36.  
    37. uProcess.dwSize = Len(uProcess)
    38. hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    39. rProcessFound = ProcessFirst(hSnapshot, uProcess)
    40.  
    41.  
    42. Do While rProcessFound
    43.     i = InStr(1, uProcess.szexeFile, Chr(0))
    44.     szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    45.    
    46.     If Right$(szExename, Len(myName)) = LCase$(myName) Then
    47.         RemoveApp = True
    48.         appCount = appCount + 1
    49.         myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    50.         AppKill = TerminateProcess(myProcess, exitCode)
    51.         Call CloseHandle(myProcess)
    52.     End If
    53.     rProcessFound = ProcessNext(hSnapshot, uProcess)
    54. Loop
    55. Call CloseHandle(hSnapshot)
    56. Finish:
    57. End Function
    58. Private Sub form_load()
    59.    RemoveApp ("EXCEL.EXE")
    60. 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.

  6. #6

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    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.

  7. #7
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    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

  8. #8

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    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.

  9. #9
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    That's how to call terminateprocess() so it will work for you....

  10. #10

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Oh...LOL...I got you now. Thanks, Jeremy
    He who listens well, speaks well.

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

  12. #12

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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

  14. #14
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969
    There is a topic in the VB codebank with an example:

    VB - Find and kill running applications

  15. #15
    New Member
    Join Date
    Mar 2005
    Posts
    4

    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
  •  



Click Here to Expand Forum to Full Width