This is excellent!
I've been looking for a way to check whether a specific app is running or not, and act accordingly.
This code works fine - slightly adapted, using it as a Boolean Function
thanks..!![]()
[/QUOTE]VB Code:
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, lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Const TH32CS_SNAPPROCESS As Long = 2& Private Const MAX_PATH As Integer = 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 Sub Form_Load() Dim lLng As Long, lA As Long, lExCode As Long Dim procObj As PROCESSENTRY32 Dim hSnap As Long Dim lRet As Long Dim sExeNam As String sExeNam$ = "YourProgramName.exe" hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) 'create a snapshot of the system process information procObj.dwSize = Len(procObj) lRet = Process32First(hSnap, procObj) 'Query information on the top-most running process Do While Process32Next(hSnap, procObj) 'loop through all the processes If InStr(1, LCase(procObj.szExeFile), LCase(sExeNam$)) > 0 Then 'Your exe name has been found lLng = OpenProcess(&H1, ByVal 0&, procObj.th32ProcessID) 'Open the process as to get its handle lA = TerminateProcess(lLng, lExCode) 'Terminate the process Exit Do End If Loop End Sub




Reply With Quote