detecting executing of non vb program

is there a way to have an event when the user runs a program such as IE?
Who is Participating?
Toad224Connect With a Mentor Commented:
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!

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,

JambyteAuthor Commented:
that way wasits to much processing.
"System Wide Shell Hook Dll (Version 2.0)"

If all you want to trap is IE there is a simpler solution
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.