Re: Check if a program (.exe) is currently running.
From: Peter Young (youngpa_at_attbi.no.com.spam.please)
Date: 03/02/04
- Previous message: Jeff Johnson [MVP: VB]: "Re: Check if process is running"
- In reply to: Mattias: "Check if a program (.exe) is currently running."
- Messages sorted by: [ date ] [ thread ]
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
- Previous message: Jeff Johnson [MVP: VB]: "Re: Check if process is running"
- In reply to: Mattias: "Check if a program (.exe) is currently running."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|