?
Solved

Is an app running?

Posted on 1999-11-15
7
Medium Priority
?
183 Views
Last Modified: 2012-05-04
Is there a simple way to tell if a Windows application is currently running - all I know is part of the name and the executable that would have been invoked?  I am using VB6 w/ SP3.  Thanks in advance.
0
Comment
Question by:browndog
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 2209378
The API FindWindow would work is you know either the exact window's caption or the class name of the window.  Which application are you trying to find?
0
 
LVL 1

Author Comment

by:browndog
ID: 2209436
Trying to find out if Oracle's Discoverer 3.1 OLAP tool is running.  So FindWindow will do it if I know the exact caption?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2209459
Yes, if you know the exact caption of a normal window you can retrieve its handle.  e.g.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Command1_Click()
    Dim hWind As Long
    Dim sApp As String
    sApp = "Oracle's exact caption" '<-Exact caption of window
    hWind = FindWindow(vbNullString, sApp)
    If hWind <> 0 Then
        Debug.Print sApp & " is running"
    Else
        Debug.Print sApp & " is NOT running"
    End If
End Sub
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 3

Expert Comment

by:manojamin
ID: 2209580
If you know the name of the process, you can use this code to get the list of processes currently running on machine...

1) http://www.thescarms.com/vbasic/runningprocs.htm

2) http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4377

Use #2 with causion. Get rid of the kill code from there...

Hope this helps you...
0
 
LVL 14

Accepted Solution

by:
mcrider earned 400 total points
ID: 2209630
You don't need to know the exact title of an app to see if it's running...

This example shows how to find a running application...  It's a 16-bit example, but I've included the 32bit function here.

VB3 How to Get Window Handle Without Specifying Exact Title
http://support.microsoft.com/support/kb/articles/Q113/4/75.asp 

Cheers!


32-BIT CODE:

------------------------------------------------------------
ADD THIS TO A MODULE TO DEFINE FindWindowLike:
------------------------------------------------------------
Private Const GWL_ID = (-12)
Global ProgHandle As Long

Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5

Declare Function SetFocusAPI Lib "user32" Alias "SetForegroundWindow" _
    (ByVal hwnd As Long) As Long
       
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
    ByVal wCmd As Long) As Long
       
Private Declare Function GetWindowLW Lib "user32" Alias "GetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long) As Long
       
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
    (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
       
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Declare Function GetDesktopWindow Lib "user32" () As Long

Declare Function GetExitCodeProcess Lib "kernel32" _
    (ByVal hProcess As Long, lpExitCode As Long) As Long

Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long

Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

Declare Function SysSetFocus Lib "user32" Alias "SetFocus" _
    (ByVal hwnd As Long) As Long

Function FindWindowLike(hWndArray() As Variant, ByVal hWndStart As Long, WindowText As String, Classname As String, ID) As Long
    Dim hwnd As Long
    Dim r As Long
    ' Hold the level of recursion:
    Static level As Long
    ' Hold the number of matching windows:
    Static iFound As Long
    Dim sWindowText As String
    Dim sClassname As String
    Dim sID
    ' Initialize if necessary:
    If level = 0 Then
        iFound = 0
        ReDim hWndArray(0 To 0)
        If hWndStart = 0 Then hWndStart = GetDesktopWindow()
    End If
    ' Increase recursion counter:
    level = level + 1
    ' Get first child window:
    hwnd = GetWindow(hWndStart, GW_CHILD)
    Do Until hwnd = 0
        DoEvents ' Not necessary
        ' Search children by recursion:
        r = FindWindowLike(hWndArray(), hwnd, WindowText, Classname, ID)
        ' Get the window text and class name:
        sWindowText = Space(255)
        r = GetWindowText(hwnd, sWindowText, 255)
        sWindowText = Left(sWindowText, r)
        sClassname = Space(255)
        r = GetClassName(hwnd, sClassname, 255)
        sClassname = Left(sClassname, r)
        ' If window is a child get the ID:
        If GetParent(hwnd) <> 0 Then
            r = GetWindowLW(hwnd, GWL_ID)
            sID = CLng("&H" & Hex(r))
        Else
            sID = Null
        End If
        ' Check that window matches the search parameters:
        If sWindowText Like WindowText And sClassname Like Classname Then
            If IsNull(ID) Then
                ' If find a match, increment counter and
                '  add handle to array:
                iFound = iFound + 1
                ReDim Preserve hWndArray(0 To iFound)
                hWndArray(iFound) = hwnd
            ElseIf Not IsNull(sID) Then
                If CLng(sID) = CLng(ID) Then
                    ' If find a match increment counter and
                    '  add handle to array:
                    iFound = iFound + 1
                    ReDim Preserve hWndArray(0 To iFound)
                    hWndArray(iFound) = hwnd
                End If
            End If
        End If
        ' Get next child window:
        hwnd = GetWindow(hwnd, GW_HWNDNEXT)
    Loop
    ' Decrement recursion counter:
    level = level - 1
    ' Return the number of windows found:
    FindWindowLike = iFound
End Function

0
 
LVL 1

Author Comment

by:browndog
ID: 2211148
Thanks to all who offered answers.  I continue to think very highly of EE and you folks who are so open with your skills and time.  I'm accepting mcrider's answer as it allows a wildcard on the app name.

Browndog
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2211374
Thanks for the points!

Cheers!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question