Re: task list

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Tom Esh (tjeshGibberish_at_earthlink.net)
Date: 05/04/04


Date: Tue, 04 May 2004 14:44:25 -0400

On Tue, 4 May 2004 14:01:47 -0400, "ted Bogucki" <ted@sma-promail.com>
wrote:

>Does anyone know how to get the list of tasks running.
>I have written a program that starts up other programs. I want to be
>able to check to see it the processed that I created is still associated
>with the program that my main program started or if the program aborted for
>any reason.

You can enumerate the processes with:
        CreateToolhelp32Snapshot
        Process32First
        Process32Next

However if you're only interested in processes that your app has
started, it may make more sense to grab the PID or a process handle
when you launch them. This also has the advantage of identifying only
those instances you've launched (as opposed to any others the user may
have started). VB's Shell function returns the PID, which you can
subsequently use with OpenProcess to obtain a handle. Alternatively
you could use CreateProcess to launch them, which provides a proc
handle. Once you have the proc handle, you can check the state of the
process with GetExitCodeProcess.

Ex (air-code, sans declares):
Dim m_PIDs As Collection 'PID stash

Public Sub LaunchApp(sPath As String)
        Dim PID As Long

        PID = Shell(sPath, vbNormalFocus)
        If PID <> 0 Then
                m_PIDs.Add PID, sPath
        End If
End Sub

Public Function IsProcRunning(sPath As String) As Boolean
        Dim PID As Long
        Dim hProc As Long
        Dim ExitCode As Long

        PID = m_PIDs.Item(sPath)
        hProc = OpenProcess(PROCESS_QUERY_INFORMATION Or SYNCHRONIZE, _
                0, PID)
        If hProc <> 0 Then
                GetExitCodeProcess hProc, ExitCode
                If ExitCode = STILL_ACTIVE Then
                        IsProcRunning = True
                End If
                CloseHandle hProc
        End If
End Function

-Tom
MVP - Visual Basic
(please post replies to the newsgroup)



Relevant Pages

  • Re: GetProcessId API call
    ... "second" process you launch - if it is indeed another PE, ... lpVerb As String ... Private Declare Function NtQueryInformationProcess Lib "ntdll.dll" _ ... (ByVal hProcess As Long, _ ...
    (microsoft.public.vb.winapi)
  • Shell function
    ... ByVal lpOperation As String, _ ... Dim lRet As Long, varTaskID As Variant ... stFile, vbNullString, vbNullString, lShowHow) ... I launch it like this: ...
    (microsoft.public.access.modulesdaovba)
  • Re: Word Object Library / Spelling Checker
    ... > Below is a snippet of code used to launch the document: ... > Public Sub OpenDocument ... > Dim oWord As Word.Application ... > Dim oDoc As Word.Document ...
    (microsoft.public.word.vba.general)
  • Re: HtmlHelp causes error on application close
    ... url contains the QuitHelp subroutine which when in the unload event ... Public Sub QuitHelp() ... When I launch MyHelp.chm from a MSACCESS 2000 application using the code ... above the help window ...
    (comp.databases.ms-access)
  • Re: Cant get IWbemConfigureRefresher::AddObjectByPath to work with Win32_PerfRawData_PerfProc_Proces
    ... OK following your suggestion I've formatted the string, wstr, as ... where pid is the processID that I'm interested in. ... But I want to call AddObjectByPath ... Manbinder Pal Singh wrote: ...
    (microsoft.public.win32.programmer.wmi)