CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 1999
    Posts
    40

    Is Program Running?

    Given a program name or file name, how can I detect if it is currently running on the system?

    Thanks...


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Is Program Running?

    Start a new project. Add a Module to it. Paste this code into the General declarations section of the Form.

    option Explicit

    private Sub Form_Load()
    Dim iRet as Long
    ' Check if THIS APP is currently running.
    ' Zero result says no, Non-Zero says Yes
    iRet = IsItRunning(App.Title) '
    MsgBox iRet
    ' is another app running?
    ' Passing another Application Title, Check if it is currently running.
    iRet = IsItRunning("Program Manager")
    MsgBox iRet
    End
    End Sub



    '
    ' Paste this code into the module

    option Explicit

    private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (byval lpClassName as string, byval lpWindowName as string) _
    as Long

    '*******************************************************************************
    ' IsItRunning (FUNCTION)
    '
    ' PARAMETERS:
    ' (In/Out) - who - string -
    '
    ' RETURN VALUE:
    ' Long - 0 if application is not running
    ' - hwnd of the application window
    ' DESCRIPTION:
    '
    '*******************************************************************************
    Function IsItRunning(who as string) as Long
    Dim OldTitle as string
    ' Save existing Title in case testing for myself
    OldTitle = App.Title
    App.Title = "Some Junk Title"
    'Attempt to get window handle
    IsItRunning = FindWindow(vbNullString, who)
    ' Restore Applications title
    App.Title = OldTitle

    End Function




    Run the program. It will look for itself then the "Program Manager"


    John G

  3. #3
    Join Date
    Nov 1999
    Posts
    40

    Re: Is Program Running?

    Will this also work for programs running in the system tray?


  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Is Program Running?

    Do not know why not. It's looking for the Programs title which should not be effected by where its running. ONe of the checks is for "Program Manager" which is an internal Windows app and has no windows at all.
    Here is another sample that will Enumerate all running processes. Check for yours.
    Start a new project, add a big text box to it, Add a Module.
    Paste the following into the Form

    option Explicit

    'Add this code to a form
    private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    'set the form's graphics mode to persistent
    me.AutoRedraw = true
    'call the Enumwindows-function
    Dim x
    EnumWindows AddressOf EnumWindowsProc, byval 0&
    End Sub
    '
    ' Add this code to the Module
    '
    option Explicit
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]

    'Add this code to a module
    Declare Function EnumWindows Lib "user32" (byval lpEnumFunc as Long, byval lParam as Long) as Boolean
    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
    public Function EnumWindowsProc(byval hwnd as Long, byval lParam as Long) as string
    Dim sSave as string, Ret as Long
    Ret = GetWindowTextLength(hwnd)
    sSave = Space(Ret)
    GetWindowText hwnd, sSave, Ret + 1
    If Trim(sSave) <> "" then _
    frmIsMeRunning.Text1 = frmIsMeRunning.Text1 & sSave & vbCrLf
    'continue enumeration
    EnumWindowsProc = true
    End Function




    '
    '
    Run the project

    John G

  5. #5
    Join Date
    Nov 1999
    Posts
    40

    Re: Is Program Running?

    Thanks


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

Featured