|
-
Jan 17th, 2001, 05:38 AM
#1
Thread Starter
Addicted Member
Hi all,
I would like to know if there is a way to check if an exe is running before I shell out to run an instance of it?
Would anyone have any advice on doing this...
Rgds,
Rocks
-
Jan 17th, 2001, 05:47 AM
#2
PowerPoster
Rocks, here the way that I've done it in all my pass project.
Code:
'Put this code under a Basic Module and
'Assume the frmMain is your application main form
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private xhwnd As Long
Public Sub Main()
Dim xAppName As String
If App.PrevInstance Then
xAppName = "Your Application Windows Title"
xhwnd = FindWindow(0&, xAppName)
If xhwnd <> 0 Then
AppActivate xAppName
xhwnd = ShowWindow(xhwnd, SW_SHOWNORMAL)
End
End If
End If
Load frmMain
frmMain.Show
End Sub
-
Jan 17th, 2001, 07:33 AM
#3
Try this:
Code:
'Author: Serge
'Origin: http://forums.vb-world.net/showthrea...threadid=16450
'Purpose: Determine if an app is running
'Version: VB5+
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
Private Sub Command1_Click()
If IsApplicationRunning("C:\program.exe") Then
MsgBox "Application is running."
Else
MsgBox "Application is not running."
End If
End Sub
-
Jan 26th, 2001, 01:34 AM
#4
Frenzied Member
For some reason, I can get a true response for every program that is running except America Online.
The code won't recognize AOL as running.
Does anybody know why?
~AOL sucks!
-
Jan 26th, 2001, 03:36 AM
#5
Thread Starter
Addicted Member
Same here
Hi,
I fiund the code great for testing Win Apps but it does'nt seem to be picking up the app that I wanted to test.........
If you are shelling to the app from code and want to check to see if it is running / finished look into win api Create Process......
I am going take a shot at it today...will let you know how it goes.....
if you have any other ideas let me know...
ciao
-
Jan 26th, 2001, 07:20 AM
#6
Just use the FindWindow API function to find AOL.
Code:
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As Long, ByVal _
lpWindowName As String) As Long
Usage
Private Sub Command1_Click()
Dim aolframe As Long
aolframe = FindWindow("AOL frame25", vbNullString)
If aolframe <> 0 Then
Msgbox "AOL is Open"
Else
Msgbox "No AOL detected"
End If
End Sub
-
Jan 26th, 2001, 10:00 AM
#7
Thread Starter
Addicted Member
what about....
Matthew,
You defined the process as "AOL frame25"
I need to check if a different process is running would I just use the name that appears in the Task Mgr. Process Tab???
Thanks,
-
Jan 26th, 2001, 11:09 AM
#8
Frenzied Member
It works man! You had one error though.
The function takes both params as strings instead of
the first one being a long.
Code:
Public Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, ByVal _
lpWindowName As String) As Long
I don't understand the "AOL frame25" either.
The title in taskman is "America Online"
What does that mean?
-
Jan 26th, 2001, 11:24 AM
#9
aol frame25 is the class of AOL.
America Online is the title.
The FindWindow API function can be used to find a window either by it's class, caption, or both.
Code:
FindWindow("AOL frame25", "America Online")
But since AOL's title is not always America Online (when you view IE in AOL, the caption changes), so you have to find it by it's class. The class will always be the same.
Hope that clears things up a bit.
And to see if AOL is running, using the code above, did you state the correct path that AOL is in?
Something like:
Code:
If IsApplicationRunning("C:\America Online 6.0\waol.exe") Then
Msgbox "AOL is running"
Else
Msgbox "AOL is not running"
End If
-
Jan 26th, 2001, 03:10 PM
#10
Frenzied Member
Ah, sneaky, sneaky, AOL.
I was looking for aol.exe which is in the same folder as
waol.exe but waol.exe is hidden.
-
Jan 26th, 2001, 03:20 PM
#11
Yeah, WAOL.exe is the REAL thing.
AOL.exe is like a shortcut to WAOL.exe.
Everything working now?
-
Jan 26th, 2001, 04:34 PM
#12
Lively Member
????
q:
How did you get het "AOL frame25" ????
how did you known ??
-
Jan 26th, 2001, 04:54 PM
#13
I've used AOL for so long, used to do some AOL programming, it just came natural .
But if you want to get it, you can use the GetClassName API function.
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
|