Solved

Ending other tasks

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Access 2016 VB code 9 86
DIR issue 7 47
Validating VB6 Function 19 50
MS Access Search and Replace Using VBA 6 44
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

746 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

10 Experts available now in Live!

Get 1:1 Help Now