detecting executing of non vb program

Posted on 2002-06-08
Last Modified: 2010-05-02
is there a way to have an event when the user runs a program such as IE?
Question by:Jambyte

Expert Comment

ID: 7064278

Here's a simple solution.. put a timer on a form.. when the timer code executes.. it uses the FindWindow API call to search for a particular application..

VB Declaration:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long

Win32API Definition:
The FindWindow function retrieves the handle of the top-level window whose
class name and window name match the specified strings. This function does
not search child windows.

For example, if I were looking for any notepad apps to start..  My code might look like this..

Private Sub Timer1_Timer()
Dim hwnd As Long

hwnd = FindWindow("Notepad", "")

If hwnd > 0 Then
    MsgBox "I found Notepad"
End If

End Sub

Good Luck,


Author Comment

ID: 7064416
that way wasits to much processing.

Accepted Solution

Toad224 earned 50 total points
ID: 7064423
The above code wouldn't work well if the Window class changed everytime the program was run, such as in default MFC VC++ applications or if you try to find a window with a caption that changes like IE.  A better way to find an application is by executable name:

Const MAX_PATH& = 260

Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

    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 Function FindApp(myName As String) As Boolean
    Dim uProcess As PROCESSENTRY32
    Dim rProcessFound As Long
    Dim hSnapshot As Long
    Dim szExename As String
    Dim exitCode As Long
    Dim myProcess As Long
    Dim AppKill As Boolean
    Dim appCount As Integer
    Dim i As Integer
    On Local Error GoTo Finish
    appCount = 0
    Const TH32CS_SNAPPROCESS As Long = 2&
    uProcess.dwSize = Len(uProcess)
    hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    rProcessFound = ProcessFirst(hSnapshot, uProcess)

    Do While rProcessFound
        i = InStr(1, uProcess.szexeFile, Chr(0))
        szExename = LCase$(Left$(uProcess.szexeFile, i - 1))

        If Right$(szExename, Len(myName)) = LCase$(myName) Then
            FindApp = True
            GoTo Finish
        End If

        rProcessFound = ProcessNext(hSnapshot, uProcess)
    Call CloseHandle(hSnapshot)
End Function

FindApp returns true if the process is running, false if it isn't...   ie.  MsgBox FindApp("calc.exe")  would show true if Calculator was running, and false if it wasn't.

If you're looking to do something whenever Internet Explorer is open, you could create a timer, and if FindApp("iexplore.exe") is true, then do something.

There are also other ways, such as to modify the "My Computer\HKEY_CLASSES_ROOT\exefile" registry key, or using the SetWindowsHookEx API, which isn't easy to use with VB.

Hope this helps.  Good luck!
LVL 28

Expert Comment

ID: 7065347
"System Wide Shell Hook Dll (Version 2.0)"

If all you want to trap is IE there is a simpler solution
LVL 49

Expert Comment

ID: 7873414
Hi Jambyte,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept DrMaltz's comment(s) as an answer.

Jambyte, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 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