Solved

How can I check if a Process is running in Visual Basic 6

Posted on 2010-08-30
4
332 Views
Last Modified: 2012-05-10
Hi
I'd to know if there is a way to check a computers running Processes to see if a particular process is running, using VB6. (e.g. Within my Visual Basic project, check to see if Outlook.exe is running).
0
Comment
Question by:kalbarriman
  • 2
4 Comments
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 33564479
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 33564595
Check For Running Process
http://www.vbforums.com/showthread.php?p=2748682
Option Explicit



Private Declare Function OpenProcess Lib "kernel32" ( _

    ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

    

Private Declare Function CloseHandle Lib "kernel32" ( _

    ByVal hObject As Long) As Long



Private Declare Function EnumProcesses Lib "PSAPI.DLL" ( _

   lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long



Private Declare Function EnumProcessModules Lib "PSAPI.DLL" ( _

    ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long



Private Declare Function GetModuleBaseName Lib "PSAPI.DLL" Alias "GetModuleBaseNameA" ( _

    ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long



Private Const PROCESS_VM_READ = &H10

Private Const PROCESS_QUERY_INFORMATION = &H400



Private Function IsProcessRunning(ByVal sProcess As String) As Boolean

    Const MAX_PATH As Long = 260

    Dim lProcesses() As Long, lModules() As Long, N As Long, lRet As Long, hProcess As Long

    Dim sName As String

    

    sProcess = UCase$(sProcess)

    

    ReDim lProcesses(1023) As Long

    If EnumProcesses(lProcesses(0), 1024 * 4, lRet) Then

        For N = 0 To (lRet \ 4) - 1

            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcesses(N))

            If hProcess Then

                ReDim lModules(1023)

                If EnumProcessModules(hProcess, lModules(0), 1024 * 4, lRet) Then

                    sName = String$(MAX_PATH, vbNullChar)

                    GetModuleBaseName hProcess, lModules(0), sName, MAX_PATH

                    sName = Left$(sName, InStr(sName, vbNullChar) - 1)

                    If Len(sName) = Len(sProcess) Then

                        If sProcess = UCase$(sName) Then IsProcessRunning = True: Exit Function

                    End If

                End If

            End If

            CloseHandle hProcess

        Next N

    End If

End Function



Private Sub Command1_Click()

    Debug.Print IsProcessRunning("vb6.exe")

End Sub 

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33564599
0
 

Author Closing Comment

by:kalbarriman
ID: 33567552
Hi
The link provided was the solution (not the pasted code)
I modified the code from the original poster so that the variable for "Process" was a string containing the EXE name as below....
http://www.vbforums.com/showthread.php?p=2748682

Public Function isRunning(ByVal Process As String) As Boolean
    Dim objWMIService, colProcesses
    Set objWMIService = GetObject("winmgmts:")
   Process = "Call Reports.exe"
    Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process where name='" & Process & "'")
    If colProcesses.Count Then
    isRunning = True
    Else
    isRunning = False
    End If
End Function

Thank you very much
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now