|
@@ -1,6 +1,7 @@
|
|
|
<?php
|
|
|
namespace DistributeHelper;
|
|
|
error_reporting(E_ALL & ~E_NOTICE);
|
|
|
+include_once "Utility.php";
|
|
|
|
|
|
class WscriptShell
|
|
|
{
|
|
@@ -14,6 +15,32 @@ class WscriptShell
|
|
|
logError('failed to creeate WScript.Shell',$e->getCode(),$e->getMessage());
|
|
|
}
|
|
|
}
|
|
|
+ /* style
|
|
|
+ 0 SW_HIDE
|
|
|
+ 1 SW_SHOWNORMAL
|
|
|
+ 2 SW_SHOWMINIMIZED
|
|
|
+ 3 SW_SHOWMAXIMIZED
|
|
|
+ 4 SW_SHOWNOACTIVATE
|
|
|
+ 5 SW_SHOW
|
|
|
+ 6 SW_MINIMIZE
|
|
|
+ 7 SW_SHOWMINNOACTIVE
|
|
|
+ 8 SW_SHOWNA
|
|
|
+ 9 SW_RESTORE
|
|
|
+ */
|
|
|
+ public static function Run($cmd,$style,$wait)
|
|
|
+ {
|
|
|
+ $shell = self::$shell;
|
|
|
+ if(! $shell){
|
|
|
+ logError('failed to Run. shell is null.');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ return $shell->Run($cmd,$style,$wait);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ logError('failed to Run',$cmd, $e->getCode(),$e->getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
public static function RegRead($key)
|
|
|
{
|
|
|
$shell = self::$shell;
|
|
@@ -100,6 +127,72 @@ class WscriptShell
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+//windows进程操作
|
|
|
+class WinProcess extends WscriptShell
|
|
|
+{
|
|
|
+ private static $tmp_dir;
|
|
|
+ public function __construct($tmp_dir)
|
|
|
+ {
|
|
|
+ WscriptShell::__construct();
|
|
|
+ self::$tmp_dir = $tmp_dir;
|
|
|
+ }
|
|
|
+ private static function pathJoin($base, $path) {
|
|
|
+ $base = str_replace('\\','/',$base);
|
|
|
+ $path = str_replace('\\','/',$path);
|
|
|
+ return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
|
|
|
+ }
|
|
|
+ public static function find($name){
|
|
|
+ $out_file = pathJoin(self::$tmp_dir,$name . '.txt');
|
|
|
+ if( file_exists($out_file) ){
|
|
|
+ unlink($out_file);
|
|
|
+ }
|
|
|
+ $cmd = "cmd.exe /C tasklist /FI \"IMAGENAME eq {$name}\" /FO list > {$out_file}";
|
|
|
+ if( self::Run($cmd,0,1) != 0){
|
|
|
+ logError('failed to Run',$cmd);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if( !file_exists($out_file) ){
|
|
|
+ logError('failed to find out file',$out_file);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $pids = array();
|
|
|
+ $fp = fopen($out_file,"r");
|
|
|
+ while($line = fgets($fp) ){
|
|
|
+ if( substr($line,0,4) == 'PID:'){
|
|
|
+ $pid = trim(substr($line,4));
|
|
|
+ array_push($pid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fclose($fp);
|
|
|
+ return $pids;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function remove($name)
|
|
|
+ {
|
|
|
+ //关闭进程
|
|
|
+ $cmd = "cmd.exe /C taskkill /F /IM {name}";
|
|
|
+ if( self::Run($cmd,0,1) != 0){
|
|
|
+ logError('failed to Run',$cmd);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //检测是否已经没有该任务进程
|
|
|
+ $pids = self::find($name);
|
|
|
+ if($pids === false){
|
|
|
+ logError('failed to execute find',$name);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if( count($pids) == 0){
|
|
|
+ logInfo('no process exists',$name);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ logInfo('process still exists after stop',$name);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
//windows系统是否有管理员权限,写入一个注册表键值测试
|
|
|
class WinAdminPriv extends WscriptShell
|
|
|
{
|