Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for program running and notify user

Status
Not open for further replies.

trium123

IS-IT--Management
Apr 8, 2005
15
GB
Hi All.
My goal is to create a windows app that checks for a program currently running(one that's minimised on the taskbar)and notify the user to log out of the program at a certain time (- lunchtime backup for example). I do not want to terminate the program since the user has to back out of the program manually, instead i would like to check if it is running and send them a reminder to close the program instead.
I have managed to get the messages done ok and at the moment the messages ask the user to check the process manually,but it would be great if i could get the checking done automatically. However i'd like some help with the "program running"checking process, Like where to start..!
Any help or advice would be gratefully received.
Thanks for your help and patience..
Regards
Ian.
 
Add the following to a module
Code:
'*****************************************
'    Windows API function declarations
'*****************************************
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
                                                ByVal wCmd As Long) As Long

Public Declare Function GetWindowText Lib "user32" _
                  Alias "GetWindowTextA" (ByVal hwnd As Long, _
                                          ByVal lpString As String, _
                                          ByVal cch As Long) As Long

Public Declare Function GetWindowTextLength Lib "user32" _
                  Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

'****************************************************
'    Declare constants used by GetWindow function
'****************************************************
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4
Const GW_CHILD = 5

Public Function TaskExists(AppSearchName As String) As Boolean

Dim lngCurrWnd As Long, lngWinTxtLen As Long, strListItem As String

If Len(AppSearchName) = 0 Then
   TaskExists = False
   Exit Function
End If

'****************************************************************
'    Get the hWnd of the first item in the master list
'    so we can process the task list entries (top-level only)
'****************************************************************
lngCurrWnd = GetWindow(frmMain.hwnd, GW_HWNDFIRST)

'**********************************************************
'    Loop while the hWnd returned by GetWindow is valid
'**********************************************************
Do While lngCurrWnd <> 0
   '************************************************************************
   '    Get the length of task name identified by lngCurrWnd in the list
   '************************************************************************
   lngWinTxtLen = GetWindowTextLength(lngCurrWnd)
   
   '**********************************************************************
   '    Get task name of the task identified by lngCurrWnd in the list
   '**********************************************************************
   strListItem = Space(lngWinTxtLen + 1)
   lngWinTxtLen = GetWindowText(lngCurrWnd, strListItem, lngWinTxtLen + 1)
   
   '*********************************************************************
   '    If AppSearchName matches task item, return True search result
   '*********************************************************************
   If InStr(UCase(strListItem), UCase(AppSearchName)) = 1 Then
      TaskExists = True
      Exit Do
   End If
   '******************************************************
   '    Get the next task list item in the master list
   '******************************************************
   lngCurrWnd = GetWindow(lngCurrWnd, GW_HWNDNEXT)
   
   '******************************
   '    Process Windows events
   '******************************
   DoEvents
Loop

End Function

Then in you checking code
Code:
If TaskExists("ApplicationToLookFor") Then
   MsgBox "ApplicationToLookFor Is Currently Running"
End If

Note that "ApplicationToLookFor" is the window caption of the app rather than the exe name.

Trevor
 
Thank you for your help. Blimey! All that code for such a simple task....
From what you have provided it looks like the code looks at the task manager list and identifies the program from there? I.E. notepad.exe?- however i'm getting two small errors, 1 DoEvents() is not declared and 2 FrmMain is not declared..
Any ideas?
Thanks very much again for your time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top