No More Solutions

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.

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"
MsgBox "Notepad is not opened"
End If

Bookmark and Share


Post a Comment