如果进程的路径中包含特定的文件夹名称,则如何终止进程



我从PowerBuilder运行一个应用程序,并在不需要它时终止它。但是应用程序在运行时,它会从特定文件夹执行其他进程。

如果所有进程从特定文件夹运行,我想杀死它们。

如何获取路径中具有指定文件夹名称的所有进程的进程句柄?

这里有一个示例来展示如何终止记事本的所有正在运行的实例.exe

OleObject wsh
integer  li_rc
wsh = CREATE OleObject
wsh.ConnectToNewObject( "MSScriptControl.ScriptControl" )
wsh.language = "vbscript"
wsh.AddCode('function terminatenotepad() ~n ' + &
 'strComputer = "." ~n ' + &
 'Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2") ~n ' + &
 'Set colItems = objWMIService.ExecQuery("Select * from Win32_Process where name = ~'notepad.exe~'") ~n ' + &
 'For Each objItem in colItems ~n ' + &
 '     objItem.Terminate ~n ' + &
 'Next ~n ' + &
 'end function ~n ' )
wsh.executestatement('terminatenotepad()')
wsh.DisconnectObject()
DESTROY wsh

尝试更改 where 子句

where name = ~'notepad.exe~'" ...

where commandline = ~'" + ls_commandline + "~'"  ...

其中ls_commandline是用于启动进程的命令行。

有关详细信息,请参阅"Win32_Process类"。

/// This is simple code example Terminate a Running Process if here is specific
/// folder name in the path of the running program (EagleGet)
/// Two variable SelectThis and TheWhere can be changed to get different 
/// results. Many things are not  dynamic in this script and certainly not the
/// best way of writing code. but its just ok for a little quick work.`
OleObject wsh
Integer  li_rc
String LineFeed = ' ~r~n ' 
String TheComputer = "." //local computer
String TheCode = "no need to set it here"
String FunctionName = "Whatever()" //any name you like
String SelectThis = "?" //only the columns/expressions
String TheWhere = "?" //only the where clause without keyword WHERE
String DoWhat = "?" //the action to perform for example 'Terminate' without quotes
String TheQuery = "no need to set here"
String WMIClass = "Win32_Process" /// The WMI class of running processes
String TheFolderName = "The folder name from which the creapy process is running (path) " 
/// You just set DoWhat, SelectThis and TheWhere. Rest of the variables, you dont need to set here
/// SelectThis = is the columns or expressions you want returned by the Query
SelectThis = "*"
/// TheFolderName = set it to the name of the folder that exist in the path 
///of the ruuning process you want to terminate
TheFolderName = "EagleGet"                                      
/// TheWhere is the WHERE clause expressions
TheWhere = "ExecutablePath  LIKE ~'%\" + TheFolderName + "\%~' "
/// DoWhat is the final function call of the WMI Class
DoWhat = "Terminate"
/// There is no need to chage anything from this point onward.
/// without double quotes, and you dont have to change TheQuery here 
TheQuery = " SELECT " + SelectThis + " FROM " + WMIClass + " WHERE " + TheWhere
TheCode = "Function " + FunctionName + LineFeed + &
"strComputer = ~"" + TheComputer  + "~"" + LineFeed + &
"Set objWMIService = GetObject(~"winmgmts:\~" & strComputer & ~"rootcimv2~")" + LineFeed + &
"Set colItems = objWMIService.ExecQuery(~"" + Trim(TheQuery) + "~" )" + LineFeed + &
   "For Each objItem in colItems" + LineFeed + &
      "objItem." + Trim(DoWhat) + LineFeed + &
   "Next " + LineFeed + &
"END Function " + LineFeed 
wsh = CREATE OleObject
wsh.ConnectToNewObject("MSScriptControl.ScriptControl")
wsh.Language = "VBScript"
wsh.AddCode(TheCode)
TRY
    wsh.ExecuteStatement(FunctionName)
CATCH (RunTimeError Re01)
    MessageBox("Query Error", "Following code has some problems.~r~n~r~n" + TheCode, StopSign!)
END TRY
wsh.DisconnectObject()
DESTROY wsh

相关内容

  • 没有找到相关文章

最新更新