|
-
Aug 25th, 2000, 07:45 AM
#1
Thread Starter
Lively Member
Hello,
How can I detect if a program is running ?
Which API used ?
Thanks,
Thierry
[email protected]
-
Aug 25th, 2000, 07:57 AM
#2
Use FindWindow.
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Command1_Click()
Dim hApp As Long
'Replace Calculator with the title of the App you want to check
hApp = FindWindow(vbNullString, "Calculator")
If hApp <> 0 Then
'App is running
Print "App is running"
Else
'App is not running
Print "App is not running"
End If
End Sub
-
Aug 25th, 2000, 08:01 AM
#3
Fanatic Member
you could use FindWindowEx with a string parameter of the window title, to see if it exists (you must get the string right though) if you aren't sure you can find it with another API Function (Someone help me out here...) whereby you can search for a window string that is "LIKE" the string you pass.
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
'example of finding the Microsoft Agent window Handle
glngWndAgent = FindWindowEx(0, 0, "AgentAnim", vbNullString)
If the function succeeds the return is the window handle of the app
If the function fails then the return is NULL
HTH
Crispin
VB6 ENT SP5
VB.NET
W2K ADV SVR SP3
WWW.BLOCKSOFT.CO.UK
[Microsoft Basic: 1976-2001, RIP]
-
Aug 25th, 2000, 09:50 AM
#4
If you want to use wildcards, you can use the EnumWindows function.
Code for a Module
Code:
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Length As Long
Dim Buffer As String
Dim Temp As String
Static iCount As Integer
iCount = iCount + 1
Length = GetWindowTextLength(hwnd) + 1
If Length > 1 Then
Buffer = Space(Length)
GetWindowText hwnd, Buffer, Length
Temp = Left(Buffer, Length - 1)
'Replace MyTitle with the title of the window you want to find
If Temp Like "*MyTitle*" Then MsgBox ("Window Found")
End If
EnumWindowsProc = 1
End Function
Code for a Form with a CommandButton
Code:
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, 0
End Sub
-
Aug 25th, 2000, 09:19 PM
#5
You can also use the find a window by it's class using the FindWindow api.
Code:
Private Sub Command1_Click()
'Find the Calculator by it's class
h = FindWindow("SciCalc", vbNullString)
If h <> 0 Then
Msgbox "App is running."
Else
Msgbox "App not found."
End If
End Sub
-
Aug 28th, 2000, 10:42 AM
#6
Likewise you can use WildCards to get a ClassName with EnumWindows.
Code for a Module
Code:
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Length As Long
Dim Buffer As String
Dim Temp As String
Dim sClass As String
Static iCount As Integer
sClass = Space(255)
Length = GetClassName(hwnd, sClass, 255)
'Look for anything that matches ?ciCa*
If Left(sClass, Length) Like "?ciCa*" Then MsgBox ("Calculator is open")
EnumWindowsProc = 1
End Function
Code for a Form with a CommandButton
Code:
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, 0
End Sub
-
Aug 28th, 2000, 07:03 PM
#7
You can also check to see if an app is running by knowing it's exe.
Code:
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Declare Function Process32First Lib "kernel32" ( _
ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32" ( _
ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function CloseHandle Lib "Kernel32.dll" _
(ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "Kernel32.dll" _
(ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" _
(ByRef lpidProcess As Long, ByVal cb As Long, _
ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
(ByVal hProcess As Long, ByVal hModule As Long, _
ByVal strModuleName As String, ByVal nSize As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" _
(ByVal hProcess As Long, ByRef lphModule As Long, _
ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Declare Function GetVersionExA Lib "kernel32" _
(lpVersionInformation As OSVERSIONINFO) As Integer
Public 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
Public Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long '1 = Windows 95.
'2 = Windows NT
szCSDVersion As String * 128
End Type
Public Const PROCESS_QUERY_INFORMATION = 1024
Public Const PROCESS_VM_READ = 16
Public Const MAX_PATH = 260
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SYNCHRONIZE = &H100000
'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
Public Const PROCESS_ALL_ACCESS = &H1F0FFF
Public Const TH32CS_SNAPPROCESS = &H2&
Public Const hNull = 0
Public Enum ePlatform
eWin95_98 = 1
eWinNT = 2
End Enum
Public gDBType As String
Public Function IsApplicationRunning(pEXEName As String) As Boolean
On Error Resume Next
Select Case getVersion()
Case eWin95_98
Dim lProc As Long, strName As String
Dim hSnap As Long, proc As PROCESSENTRY32
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If hSnap = hNull Then Exit Function
proc.dwSize = Len(proc)
' Iterate through the processes
lProc = Process32First(hSnap, proc)
Do While lProc
strName = StrZToStr(proc.szExeFile)
If InStr(UCase(strName), UCase(pEXEName)) Then
IsApplicationRunning = True
Exit Function
End If
lProc = Process32Next(hSnap, proc)
Loop
Case eWinNT
Dim cb As Long
Dim cbNeeded As Long
Dim NumElements As Long
Dim lProcessIDs() As Long
Dim cbNeeded2 As Long
Dim lNumElements2 As Long
Dim lModules(1 To 200) As Long
Dim lRet As Long
Dim strModuleName As String
Dim nSize As Long
Dim hProcess As Long
Dim i As Long
'Get the array containing the process id's for each process object
cb = 8
cbNeeded = 96
Do While cb <= cbNeeded
cb = cb * 2
ReDim lProcessIDs(cb / 4) As Long
lRet = EnumProcesses(lProcessIDs(1), cb, cbNeeded)
Loop
NumElements = cbNeeded / 4
For i = 1 To NumElements
'Get a handle to the Process
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
Or PROCESS_VM_READ, 0, lProcessIDs(i))
'Got a Process handle
If hProcess <> 0 Then
'Get an array of the module handles for the specified
'process
lRet = EnumProcessModules(hProcess, lModules(1), 200, _
cbNeeded2)
'If the Module Array is retrieved, Get the ModuleFileName
If lRet <> 0 Then
strModuleName = Space(MAX_PATH)
nSize = 500
lRet = GetModuleFileNameExA(hProcess, lModules(1), _
strModuleName, nSize)
strModuleName = Left(strModuleName, lRet)
'Check for the client application running
If InStr(UCase(strModuleName), UCase(pEXEName)) Then
IsApplicationRunning = True
Exit Function
End If
'List1.AddItem Left(strModuleName, lRet)
End If
End If
'Close the handle to the process
lRet = CloseHandle(hProcess)
Next
End Select
End Function
Function StrZToStr(pString As String) As String
StrZToStr = Left$(pString, Len(pString) - 1)
End Function
Public Function getVersion() As ePlatform
Dim osinfo As OSVERSIONINFO
Dim lRetVal As Integer
osinfo.dwOSVersionInfoSize = 148
osinfo.szCSDVersion = Space$(128)
lRetVal = GetVersionExA(osinfo)
getVersion = osinfo.dwPlatformId
End Function
Usage:
apprunning = IsApplicationRunning("C:\Program.exe")
If apprunning = True Then
MsgBox "Program is running"
Else
MsgBox "Program is not running"
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|