Ckeck if a program is launched

Posted on 2005-04-11
Last Modified: 2010-04-17
Hello experts
I need to check from my VB project if the outlook express is launched and if it is not, launch it, I need to check this every x minutes, HOw can I do it?

Question by:diegohugo
    LVL 6

    Expert Comment


    You are going to have to make an API call.  One way of doing it would be this:

    In a code module paste the following code:

    Public Const TH32CS_SNAPPROCESS As Long = 2&
    Public Const MAX_PATH As Long = 260

    Public 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
    Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" _
       (ByVal lFlags As Long, ByVal lProcessID As Long) As Long

    Public Declare Function ProcessFirst Lib "kernel32" _
        Alias "Process32First" _
       (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

    Public Declare Function ProcessNext Lib "kernel32" _
        Alias "Process32Next" _
       (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

    Public Declare Sub CloseHandle Lib "kernel32" _
       (ByVal hPass As Long)

    Add a Timer control to your form and set it's Interval property to the appropriate time interval (in milliseconds) for the program to check if Outlook is running.  Paste the following code into your form:

    Private Sub Timer1_Timer()
      Dim hSnapShot As Long
      Dim uProcess As PROCESSENTRY32
      Dim success As Long
      Dim outlookRunning As Boolean

      hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)

      If hSnapShot = -1 Then Exit Sub

      uProcess.dwSize = Len(uProcess)
      success = ProcessFirst(hSnapShot, uProcess)

      If success = 1 Then
          If uProcess.szExeFile = "Outlook.exe" Then
            outlookRunning = True
        Loop While ProcessNext(hSnapShot, uProcess)
      End If

      Call CloseHandle(hSnapShot)
      If Not outlookRunning Then
        Shell "c:\program files\outlook\outlook.exe" 'Set this to the exact path of your Outlook.exe file
      End If
    End Sub

    That's it.  Run your program and it will continue to check if Outlook is running and if it is not then it will be started.

    This sample was adapted from

    Hope this helps.

    LVL 2

    Accepted Solution

    Add this code to your form:

    Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "USER32" () As Long

    Private Sub LauchOutlookExpressIfItIsNotRunning()
        Dim handle As Long
        handle = FindWindow("Outlook Express Browser Class", vbNullString)
        If handle = 0 Then
            ShellExecute GetDesktopWindow, "open", "msimn", vbNullString, vbNullString, 1
        End If
    End Sub

    Put a timer on your form and add call the LauchOutlookExpressIfItIsNotRunning() procedure from the timer tick event.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    withoutTen challenge 14 67
    Enhanced For loop vs ForEach Loop 4 41
    changePi Challenge 15 55
    wordcount challenge 11 42
    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    794 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

    16 Experts available now in Live!

    Get 1:1 Help Now