本帖最后由 yu2n 于 2013-1-29 09:34 编辑
Ps: 我觉得你应该试着修改别人的代码,一步步学习,而不是要现成的。
参考:- ' QQ 程序设定,自行修改(必须)
- Const pass="123456"
- Const QQAppName = "qq.exe"
- Const QQFullPath = "E:\AF071\Desktop\qq.exe"
-
-
- ' 全局变量
- Dim RunQQ, MePID
- RunQQ = False
- MePID = GetMePid()
-
-
- ' 程序初始化,取得参数
- If WScript.Arguments.Count = 0 Then
-
- If MeIsRunAgain() = True Then
- Call TipInfo( "程序访问控制", "正在退出监控程序……", 2 )
- Call MeClose()
- Else
- Call CloseApp(QQAppName, "")
- Call TipInfo( "程序访问控制", "正在启动监控程序……", 3 )
- Call Main()
- End If
-
- WScript.Quit
-
- Else
- Dim strArg, arrTmp
- For Each strArg In WScript.Arguments
- arrTmp = Split(strArg, "=")
- If UBound( arrTmp ) = 1 Then
- Select Case LCase( arrTmp(0) )
- Case "process_stop"
- Call process_stop( arrTmp(1) )
- Case Else
- WScript.Quit
- End Select
- End If
- Next
- WScript.Quit
- End If
-
-
-
- ' 主程序
- Sub Main()
- Do
- If (QQ_IsRun() = False) Then
- RunQQ = False
- Else
-
- If (RunQQ = False) Then
- Call QQ_Close()
- Call QAQ()
- End If
-
- End If
-
- WScript.Sleep 1000
- Loop
- End Sub
-
-
- ' 获取输入的密码
- Sub QAQ()
-
- InputPwd = Trim( InputBox("请输入密码:", "程序访问控制", "") )
-
- If InputPwd = pass then
-
- Call MeSubAppClose( MePID )
- Call TipInfo( "程序访问控制", "密码校验成功 !", 3 )
- Call QQ_Start()
- RunQQ = True
-
- Else
-
- Call ErrorInfo( "程序访问控制", "密码校验失败 !", 3 )
- RunQQ = False
-
- If Confirm( "程序访问控制 - 继续验证密码" ) = True Then Call QAQ()
-
- End If
-
- End Sub
-
-
- ' 启动QQ
- Sub QQ_Start()
-
- CreateObject("WScript.Shell").Run """" & QQFullPath & """", 1, False
-
- End Sub
-
-
- ' 检测 QQ 是否运行
- Function QQ_IsRun()
-
- QQ_IsRun = False
- If Not IsRun(QQAppName, "") = 0 Then QQ_IsRun = True
-
- End Function
-
-
- ' 关闭 QQ 程序
- Sub QQ_Close()
-
- CreateObject("WScript.Shell").Run """" & WScript.ScriptFullName & """ process_stop=" & QQAppName, 0, False
-
- End Sub
-
-
- ' 持续的关闭 QQ 程序
- Sub process_stop( byVal AppName )
-
- Do
- Call CloseApp(AppName, "")
- WScript.Sleep 1000
- Loop
-
- End Sub
-
-
-
- ' 提示信息
- Sub TipInfo( strTitle, strMsg, sTime )
- If Len(strMsg) < 22 Then strMsg = " " & strMsg & String(22 - 1 - Len(strMsg), " ")
- CreateObject("WScript.Shell").popup strMsg, sTime , strTitle, 64+4096 ' 提示信息
- End Sub
- Sub ErrorInfo( strTitle, strMsg, sTime )
- If Len(strMsg) < 22 Then strMsg = " " & strMsg & String(22 - 1 - Len(strMsg), " ")
- CreateObject("WScript.Shell").popup strMsg, sTime , strTitle, 16+4096 ' 提示信息
- End Sub
-
- ' 确认继续
- Function Confirm( ByVal strMsg )
- Confirm = False
- Set wso = CreateObject("WScript.Shell")
- If wso.Popup("确定要继续吗?" & String(17, " "), 5, strMsg, 48+4096+1) = 1 Then
- Confirm = True
- End If
- End Function
-
-
-
- ' 检测程序是否运行
- Function IsRun(byVal AppName, byVal AppPath) ' Eg: Call IsRun("mshta.exe", "c:\test.hta")
- IsRun = 0 : i = 0
- For Each ps in GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_
- IF LCase(ps.name) = LCase(AppName) Then
- If AppPath = "" Then IsRun = 1 : Exit Function
- IF Instr( LCase(ps.CommandLine) , LCase(AppPath) ) Then i = i + 1
- End IF
- Next
- IsRun = i
- End Function
-
-
- ' 关闭程序
- Sub CloseApp(byVal AppName, byVal AppPath) ' Eg: Call CloseApp("mshta.exe", "c:\test.hta")
- On Error Resume Next
- For Each ps in GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_
- IF LCase(ps.name) = LCase(AppName) Then
- If AppPath = "" Then
- ps.terminate
- Else
- IF Instr( LCase(ps.CommandLine) , LCase(AppPath) ) Then ps.terminate
- End If
- End IF
- Next
- On Error GoTo 0
- End Sub
-
-
- ' 获取自身PID
- Function GetMePid()
- For Each ps In Getobject("winmgmts:\\.").InstancesOf("Win32_Process")
- If ((LCase(ps.name) = LCase(Right(WScript.FullName, 11))) And _
- Instr(LCase(ps.CommandLine) , LCase(WScript.ScriptFullName))) Then
- GetMePid = ps.ProcessID
- Exit Function
- End If
- Next
- End Function
-
-
- ' 检测自身是否重复运行
- Function MeIsRunAgain()
- MeIsRunAgain = False
- Dim ps, i
- For Each ps in GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_
- If Lcase(ps.name) = LCase(Right(WScript.FullName,11)) Then
- If instr(Lcase(ps.CommandLine),Lcase(WScript.ScriptFullName)) Then i = i + 1
- End If
- Next
- If i > 2 Then
- MeIsRunAgain = True
- End If
- End Function
-
-
- ' 关闭自身
- Function MeClose()
- Dim MeAllPid
- Set pid = Getobject("winmgmts:\\.").InstancesOf("Win32_Process")
- For Each ps In pid
- If Lcase(ps.name) = LCase(Right(WScript.FullName,11)) Then
- If Instr(Lcase(ps.CommandLine),Lcase(WScript.ScriptFullName)) Then
- MeAllPid = MeAllPid & "/PID " & ps.ProcessID & " "
- End If
- End If
- Next
- Set pid = Nothing
- CreateObject("WScript.Shell").Run "TASKKILL " & MeAllPid & " /F /T", 0, False
- End Function
-
-
- ' 关闭子程序
- Function MeSubAppClose( ByVal MePID)
- Dim MeAllPid, i
- Set pid = Getobject("winmgmts:\\.").InstancesOf("Win32_Process")
- For Each ps In pid
- If Lcase(ps.name) = LCase(Right(WScript.FullName,11)) Then
- If Instr(Lcase(ps.CommandLine),Lcase(WScript.ScriptFullName)) Then
- If Not MePID = ps.ProcessID Then
- ps.terminate
- End If
- End If
- End If
- Next
- Set pid = Nothing
- End Function
复制代码
|