Solved

Ending other tasks

Posted on 2000-02-24
5
160 Views
Last Modified: 2010-05-02
How can I find out what other tasks are executing, and terminate those that I choose?
0
Comment
Question by:ADenney
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 2554947


'fill a list box with running processes (won't work in NT)

Attribute VB_Name = "Module1"


      Option Explicit
Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long


Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long


Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long



      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 CreateToolhelp32Snapshot Lib _
      "kernel32" (ByVal dwFlags As Long, _
      ByVal th32ProcessID As Long) As Long
     
Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) 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 Const TH32CS_SNAPPROCESS = &H2&
      Private Const hNull = 0
Public Const WM_SYSCOMMAND = &H112
Public Const SC_CLOSE = &HF060
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const PROCESS_VM_READ = &H10

     Public Function ExeList(x As ListBox)

          Dim hProc As Long
          Dim lRet As Long, Proc As PROCESSENTRY32
          Dim andy As Long, sDeighton As String
          Dim sClean As String
         
          hProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
           
           
          If hProc = 0 Then
              Exit Function
          End If
           
          Proc.dwSize = Len(Proc)
          lRet = Process32First(hProc, Proc)
           
          Do While lRet
           
              sClean = Left(Proc.szExeFile, InStr(Proc.szExeFile, Chr(0)) - 1)
           
              sDeighton = ""
              For andy = Len(sClean) To 1 Step -1
               
                If Mid(sClean, andy, 1) = "\" Then Exit For
               
                sDeighton = Mid(sClean, andy, 1) & sDeighton
               
              Next
             
              x.AddItem sDeighton
                               
              lRet = Process32Next(hProc, Proc)
           
          Loop

      End Function


'The kill a process

'ExeRunning("Notepad.exe") for example

      Public Function ExeRunning(ByVal ExeFind As String) As Boolean

          Dim hProc As Long
          Dim lRet As Long, Proc As PROCESSENTRY32
          Dim andy As Long, sDeighton As String
          Dim sClean As String
          Dim IRETURNCODE As Long
          Dim ProcFromprocid As Long
          Dim lpExitCode As Long
         
             Const NILL = 0&
         
          hProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
           
          ExeRunning = False
           
          If hProc = 0 Then
              Exit Function
          End If
           
          Proc.dwSize = Len(Proc)

          lRet = Process32First(hProc, Proc)
           
          Do While lRet
           
              Debug.Print Proc.th32ProcessID
              sClean = Left(Proc.szExeFile, InStr(Proc.szExeFile, Chr(0)) - 1)
           
              sDeighton = ""
              For andy = Len(sClean) To 1 Step -1
               
                If Mid(sClean, andy, 1) = "\" Then Exit For
               
                sDeighton = Mid(sClean, andy, 1) & sDeighton
               
              Next
             
              If UCase(sDeighton) = UCase(ExeFind) Then
                  ExeRunning = True
                  ProcFromprocid = OpenProcess(PROCESS_QUERY_INFORMATION Or _
                  PROCESS_VM_READ, 0, Proc.th32ProcessID)
                 
                  Call GetExitCodeProcess(ProcFromprocid, lpExitCode)
                  Call TerminateProcess(ProcFromprocid, lpExitCode)
                 
                  Exit Function
              End If
               
              lRet = Process32Next(hProc, Proc)
           
          Loop

      End Function
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2554967
Check out the following microsoft KB articles:


HOWTO: Programmatically Close a Separate Application
http://support.microsoft.com/support/kb/articles/Q176/3/91.ASP?LNG=ENG&SA=MSDN&FR=1 

HOWTO: Enumerate Windows Using the WIN32 API
http://support.microsoft.com/support/kb/articles/Q183/0/09.ASP?LNG=ENG&SA=MSDN&FR=1 
 
How to Get Windows Master List (Task List)
http://support.microsoft.com/support/kb/articles/Q78/0/01.ASP?LNG=ENG&SA=MSDN&FR=1 


Cheers!
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2555230
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2555247
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2607098
ADenney,

Are you still there??  Did your problem get solved? if so, please accept an answer and grade it...


Cheers!®©
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question