Solved

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

Posted on 2010-08-30
4
355 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

919 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

12 Experts available now in Live!

Get 1:1 Help Now