[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


detecting executing of non vb program

Posted on 2002-06-08
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

649 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