如何用VB6.0编写一段程序,能实现某个程序运行结束后(例如QQ.EXE运行结束),电脑自动关机.谢谢,重酬.
在模块中加入如下代码:
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
'上面声明常数 和 API函数
Function exitproc(ByVal exefile As String) As Boolean
exitproc = False '先将该函数过程赋值为假
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&) '给系统进程创建快照 调用成功 则返回快照的句柄 调用失败 返回INVAID_HANDLE_VALUE
uProcess.dwSize = Len(uProcess)
r = Process32First(hSnapShot, uProcess) '初始遍历进程
Do While r
If Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0)) = exefile Then 'left截取null前的字符~也就是进程列表~再判断是否有qq.exe
exitproc = True '该函数过程为真 即有此进程
Exit Do '退出循环
End If
r = Process32Next(hSnapShot, uProcess) '遍历下一个进程
Loop
End Function
窗体代码如下 一个timer控件 一个按钮(可要可不要)
Private Sub Command1_Click()
Timer1.Enabled = False '停止监控
End Sub
Private Sub Form_Load()
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
If exitproc("QQ.exe") Then '检测QQ.exe进程是否存在
Else
x = MsgBox("15秒后关闭计算机", vbYesNo, "警告") '弹出对话框
Timer1.Enabled = False '先将timer控件暂停监视
If x = vbYes Then '如果点击对话框“是”按钮 则执行关机
Shell "cmd /c shutdown -s -t 15", 0
Else
Timer1.Enabled = True '如果点 ”否“则继续监控
End If
End If
End Sub