Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Dim Phwnd&, ChildHwnd&, Tmpstr$, Textlen&, Errcount%, OldStr$, ExePath$, Starttm&, aa$
Private Sub Form_Load()
Timer1.Interval = 100
ExePath = "c:\die.exe"
End Sub
Private Sub Timer1_Timer()
Phwnd = FindWindow(vbNullString, "死机测试")
If Phwnd <> 0 Then
ChildHwnd = FindWindowEx(Phwnd, 0, "ThunderRT6TextBox", vbNullString)
If ChildHwnd > 0 Then
aa = GetText(ChildHwnd)
If OldStr = aa Then
Errcount = Errcount + 1
If Errcount > 100 Then
Timer1.Enabled = False
'******************* 关掉它并停止计时器的监控
Call SendMessage(Phwnd, WM_CLOSE, 0, ByVal 0&)
Me.Caption = "死机了, 已被我关掉啦"
Starttm = Timer
Do
DoEvents
Loop Until Timer - Starttm >= 3
'******************* 暂停3秒后,再度开启c:\die.exe并重新启动计时器监控
Call Shell(ExePath, vbNormalFocus)
Timer1.Enabled = True
End If
Else
OldStr = aa: Errcount = 0
End If
Else
OldStr = "找不到检测窗口"
End If
Else
OldStr = "程序未运行"
End If
Me.Caption = OldStr
End Sub
Function GetText(Thwnd As Long) As String
Textlen = SendMessage(Thwnd, WM_GETTEXTLENGTH, 0, 0)
If Textlen = 0 Then GetText = "": Exit Function
Textlen = Textlen + 1
Tmpstr = Space(Textlen)
Textlen = SendMessage(Thwnd, WM_GETTEXT, Textlen, ByVal Tmpstr)
GetText = Left(Tmpstr, Textlen)
End Function
Private Sub Form_Load()
Me.Caption = "死机测试"
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
Text1.Text = Time$
End Sub
'****************************监视的代码
'添加 Timer1
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Dim Phwnd&, ChildHwnd&, Tmpstr$, Textlen&, Errcount%, OldStr$
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
Phwnd = FindWindow(vbNullString, "死机测试")
If Phwnd <> 0 Then
ChildHwnd = FindWindowEx(Phwnd, 0, "ThunderTextBox", vbNullString)
If ChildHwnd > 0 Then
If GetText(ChildHwnd) <> OldStr Then
Errcount = Errcount + 1
If Errcount > 100 Then MsgBox "死机了"
OldStr = GetText(ChildHwnd)
End If
Else
OldStr = "未运行"
End If
Else
OldStr = "未运行"
End If
Me.Caption = OldStr
End Sub
Function GetText(Thwnd As Long) As String
Textlen = SendMessage(Thwnd, WM_GETTEXTLENGTH, 0, 0)
If Textlen = 0 Then GetText = "": Exit Function
Textlen = Textlen + 1
Tmpstr = Space(Textlen)
Textlen = SendMessage(Thwnd, WM_GETTEXT, Textlen, ByVal Tmpstr)
GetText = Left(Tmpstr, Textlen)
End Function