3

How to check if a program is running now or not by its title ? (using vb6)

Example :

'check if there is a program contain a "Notepad" in its title

if (does "Notepad" running now ?) then 

end if

alt text

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
faressoft
  • 19,053
  • 44
  • 104
  • 146

4 Answers4

7
''# preparation (in a separate module)
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Function FindWindowHandle(Caption As String) As Long
  FindWindowHandle = FindWindow(vbNullString, Caption)
End Function

''# use (anywhere)
MsgBox FindWindowHandle("Untitled - Notepad")

Code above basically taken from here.

The exact window caption must be known for this. The function will return <> 0 if a window with the given caption was found, 0 otherwise.

To find a window whose caption contains a certain string, you will need to enumerate all windows and look for the correct one yourself. This process is a little more complicated, but explained in great detail here: everythingaccess.com - Bring an external application window to the foreground.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • but I want to check if there is a program contain "Notepad" in its title because I don't know what is the document name(Untitled or anything else) – faressoft Jan 03 '11 at 16:31
  • @faressoft: That's why I included the link to a blog post that explains how this can be done. – Tomalak Jan 03 '11 at 16:37
3

Karl Peterson has some great code for this on his excellent VB6 website. It uses EnumWindows like Tomalak's answer (in the link)

MarkJ
  • 30,070
  • 5
  • 68
  • 111
1

Pass in the handle of your app and a partial caption. It will return true if found.

Public Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean

Dim lhWndP            As Long
Dim sStr              As String

GetHandleFromPartialCaption = False
lhWndP = FindWindow(vbNullString, vbNullString)                                      'PARENT WINDOW

Do While lhWndP <> 0
    sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
    GetWindowText lhWndP, sStr, Len(sStr)
    sStr = Left$(sStr, Len(sStr) - 1)
    If InStr(1, sStr, sCaption) > 0 Then
        GetHandleFromPartialCaption = True
        lWnd = lhWndP
        Exit Do
    End If
    lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
Loop

End Function

Cidtek
  • 324
  • 1
  • 8
-3

Now right click on taskbar and click task manager. Now show your running program.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
raja
  • 11