本帖最后由 yu2n 于 2015-11-24 17:15 编辑
获取自身PID By Yu2n- '获取自身PID By Yu2n
- Function MePid()
- Dim oPrc : MePid = 0
- With CreateObject("WScript.Shell").Exec("mshta.exe")
- For Each oPrc In GetObject("winmgmts:").ExecQuery( _
- "Select * From Win32_Process Where ProcessId=" & .ProcessID,,0)
- MePid = oPrc.ParentProcessId
- Next
- .Terminate()
- End With
- End Function
复制代码 检查 VBS 脚本是否重复执行- 'AppPrevInstance.vbs 检查 VBS 脚本是否重复执行 By Yu2n
- If AppPrevInstance() Then
- Msgbox "请不要重复运行!",vbSystemModal+vbCritical,">"
- WScript.Quit
- Else
- Msgbox "Hello World!",vbSystemModal+vbInformation,">"
- End If
-
- '检测程序是否重复运行(按 ScriptFullName、ScriptName 查询)
- Function AppPrevInstance()
- Dim sSQL, nCount
- sSQL = "Select * From Win32_Process Where (Name='cscript.exe' Or Name='wscript.exe') And CommandLine Like '%{P1}%'"
- nCount = GetObject("winmgmts:").ExecQuery(Replace(Replace(sSQL,"{P1}",WScript.ScriptFullName),"\","\\")).Count
- If nCount = 0 Then nCount = GetObject("winmgmts:").ExecQuery(Replace(sSQL,"{P1}",WScript.ScriptName)).Count
- AppPrevInstance = (nCount > 1)
- End Function
复制代码 '如果本脚本被重复执行,关闭之前重复执行脚本程序,只留下此脚本执行- 'KillReRun.vbs 关闭重复执行的脚本程序 By Yu2n
- '如果本脚本被重复执行,关闭之前重复执行脚本程序,只留下此脚本执行
- Msgbox "Hello World!",vbSystemModal+vbInformation,">"
- KillReRun
- Msgbox "已成功关闭重复运行的程序!",vbSystemModal+vbInformation,">"
-
- '如果本脚本被重复执行,关闭之前重复执行脚本程序,只留下此脚本执行
- Function KillReRun()
- Dim sSql, sSqlFF, sSqlFNX, oPrc, nMePid : nMePid = 0
- With CreateObject("WScript.Shell").Exec("mshta.exe")
- For Each oPrc In GetObject("winmgmts:").ExecQuery( _
- "Select * From Win32_Process Where ProcessId=" & .ProcessID,,0)
- nMePid = oPrc.ParentProcessId
- Next
- .Terminate()
- End With
- sSql = "Select * From Win32_Process Where (Name='cscript.exe' Or Name='wscript.exe') And CommandLine Like '%{P1}%'"
- sSqlFF = Replace(Replace(sSql,"{P1}",WScript.ScriptFullName),"\","\\") '按 ScriptFullName 查询
- sSqlFNX = Replace(sSql,"{P1}",WScript.ScriptName) '按 ScriptName 查询
- If GetObject("winmgmts:").ExecQuery(sSqlFF).Count > 0 Then
- sSql = sSqlFF
- Else
- sSql = sSqlFNX
- End If
- For Each oPrc In GetObject("winmgmts:").ExecQuery(sSql & " And ProcessId!=" & nMePid)
- oPrc.Terminate()
- Next
- Set oPrc = Nothing
- End Function
复制代码
|