shenshihai преди 2 години
родител
ревизия
0fb24df27e
променени са 1 файла, в които са добавени 16 реда и са изтрити 8 реда
  1. 16 8
      src/WscriptShell.php

+ 16 - 8
src/WscriptShell.php

@@ -10,7 +10,7 @@ class WscriptShell
 		try{
 			if(self::$shell == null)
 				self::$shell = new \com("WScript.Shell");
-		}catch (Exception $e) {
+		}catch (\Exception $e) {
 			 logError('failed to creeate WScript.Shell',$e->getCode(),$e->getMessage());
 		}
 	}
@@ -23,7 +23,7 @@ class WscriptShell
 		}
 		try{
 			return $shell->RegRead($key);
-		} catch (Exception $e) {
+		} catch (\Exception $e) {
 			logError('failed to RegRead',$e->getCode(),$e->getMessage());
 			return false;
 		}
@@ -38,7 +38,7 @@ class WscriptShell
 		try{
 			$shell->RegWrite($key,$value,$type);
 			return true;
-		} catch (Exception $e) {
+		} catch (\Exception $e) {
 			logError('failed to RegWrite',$e->getCode(),$e->getMessage());
 			return false;
 		}
@@ -50,10 +50,18 @@ class WscriptShell
 			logError('failed to RegDelete. shell is null.');
 			return false;
 		}
+		//check if key exists
+		try{
+			$shell->RegRead($key);
+		} catch (\Exception $e) {
+			logInfo('reg key not exists. skip delete',$key);
+			return true;
+		}
+		//delete key
 		try{
 			$shell->RegDelete($key);
 			return true;
-		} catch (Exception $e) {
+		} catch (\Exception $e) {
 			logError('failed to RegDelete',$e->getCode(),$e->getMessage());
 			return false;
 		}
@@ -72,7 +80,7 @@ class WscriptShell
 			$link->Description = $description;
 			$link->Save();
 			return true;
-		} catch (Exception $e) {
+		} catch (\Exception $e) {
 			 logError('failed to createShortcut',$e->getCode(),$e->getMessage());
 			 return false;
 		}
@@ -86,7 +94,7 @@ class WscriptShell
 		}
 		try{
 			return $shell->SpecialFolders("Desktop");
-		} catch (Exception $e) {
+		} catch (\Exception $e) {
 			 logError('failed to getDesktopFolder',$e->getCode(),$e->getMessage());
 			 return false;
 		}
@@ -168,7 +176,7 @@ class WinAutorun extends WscriptShell
 			$key = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\' . $name;
 			$shell->RegDelete($key);
 			return true;
-		} catch (Exception $e) {
+		} catch (\Exception $e) {
 			 logError('failed to removeAutorun',$e->getCode(),$e->getMessage());
 			 return false;
 		}
@@ -192,7 +200,7 @@ class WinAutorun extends WscriptShell
 			$key = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\' . $name;
 			$shell->RegWrite($key,$cmd,'REG_EXPAND_SZ');
 			return true;
-		} catch (Exception $e) {
+		} catch (\Exception $e) {
 			 logError('failed to registerAutorun',$e->getCode(),$e->getMessage());
 			 return false;
 		}