check if app. is running

Post new topic   Reply to topic

View previous topic View next topic Go down

check if app. is running

Post  ~Fleck on Sun Jun 28, 2009 10:55 am

to check if app. is running
Code:

Option Explicit

Private Const PROCESS_QUERY_INFORMATION As Long = (&H400)
Private Const STILL_ACTIVE              As Long = &H103

Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByRef lpExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Private Sub Form_Load()
    Dim lPid As Long

    lPid = Shell("notepad.exe", vbNormalFocus)
    Debug.Print IsAppRunning(lPid)
End Sub

Private Function IsAppRunning(ByVal lProgID As Long) As Boolean
    Dim lhProc  As Long
    Dim lRet    As Long

    lhProc = OpenProcess(PROCESS_QUERY_INFORMATION, False, lProgID)
    If Not lhProc = 0 Then
        Call GetExitCodeProcess(lhProc, lRet)
        Call CloseHandle(lhProc)
        IsAppRunning = (lRet = STILL_ACTIVE)
    End If
End Function

~Fleck
Moderator
Moderator

Posts: 274
Activity: -258
Reputation: 8
Join date: 2009-06-28
Age: 16
Location: 127.0.0.1

Back to top Go down

Re: check if app. is running

Post  ~Fleck on Tue Jun 30, 2009 2:22 pm

with mutex
Code:

'Megrem
Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (ByVal lpMutexAttributes As Long, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const ERROR_ALREADY_EXISTS = 183&
 
Public Function CheckMutex(strMutex As String) As Boolean
    Dim mutexvalue As Long
    mutexvalue = CreateMutex(ByVal 0&, 1, strMutex)
    If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
        CheckMutex = True
        CloseHandle mutexvalue
    Else
        CheckMutex = False
    End If
End Function

_________________

Thanks to FusioN for this awesome sig :]

~Fleck
Moderator
Moderator

Posts: 274
Activity: -258
Reputation: 8
Join date: 2009-06-28
Age: 16
Location: 127.0.0.1

Back to top Go down

View previous topic View next topic Back to top


Post new topic   Reply to topic
Permissions of this forum:
You cannot reply to topics in this forum