Re: Check if a program (.exe) is currently running.

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

From: Peter Young (youngpa_at_attbi.no.com.spam.please)
Date: 03/02/04

  • Next message: JD: "Using quotes in the command prompt"
    Date: Tue, 2 Mar 2004 13:51:29 -0600
    
    

    "Mattias" <anonymous@discussions.microsoft.com> wrote in message
    news:C50285B2-C62E-4AF2-BB11-678350586F1C@microsoft.com...
    > How do I in VB6 check if a certain program (.exe) is currently running.

    Drop this in a BAS module and call it like so...

    If AppIsRunning("notepad.exe") Then
        'notepad is running
    End If

    ----
    Option Explicit
    Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128
    End Type
    Private Declare Function GetVersionExA Lib "kernel32" _
           (lpVersionInformation As OSVERSIONINFO) As Long
    Private Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long    ' This process
        th32DefaultHeapID As Long
        th32ModuleID As Long    ' Associated exe
        cntThreads As Long
        th32ParentProcessID As Long    ' This process's parent process
        pcPriClassBase As Long    ' Base priority of process threads
        dwFlags As Long
        szExeFile As String * 260    ' MAX_PATH
    End Type
    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" _
           (ByVal dwFlags As Long, _
            ByVal th32ProcessID As Long) As Long
    Private Declare Function Process32First Lib "kernel32" _
           (ByVal hSnapshot As Long, _
            ByRef lppe As PROCESSENTRY32) As Long
    Private Declare Function Process32Next Lib "kernel32" _
           (ByVal hSnapshot As Long, _
            ByRef lppe As PROCESSENTRY32) As Long
    Private Declare Function EnumProcesses Lib "psapi.dll" _
           (ByRef lpidProcess As Long, _
            ByVal cb As Long, _
            ByRef cbNeeded As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" _
           (ByVal dwDesiredAccess As Long, _
            ByVal bInheritHandle As Long, _
            ByVal dwProcessId As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" _
           (ByVal hObject As Long) As Long
    Private Declare Function GetModuleFileNameExA Lib "psapi.dll" _
           (ByVal hProcess As Long, _
            ByVal hModule As Long, _
            ByVal ModuleName As String, _
            ByVal nSize As Long) As Long
    Private Declare Function EnumProcessModules Lib "psapi.dll" _
           (ByVal hProcess As Long, _
            ByRef lphModule As Long, _
            ByVal cb As Long, _
            ByRef cbNeeded As Long) As Long
    Private Const TH32CS_SNAPPROCESS = &H2&
    Private Const PROCESS_VM_READ = 16
    Private Const PROCESS_QUERY_INFORMATION = &H400
    Private Const MAX_PATH = 260
    Public Function AppIsRunning(ByRef sAppName As String) As Boolean
        Dim sCompareText As String
        Dim lCB As Long
        Dim lcbNeeded As Long
        Dim lNumElements As Long
        Dim lProcessIDs() As Long
        Dim lcbNeeded2 As Long
        Dim lModules(1 To 200) As Long
        Dim lRet As Long
        Dim sModuleName As String
        Dim nSize As Long
        Dim hProcess As Long
        Dim i As Long
        Dim sName As String
        Dim hSnap As Long
        Dim tProcEntry As PROCESSENTRY32
        sCompareText = LCase$(sAppName)
        If IsWinNT() Then
            'Get the array containing the process id's for each process object
            lCB = 8
            lcbNeeded = 96
            Do While lCB <= lcbNeeded
                lCB = lCB * 2
                ReDim lProcessIDs(lCB / 4)
                lRet = EnumProcesses(lProcessIDs(1), lCB, lcbNeeded)
            Loop
            lNumElements = lcbNeeded / 4
            For i = 1 To lNumElements
                'Get a handle to the Process
                hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
                                       Or PROCESS_VM_READ, _
                                       0, _
                                       lProcessIDs(i))
                'Got a Process handle
                If hProcess Then
                    'Get an array of the module handles for the specified
                    'process
                    lRet = EnumProcessModules(hProcess, _
                                              lModules(1), _
                                              200, _
                                              lcbNeeded2)
                    'If the Module Array is retrieved, Get the ModuleFileName
                    If lRet Then
                        sModuleName = Space$(MAX_PATH)
                        nSize = 500
                        lRet = GetModuleFileNameExA(hProcess, _
                                                    lModules(1), _
                                                    sModuleName, _
                                                    nSize)
                        If LCase$(ParseFileName(Left$(sModuleName, lRet))) = _
                                                                sCompareText Then
                            AppIsRunning = True
                        End If
                    End If
                End If
                'Close the handle to the process
                lRet = CloseHandle(hProcess)
            Next
        Else
            hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
            If hSnap Then
                tProcEntry.dwSize = Len(tProcEntry)
                ' Iterate through the processes
                Do While Process32First(hSnap, tProcEntry)
                    sName = Left$(tProcEntry.szExeFile, Len(tProcEntry.szExeFile) - 1)
                    If LCase$(ParseFileName(sName)) = sCompareText Then
                        AppIsRunning = True
                    End If
                Loop
            End If
        End If
    End Function
    Private Function IsWinNT() As Boolean
        Dim osinfo As OSVERSIONINFO
        osinfo.dwOSVersionInfoSize = 148
        If GetVersionExA(osinfo) Then
            IsWinNT = (osinfo.dwPlatformId = 2)
        End If
    End Function
    Public Function ParseFileName(ByRef sPathAndFilename As String) As String
        ParseFileName = Mid$(sPathAndFilename, InStrRev(sPathAndFilename, "\") + 1)
    End Function
    

  • Next message: JD: "Using quotes in the command prompt"

    Relevant Pages

    • Re: Alpha search to load a list box
      ... Dim strTemp As String ... Private Sub LblAlpha_MouseDown(Button As Integer, Shift As Integer, X ... Dim StartX As Long, WidthX As Long ... Private Declare Function apiSelectObject Lib "gdi32" Alias ...
      (microsoft.public.access.formscoding)
    • Re: Alpha search to load a list box
      ... Dim strTemp As String ... Private Declare Function apiSelectObject Lib "gdi32" Alias ... Dim newfont As Long ' Handle to our Font Object we created. ...
      (microsoft.public.access.formscoding)
    • Re: CryptAPI
      ... > Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias ... > As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long ... > On Error GoTo ErrSign ...
      (microsoft.public.vb.winapi)
    • Re: Shellexecute in VB6
      ... Dim Serial_Port_Initialized As Boolean ... Private Declare Function OpenProcess Lib "kernel32" _ ... (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As ...
      (microsoft.public.vb.general.discussion)
    • ListView.SelectedItem cannot be modified
      ... Dim objFind As LV_FINDINFO ... Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd ... lpClassName As String, ByVal lpWindowName As String) As Long ... 'CompareDates: This is the sorting routine that gets passed to the ...
      (microsoft.public.vb.controls)