Solved

detecting executing of non vb program

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

Expert Comment

by:DrMaltz
Comment Utility
Jambyte,

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,

DrMaltz
0
 
LVL 2

Author Comment

by:Jambyte
Comment Utility
that way wasits to much processing.
0
 
LVL 1

Accepted Solution

by:
Toad224 earned 50 total points
Comment Utility
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

Private 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 Function FindApp(myName As String) As Boolean
    Const PROCESS_ALL_ACCESS = 0
    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)
    Loop
    Call CloseHandle(hSnapshot)
Finish:
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!
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
"System Wide Shell Hook Dll (Version 2.0)"
http://www.freevbcode.com/ShowCode.Asp?ID=1308


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

Expert Comment

by:DanRollins
Comment Utility
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
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now