Solved

Ending other tasks

Posted on 2000-02-24
5
156 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 54
VBA filters 2 59
How to update a sql 2012 database table with the time difference from 2 time frames in vb.net 2010 4 83
VB6 ListBox Question 4 42
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now