?
Solved

Ckeck if a program is launched

Posted on 2005-04-11
2
Medium Priority
?
281 Views
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?

thanks
0
Comment
Question by:diegohugo
2 Comments
 
LVL 6

Expert Comment

by:TDSnet
ID: 13759800
Hi,

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
    Do
      If uProcess.szExeFile = "Outlook.exe" Then
        outlookRunning = True
      Else
    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 http://vbnet.mvps.org/index.html?code/system/toolhelpprocesses.htm

Hope this helps.

Cheers,
Jack
0
 
LVL 2

Accepted Solution

by:
kHSw earned 1000 total points
ID: 13759908
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.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

864 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