Notifications
Clear all

Saber se um aplicativo esta rodando

7 Posts
1 Usuários
0 Reactions
1,981 Visualizações
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 
'COLOQUE ESTA FUNÇÃO EM UM MÓDULO:
Option Explicit
Private Const TH32CS_SNAPPROCESS As Long = 2
Private Const MAX_PATH As Long = 260
Private 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
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, _
                                                                  ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, _
                                                        typProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, _
                                                       typProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)

Public Function AppIsRunning(ByVal AppName As String) As Boolean
    Dim Process As PROCESSENTRY32
    Dim hSnapShot As Long
    Dim r As Long
    AppName = LCase$(AppName)
    hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
    If hSnapShot <> -1 Then
        Process.dwSize = Len(Process)
        r = Process32First(hSnapShot, Process)
        Do While r
            If LCase$(Left$(Process.szExeFile, InStr(1, Process.szExeFile, vbNullChar) - 1)) = AppName Then
                AppIsRunning = True
                r = False
            End If
            r = Process32Next(hSnapShot, Process)
        Loop
        CloseHandle hSnapShot
    End If
End Function

Para testar basta usar a função AppIsRunning passando o nome do programa e sua extensão.

 
Postado : 06/11/2009 2:12 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Muiiiiiittooo Legal!!!

 
Postado : 27/01/2010 2:39 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Olá, não entendi muito bem... rs Desculpe-me...

Como posso usá-lo?

Abs!!

 
Postado : 02/02/2010 10:47 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Rodrigo, a dica é bacana, via API.
tenho uma sugestão de uma função mais simples via WMI (As novas apis avançadas do Windows)


Function ProcessIsRuning(strNomeProc As String) As Boolean
    Dim WMISrv As Object, colProcessList As Object
    'Instancia o serviço WMI
    Set WMISrv = GetObject("winmgmts:{impersonationLevel=impersonate}!\.rootcimv2")
    'Retorna a coleção de processos
    Set colProcessList = WMISrv.ExecQuery("Select * from Win32_Process Where Name = '" & strNomeProc & "'")
    'Verifica se o processo localizado esta aberto
    ProcessIsRuning = colProcessList.Count > 0
End Function

Sub TestaWMI()
    Debug.Print ProcessIsRuning("Excel.exe")
End Sub


 
Postado : 21/02/2010 7:38 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Boa essa em cara, é valido para todas as versões do windows? ou é do .net framework?

 
Postado : 21/02/2010 7:54 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Não é do .NET não, embora o .NET as usam para algumas varias tarefas.
esta presente na versoes do windows a partir do xp

 
Postado : 12/04/2010 5:40 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Benchmark para 300 execuções (no VBA):

IsAppRunning: 0,265625 seg
IsProcessRuning: 4,21875 seg

 
Postado : 10/05/2011 11:57 am