咔叽游戏

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 427|回复: 0

[vbs] VBS进程判断代码

[复制链接]
  • TA的每日心情
    无聊
    2019-6-2 14:11
  • 签到天数: 4 天

    [LV.2]圆转纯熟

    发表于 2020-4-17 05:44:09 | 显示全部楼层 |阅读模式
    vbs核心代码

    Option Explicit
    Dim objWMIService,colProcessList,strComputer
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'excel.exe'")
    If colProcessList.Count>0 Then
            MsgBox "检测到EXCEL程序运行中,程序退出!"
            WScript.Quit
    End If
    Set colProcessList = Nothing
    Set objWMIService = Nothing
    WScript.Quit当然你可以判断 winrar.exe等等
    下面附一个代码,原来中文命名的,咔叽网单www.2nzz.com已经修改为英文命名并且正常运行了,因为时间问题,需要的朋友可以自行修改精简

    '检测进程
    proname = "qq.exe"
    reName = IsProcess(proname)
    If reName = True Then
      msgbox "发现进程"
    ElseIf reName = False Then
      msgbox "没有发现进程"
    End If
    '检测进程 优化后的代码
    If IsProcess("qq.exe") = True Then
      msgbox "发现进程"
    Else
      msgbox "没有发现进程"
    End If
    '检测进程组
    proName_all = "qq.exe|notepad.exe"
    reName = IsProcessEx(proName_all)
    If reName = True Then
      msgbox "发现进程"
    ElseIf reName = False Then
      msgbox "没有发现进程"
    End If
    '检测进程组 优化后的代码
    If IsProcessEx("qq.exe|notepad.exe") = True Then
      msgbox "发现进程"
    Else
      msgbox "没有发现进程"
    End If
    '结束进程 前台执行
    proname = "qq.exe"
      Call CloseProcess(proname, 1)
    '结束进程 后台执行
    proname = "qq.exe"
      Call CloseProcess(proname, 0)
      '结束进程组 前台执行
    proName_all = "qq.exe|notepad.exe"
      Call CloseProcessEx(proName_all, 1)
    '结束进程组 后台执行
    proName_all = "qq.exe|notepad.exe"
      Call CloseProcessEx(proName_all, 0)
    '实例应用 结束进程 前台执行 10秒超时
    proname = "qq.exe"
    For i=1 to 10
      Call CloseProcess(proname,1)
      Delay 1000
      reName = IsProcess(proname)
      If reName = False Then
        Exit For
      End If
    Next
    If reName=True Then
      msgbox "结束进程失败"
    Else
      msgbox "结束进程成功"
    End If
    '实例应用 结束进程 前台执行 优化后的代码(直到型循环) 有些进程VBS检测不到 所以先关闭后检测
    Do
      Call CloseProcess("qq.exe",1)
      Delay 1000
    Loop While IsProcess("qq.exe")=True
    msgbox "结束进程成功"
    '实例应用 结束进程组 后台执行 10秒超时
    proName_all = "qq.exe|notepad.exe"
    For j=1 to 10
      Call CloseProcessEx(proName_all,0)
      Delay 1000
      reName = IsProcessEx(proName_all)
      If reName = False Then
        Exit For
      End If
    Next
    If reName=True Then
      msgbox "结束进程失败"
    Else
      msgbox "结束进程成功"
    End If
    '实例应用 结束进程组 后台执行 优化后的代码(直到型循环) 有些进程VBS检测不到 所以先关闭后检测
    Do
      Call CloseProcessEx( "qq.exe|notepad.exe",0)
      Delay 1000
    Loop While IsProcessEx( "qq.exe|notepad.exe")=True
    msgbox "结束进程成功"
    '函数 子程序部分代码
    '检测进程
    Function IsProcess(ExeName)
      Dim WMI, Obj, Objs,i
      IsProcess = False
      Set WMI = GetObject("WinMgmts:")
      Set Objs = WMI.InstancesOf("Win32_Process")
      For Each Obj In Objs
        If InStr(UCase(ExeName),UCase(Obj.Description)) <> 0 Then
          IsProcess = True
          Exit For
        End If
      Next
      Set Objs = Nothing
      Set WMI = Nothing
    End Function
    '结束进程
    Sub CloseProcess(ExeName,RunMode)
      dim ws
      Set ws = createobject("Wscript.Shell")
      ws.run "cmd.exe /C Taskkill /f /im " & ExeName,RunMode
      Set ws = Nothing
    End Sub
    '检测进程组
    Function IsProcessEx(ExeName)
      Dim WMI, Obj, Objs,ProcessName,i
      IsProcessEx = False
      Set WMI = GetObject("WinMgmts:")
      Set Objs = WMI.InstancesOf("Win32_Process")
      ProcessName=Split(ExeName,"|")
      For Each Obj In Objs
        For i=0 to UBound(ProcessName)
          If InStr(UCase(ProcessName(i)),UCase(Obj.Description)) <> 0 Then
            IsProcessEx = True
            Exit For
          End If
        Next
      Next
      Set Objs = Nothing
      Set WMI = Nothing
    End Function
    '结束进程组
    Sub CloseProcessEx(ExeName,RunMode)
      dim ws,ProcessName,CmdCode,i
      ProcessName = Split(ExeName, "|")
      For i=0 to UBound(ProcessName)
        CmdCode=CmdCode & " /im " & ProcessName(i)
      Next
      Set ws = createobject("Wscript.Shell")
      ws.run "cmd.exe /C Taskkill /f" & CmdCode,RunMode
      Set ws = Nothing
    End Sub好了这篇关于vbs进程判断的文章就介绍到这

    原文地址:https://www.jb51.net/article/175944.htm

    QQ|免责声明|小黑屋|手机版|Archiver|咔叽游戏

    GMT+8, 2024-3-19 15:26

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表