Results 1 to 6 of 6

Thread: Check if a program is running

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    188
    I want to check from one program to see if another program is running and if it is shut down that program. It's for an upgrade install. Any ideas?

  2. #2
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Isle of Man
    Posts
    276

    YES!

    first of all, i didn't come up with this myself.
    it's from kpdteam.tripod.com

    this looks for a window headed Calculator, and closes it

    you're gonna neeed to know what the form's caption is, though.....

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Const WM_CLOSE = &H10
    Private Sub Form_Load()
    Dim winHwnd As Long
    Dim RetVal As Long
    winHwnd = FindWindow(vbNullString, "Calculator")
    If winHwnd <> 0 Then
    PostMessage winHwnd, WM_CLOSE, 0&, 0&
    Else
    MsgBox "The Calculator is not open."
    End If
    End Sub


  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    partial name close window

    Part of title:
    This code uses EnumWindows to retrieve the title of all
    windows in the system, compares it with the partial name,
    and stops when a match is made. Code modified from a sample
    available at http://www.thescarms.com
    Code taken from a Q&A On Experts Exchange Posted By: Erick37


    '~~~~~~~MODULE CODE~~~~~~~~~~~~

    Option Explicit

    Public Const WM_CLOSE = &H10
    Public Const MAX_PATH = 260

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    ByVal lParam As Long) As Long
    Declare Function EnumWindows Lib "user32" _
    (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

    Private sAppTitle As String
    Public glHwnd As Long

    Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long
    Dim lResult As Long
    Dim sWndName As String

    fEnumWindowsCallBack = 1
    sWndName = Space$(MAX_PATH)

    lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
    sWndName = Left$(sWndName, lResult)
    'Search Title for our string
    If (InStr(1, sWndName, sAppTitle, vbTextCompare) > 0) Then
    Debug.Print sWndName
    glHwnd = hwnd
    fEnumWindowsCallBack = 0
    End If
    End Function

    Public Function SearchWindows(sApp As String, hwnd As Long) As Long
    sAppTitle = sApp
    glHwnd = 0
    Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
    SearchWindows = glHwnd
    End Function

    '~~~~~~~FORM CODE~~~~~~~~~~~~~~

    Option Explicit

    Private Sub Command1_Click()
    Dim sApp As String
    'Find notepad with partial name
    sApp = "notepa"
    glHwnd = SearchWindows(sApp, Me.hwnd)
    'End application if found
    If glHwnd > 0 Then
    PostMessage glHwnd, WM_CLOSE, 0&, 0&
    End If
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    188
    Thanx!

  5. #5
    Lively Member
    Join Date
    Jan 1999
    Location
    Burlington, IA, USA`
    Posts
    77
    Maybe you could use this.

    Detect if the application is already running.
    Sub Form_Load ()
    If App.PrevInstance Then
    MsgBox App.EXEName & " already running!", 4096, "Warning"
    End
    End If
    End Sub

    An ass may bray a good long time before he shakes the stars down.
    T.S. Elliot

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    If you know the caption name you could use

    Code:
    AppActivate "Calculator"
    SendKeys "%{F4}"
    If the calculator is open, this will close it.

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