Results 1 to 4 of 4

Thread: How can you tell if an app is open?

  1. #1
    Guest

    Question

    I am making an application that works with another one, is it possible to find out if the app it is working for is open?

    Thanks

  2. #2
    Addicted Member Razzle's Avatar
    Join Date
    Jan 2000
    Location
    Berlin, Germany
    Posts
    161
    There are two different ways:
    1. you look for the app's window using FindWindow
    2. you look for the running process

    1.
    Code:
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Function IsAppRunning(AppWindowName as String) as Boolean
    if FindWindow(AppWindowName, vbNullstring) <> 0 Then
     IsAppRunning = True
    Else
     IsAppRunning = False
    End if
    End Function
    2.
    Code:
    Public Const TH32CS_SNAPPROCESS As Long = 2&
    Public Const MAX_PATH As Integer = 260
    
    Public Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szExeFile As String * MAX_PATH
    End Type
        
    
    Public Declare Function CreateToolhelpSnapshot Lib "kernel32" _
        Alias "CreateToolhelp32Snapshot" _
       (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    
    Public Declare Function ProcessFirst Lib "kernel32" _
        Alias "Process32First" _
       (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    
    Public Declare Function ProcessNext Lib "kernel32" _
        Alias "Process32Next" _
       (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    
    Public Declare Sub CloseHandle Lib "kernel32" _
       (ByVal hPass As Long)
    
    Public RProcesses() As PROCESSENTRY32
    Public Const PROCESS_TERMINATE = &H1
    Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    
    
    Public Sub GetRunningProcesses()
    
        Dim hSnapShot As Long
        Dim uProcess As PROCESSENTRY32
        Dim r As Long
        Dim i&
        ReDim RProcesses(0)
        hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
       
        If hSnapShot = 0 Then Exit Sub
    
        uProcess.dwSize = Len(uProcess)
        r = ProcessFirst(hSnapShot, uProcess)
        i = 1
        Do While r
            ReDim Preserve RProcesses(UBound(RProcesses) + 1)
            RProcesses(i) = uProcess
            i = i + 1
            r = ProcessNext(hSnapShot, uProcess)
    
        Loop
    
        Call CloseHandle(hSnapShot)
    
    End Sub
    
    Function IsAppRunning(AppPath As String) As Boolean
    Dim PrName$
    GetRunningProcesses
    For i = 1 To UBound(RProcesses)
    PrName = Left(RProcesses(i).szExeFile, InStr(RProcesses(i).szExeFile, Chr(0)) - 1)
    If UCase$(PrName) = UCase$(AppPath) Then IsAppRunning = True: Exit Function
    Next i
    IsAppRunning = False
    End Function
    [Edited by Razzle on 05-07-2000 at 09:50 AM]
    Razzle
    ICQ#: 31429438
    What is the difference between a raven?
    -The legs. The length is equal, especially the right one.

  3. #3
    Guest
    Thanks. I think #1 will do me fine. #2 is a bit too long.

  4. #4
    Addicted Member Razzle's Avatar
    Join Date
    Jan 2000
    Location
    Berlin, Germany
    Posts
    161
    yep, it's pretty long but it's also handy if you want to terminate the app or if you want to be sure about the exe name of the running app
    Razzle
    ICQ#: 31429438
    What is the difference between a raven?
    -The legs. The length is equal, especially the right one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width