如何能知道某一應用程式是否被執行:

  如何能知道某一應用程式是否被執行?我們可以利用API函數-EnumWindow,由於這是回調函數,因此使用不當和導致死機!

模組程式碼:

Option Explicit
Private Declare Function EnumWindows Lib "user32" (ByVal Lpfn As Long, Lparam As Any) As Boolean
Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Function CallBackFunction(ByVal hWnd As Long, Lparam As Form) As Long
Dim StrhWnd As String * 8
 If IsWindowVisible(hWnd) Then
  StrhWnd = hWnd
  Lparam.EWin = Lparam.EWin & "識別字元" & GetWindowCaption(hWnd) & StrhWnd & vbCrLf
 End If
 CallBackFunction = True
End Function

Private Function GetWindowCaption(hWnd As Long) As String
Dim WordLength As Long
Dim WindowCaption As String * 255
 WordLength = GetWindowText(hWnd, WindowCaption, 255)
 GetWindowCaption = Left(WindowCaption, WordLength)
End Function

Public Sub CallbackDemo(frmName As Form)
 frmName.Cls
 EnumWindows AddressOf CallBackFunction, frmName
End Sub

表單程式碼:

Public EWin As String

Private Sub Command1_Click()
 CallbackDemo Me
 If InStr(EWin, vbCrLf & "識別字元" & Text1.Text) <> 0 Then
  MsgBox "正在運作中", vbInformation, "ENumWindow"
 Else
  MsgBox "找不到程式", vbInformation, "ENumWindow"
 End If
End Sub


上一頁