Results 1 to 6 of 6

Thread: Active application

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Location
    Easton, PA USA
    Posts
    3

    Post Didn't work. Help!

    Thanks for the prompt reply Serge.
    Unfortunately, I can't get it to work.
    I even went as far as creating a new project and
    included this code on the form load event (calculator program). No matter if it is open or not I get that the app is NOT running. I even included the full path and it still didn't work either way.
    Can you offer any suggestions?
    Thanks in advance

    Code:
    If IsApplicationRunning("calc.exe") Then
        MsgBox "Application is running."
    Else
        MsgBox "Application is not running."
    End If
    ---------------

    Of course. It's going to be kind of long, but here it is. Add a module to your project and copy this code:
    [code]....
    Then from the form (or any control) call the function like this:



    Just substitute MyEXE.exe with the valid EXE name.

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post If you are specifically checking if Calculator is running...

    Then you can use the AppActivate Statement:
    Code:
    Private Sub Command1_Click()
        On Error GoTo ErrorHandler
        AppActivate "Calculator"
        MsgBox "Calculator is running."
        End
        
        Exit Sub
    ErrorHandler:
        MsgBox "Calculator is NOT running."
        End
    End Sub
    All the best.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Location
    Easton, PA USA
    Posts
    3

    Post AppActivate

    Not specifically looking for calculator, just used that as
    a simple test.
    Would I need to put the files' path after AppActivate?

    ----------

    Then you can use the AppActivate Statement:
    Code:
    Private Sub Command1_Click()
        On Error GoTo ErrorHandler
        AppActivate "Calculator"
        MsgBox "Calculator is running."
        End
        
        Exit Sub
    ErrorHandler:
        MsgBox "Calculator is NOT running."
        End
    End Sub
    All the best.

  4. #4
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    DrChad,

    You don't have to include the App's path. AppActivate does a search based on the Window Titles of running Apps. From MSDN...

    Activates an application window.

    Syntax

    AppActivate title[, wait]

    The AppActivate statement syntax has thesenamed arguments:

    Part Description
    title Required.String expression specifying the title in the title bar of the application window you want to activate. The task ID returned by the Shell function can be used in place of title to activate an application.
    wait Optional.Boolean value specifying whether the calling application has the focus before activating another. If False (default), the specified application is immediately activated, even if the calling application does not have the focus. If True, the calling application waits until it has the focus, then activates the specified application.


    Remarks

    The AppActivate statement changes the focus to the named application or window but does not affect whether it is maximized or minimized. Focus moves from the activated application window when the user takes some action to change the focus or close the window. Use the Shell function to start an application and set the window style.

    In determining which application to activate, title is compared to the title string of each running application. If there is no exact match, any application whose title string begins with title is activated. If there is more than one instance of the application named by title, one instance is arbitrarily activated.
    All the best.

  5. #5
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Here is the modified (working) module code:
    Code:
    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
    Call it the same way.

    Code:
    If IsApplicationRunning("MyEXE.exe") Then
        MsgBox "Application is running."
    Else
        MsgBox "Application is not running."
    End If

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Location
    Easton, PA USA
    Posts
    3

    Post works now

    Thanks Serge, the new code works. I don't understand most of it, but it gets the job done.


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