Friday, April 17, 2009

How to detect running program

Make a module and use the functionality of API to make it work.

Code in the module:
Option Explicit
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long

Private Declare Function EnumWindows Lib "user32.dll" _
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Private Target As String
Private IsOpen As Boolean

Private Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long
Dim hWndREC As Long
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length)
If InStr(1, title, Target, vbTextCompare) Then
IsOpen = True
End If
EnumCallback = 1
End Function

Public Function WindowOpen(app_name As String) As Boolean
'app_name is the name of the application when tracked on Windows Task Manager
IsOpen = False
Target = app_name
EnumWindows AddressOf EnumCallback, 0
If IsOpen Then WindowOpen = True
End Function


To check whether a program is running, e.g. Notepad:

If WindowOpen("Notepad") Then
MsgBox "Notepad is opened"
Else
MsgBox "Notepad is not opened"
End If

Share:

0 comments:

Post a Comment

You may be intersted in

Related Posts

Updating Table Containing Xml Column via LinkedServer

If you are trying to update a table containing XML column via Linked Server in SQL Server, and you are not able to, you are not alone. There...

About Me

My photo
Is an ordinary man, with a little knowledge to share and high dreams to achieve. I'd be glad if I can help others, 'coz the only thing for the triumph of evil is for a good man to do nothing.

About Blog

You can find a lot of debugging and deploying problems while developing applications in .NET and Visual Basic here. There are also some querying tips in SQL and typical source codes which might be useful shared here.

Popular Posts

Blogroll

Followers

Leave a Message