Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Ending other tasks

Posted on 2000-02-24
5
Medium Priority
?
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses

636 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