You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

817 lines
32 KiB

6 years ago
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Security.AccessControl;
  7. using System.Security.Principal;
  8. using Microsoft.Win32;
  9. using Microsoft.Win32.Security;
  10. namespace install_wim_tweak
  11. {
  12. class Program
  13. {
  14. const string HIVE_MOUNT_DIR = "windows6_x_software";
  15. static string _pkgDirectory = HIVE_MOUNT_DIR + "\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\";
  16. const string HIVE_MOUNT_POINT = "HKLM\\" + HIVE_MOUNT_DIR;
  17. const string REGISTRY_PATH = "Windows\\system32\\config\\SOFTWARE";
  18. static readonly string ProgramHeader =
  19. "-------------------------------------------\n" +
  20. "--------Registry Tweak Tool v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version + "-------\n" +
  21. "---------------for Windows 6.x-------------\n" +
  22. "---------Created by Michał Wnuowski--------\n" +
  23. "-----Concept by Aviv00@msfn / lite8@MDL----\n" +
  24. "-----------Modified by Legolash2o----------\n" +
  25. "-------------------------------------------\n\n";
  26. const String PROGRAM_HELP_INFO =
  27. "USAGE : \n" +
  28. " install_wim_tweak [/p <Path>] [/c <PackageName> (optional)] [/?]\n\n" +
  29. "REMARKS : \n" +
  30. " /p<Path> Use '/p' switch to provide path to mounted install.wim\n" +
  31. " /o Use '/o' to run on current Windows\n" +
  32. " /c <ComponentName> Use '/c' to show a specific package\n" +
  33. " /? Use '/?' switch to display this info\n" +
  34. " /l Outputs all packages to \"Packages.txt\"\n" +
  35. "EXAMPLE : \n" +
  36. " install_wim_tweak /p C:\\temp files\\mount\n" +
  37. " install_wim_tweak /c Microsoft-Hyper-V-Common-Drivers-Package";
  38. static bool _failed;
  39. static string _bkpFile;
  40. static string _hiveFileInfo;
  41. static string _comp = "";
  42. static Dictionary<char, string> _cmdLineArgs;
  43. static bool _online;
  44. static readonly string PackLog = Environment.CurrentDirectory + "\\Packages.txt";
  45. static string _cr = "";
  46. static bool _vis;
  47. //static Sid SysUser = new Sid();
  48. static void Main(string[] args)
  49. {
  50. Console.ForegroundColor = ConsoleColor.White;
  51. Console.Write(ProgramHeader);
  52. Console.ResetColor();
  53. try
  54. {
  55. _cmdLineArgs = ProcessCmdArgs(args, new char[] { 'p', '?', 'c', 'o', 'l', 'r', 'n', 'h', 'd' });
  56. if (_cmdLineArgs.ContainsKey('?'))
  57. {
  58. Console.Write(PROGRAM_HELP_INFO);
  59. Console.ForegroundColor = ConsoleColor.Cyan;
  60. Console.Write("\nPlease make sure you use lowercase for the /p, /c, /o and /l");
  61. Console.ResetColor();
  62. Environment.Exit(1);
  63. }
  64. if (_cmdLineArgs.ContainsKey('c'))
  65. {
  66. if (!string.IsNullOrEmpty(_cmdLineArgs['c']))
  67. {
  68. _comp = Path.Combine(_cmdLineArgs['c'], "");
  69. }
  70. else
  71. {
  72. Console.ForegroundColor = ConsoleColor.White;
  73. Console.WriteLine("Type the name of the package, if nothing is entered all packages will be made visible :");
  74. Console.ForegroundColor = ConsoleColor.Cyan;
  75. _comp = Path.Combine(Console.ReadLine(), "");
  76. }
  77. Console.ResetColor();
  78. }
  79. if (_cmdLineArgs.ContainsKey('o'))
  80. {
  81. _hiveFileInfo = Path.Combine(System.IO.Path.GetPathRoot(Environment.SystemDirectory), REGISTRY_PATH);
  82. Console.ForegroundColor = ConsoleColor.Cyan;
  83. Console.WriteLine("MountPath : Online");
  84. Console.ResetColor();
  85. _pkgDirectory = _pkgDirectory.Replace("windows6_x_software", "Software");
  86. _online = true;
  87. }
  88. if (_cmdLineArgs.ContainsKey('h'))
  89. {
  90. _vis = true;
  91. }
  92. if (!_cmdLineArgs.ContainsKey('o'))
  93. {
  94. if (!_cmdLineArgs.ContainsKey('p'))
  95. {
  96. Console.ForegroundColor = ConsoleColor.White;
  97. Console.WriteLine("Type path to mounted install.wim :");
  98. Console.ForegroundColor = ConsoleColor.Cyan;
  99. _hiveFileInfo = Path.Combine(Console.ReadLine(), REGISTRY_PATH);
  100. if (_hiveFileInfo.Substring(0, _hiveFileInfo.Length - REGISTRY_PATH.Length).Length == 3)
  101. {
  102. Console.WriteLine("MountPath : Online");
  103. _pkgDirectory = _pkgDirectory.Replace("windows6_x_software", "Software");
  104. _online = true;
  105. }
  106. else
  107. {
  108. Console.WriteLine("MountPath : {0}", "\"" + _hiveFileInfo.Substring(0, _hiveFileInfo.Length - REGISTRY_PATH.Length) + "\"");
  109. _online = false;
  110. }
  111. Console.ResetColor();
  112. }
  113. else
  114. {
  115. _hiveFileInfo = Path.Combine(_cmdLineArgs['p'], REGISTRY_PATH);
  116. Console.ForegroundColor = ConsoleColor.Cyan;
  117. if (_cmdLineArgs['p'].Length == 3)
  118. {
  119. Console.WriteLine("MountPath : Online");
  120. _pkgDirectory = _pkgDirectory.Replace("windows6_x_software", "Software");
  121. _online = true;
  122. }
  123. else
  124. {
  125. Console.WriteLine("MountPath : {0}", "\"" + _cmdLineArgs['p'] + "\"");
  126. _online = false;
  127. }
  128. Console.ResetColor();
  129. }
  130. }
  131. if (string.IsNullOrEmpty(_hiveFileInfo))
  132. Environment.Exit(-2);
  133. if (!File.Exists(_hiveFileInfo))
  134. {
  135. Console.ForegroundColor = ConsoleColor.Red;
  136. Console.WriteLine("Registry file not found, please make sure your mount path is correct!");
  137. Console.ResetColor();
  138. _failed = true;
  139. Environment.Exit(-532459699);
  140. }
  141. if (!string.IsNullOrEmpty(_comp))
  142. {
  143. string T = _comp;
  144. while (T.Contains("~"))
  145. {
  146. T = T.Substring(0, T.Length - 1);
  147. }
  148. Console.ForegroundColor = ConsoleColor.Cyan;
  149. Console.WriteLine("Component : " + "\"" + T + "\"");
  150. Console.ResetColor();
  151. }
  152. Console.ForegroundColor = ConsoleColor.White;
  153. Console.WriteLine("\n------------------Starting-----------------");
  154. Console.ResetColor();
  155. if (_online == false)
  156. {
  157. if (!_cmdLineArgs.ContainsKey('l') && !_cmdLineArgs.ContainsKey('n'))
  158. {
  159. Console.Write("Creating BKP of registry file... ");
  160. _bkpFile = Path.Combine(Environment.CurrentDirectory, "SOFTWAREBKP");
  161. if (!File.Exists(_bkpFile))
  162. {
  163. File.Copy(_hiveFileInfo, _bkpFile, true);
  164. }
  165. Console.ForegroundColor = ConsoleColor.Green;
  166. Console.WriteLine("OK");
  167. Console.ResetColor();
  168. }
  169. Console.Write("Mounting registry file... ");
  170. if (!Contains<string[], string>(Registry.LocalMachine.GetSubKeyNames(), HIVE_MOUNT_DIR))
  171. {
  172. if (!LoadHive(_hiveFileInfo, HIVE_MOUNT_POINT))
  173. {
  174. Console.ForegroundColor = ConsoleColor.Red;
  175. Console.WriteLine("FAIL");
  176. Console.ResetColor();
  177. _failed = true;
  178. Ending();
  179. }
  180. }
  181. Console.ForegroundColor = ConsoleColor.Green;
  182. Console.WriteLine("OK");
  183. Console.ResetColor();
  184. }
  185. if (_cmdLineArgs.ContainsKey('l'))
  186. {
  187. Console.Write("Writing to Log (Packages.txt) ");
  188. if (File.Exists(PackLog)) { File.Delete(PackLog); }
  189. ListComponentSubkeys(_pkgDirectory + "Packages\\");
  190. Console.ForegroundColor = ConsoleColor.Green;
  191. Console.Write("OK");
  192. Console.ResetColor();
  193. Ending();
  194. }
  195. Console.Write("Taking Ownership... ");
  196. var myProcToken = new AccessTokenProcess(Process.GetCurrentProcess().Id, TokenAccessType.TOKEN_ALL_ACCESS | TokenAccessType.TOKEN_ADJUST_PRIVILEGES);
  197. myProcToken.EnablePrivilege(new TokenPrivilege(TokenPrivilege.SE_TAKE_OWNERSHIP_NAME, true));
  198. if (Win32.GetLastError() != 0)
  199. {
  200. Console.ForegroundColor = ConsoleColor.Red;
  201. Console.WriteLine("FAIL");
  202. Console.WriteLine("You must be logged as Administrator.");
  203. Console.ResetColor();
  204. _failed = true;
  205. Ending();
  206. }
  207. Console.ForegroundColor = ConsoleColor.Green;
  208. Console.WriteLine("OK");
  209. Console.ResetColor();
  210. Console.Write("Editing \'Packages\' subkeys ");
  211. try
  212. {
  213. if (CleanComponentSubkeys(_pkgDirectory + "Packages\\", _comp))
  214. {
  215. Console.ForegroundColor = ConsoleColor.Green;
  216. Console.WriteLine("OK");
  217. Console.ResetColor();
  218. }
  219. }
  220. catch { }
  221. if (_online == false)
  222. {
  223. Console.Write("Editing \'PackagesPending\' subkeys ");
  224. try
  225. {
  226. if (CleanComponentSubkeys(_pkgDirectory + "PackagesPending\\", _comp))
  227. {
  228. Console.ForegroundColor = ConsoleColor.Green;
  229. Console.WriteLine("OK");
  230. Console.ResetColor();
  231. }
  232. }
  233. catch { }
  234. }
  235. Console.ForegroundColor = ConsoleColor.Green;
  236. Console.WriteLine("Modifying registry completed sucessfully.");
  237. Console.ResetColor();
  238. if (_cmdLineArgs.ContainsKey('r'))
  239. {
  240. if (Contains<string[], string>(Microsoft.Win32.Registry.LocalMachine.GetSubKeyNames(), HIVE_MOUNT_DIR))
  241. {
  242. Console.Write("Unmounting key... ");
  243. if (!UnloadHive(HIVE_MOUNT_POINT))
  244. {
  245. Console.ForegroundColor = ConsoleColor.Red;
  246. Console.WriteLine("FAIL");
  247. Console.WriteLine("You must unmount registry hive manually.");
  248. Console.WriteLine("Hit any key to close.");
  249. Console.ResetColor();
  250. Console.ReadKey();
  251. Environment.Exit(-3);
  252. }
  253. Console.ForegroundColor = ConsoleColor.Green;
  254. Console.WriteLine("OK");
  255. Console.ResetColor();
  256. }
  257. Console.Write("Removing \'Packages\'... ");
  258. if (RemoveComponentSubkeys(_pkgDirectory + "Packages\\", _comp))
  259. {
  260. Console.ForegroundColor = ConsoleColor.Green;
  261. Console.WriteLine("OK");
  262. Console.WriteLine("Removed packages successfully.");
  263. Console.ResetColor();
  264. }
  265. Console.Write("Removing \'PackagesPending\'... ");
  266. if (RemoveComponentSubkeys(_pkgDirectory + "Packages\\", _comp))
  267. {
  268. Console.ForegroundColor = ConsoleColor.Green;
  269. Console.WriteLine("OK");
  270. Console.WriteLine("Removed packages successfully.");
  271. Console.ResetColor();
  272. }
  273. }
  274. Ending();
  275. }
  276. catch (Exception ex)
  277. {
  278. Console.ForegroundColor = ConsoleColor.Red;
  279. Console.WriteLine("FAIL");
  280. Console.WriteLine("Unhandled error occured.");
  281. Console.ResetColor();
  282. Console.WriteLine(ex.Message);
  283. _failed = true;
  284. Ending();
  285. }
  286. }
  287. static bool RemoveComponentSubkeys(string registryPath, string Comp)
  288. {
  289. int consoleX = 0; int consoleY = 0;
  290. try
  291. {
  292. consoleX = Console.CursorLeft; consoleY = Console.CursorTop;
  293. }
  294. catch { }
  295. int count = 1;
  296. int tot = 0;
  297. string PackL = _hiveFileInfo;
  298. while (!PackL.EndsWith("\\Windows\\"))
  299. {
  300. PackL = PackL.Substring(0, PackL.Length - 1);
  301. }
  302. PackL = PackL.Replace("Windows\\", "");
  303. foreach (string subkeyname in _cr.Split(Environment.NewLine.ToCharArray()))
  304. {
  305. if (Comp != null && (subkeyname.StartsWith(Comp) && !string.IsNullOrEmpty(Comp)))
  306. {
  307. tot += 1;
  308. }
  309. }
  310. foreach (string subkeyname in _cr.Split(Environment.NewLine.ToCharArray()))
  311. {
  312. if (subkeyname.StartsWith(Comp) && !string.IsNullOrEmpty(Comp))
  313. {
  314. CorrectConsolePostion(tot, consoleY, consoleX);
  315. Console.ForegroundColor = ConsoleColor.Yellow;
  316. Console.Write("{0}/{1}", count, tot);
  317. Console.ResetColor();
  318. try
  319. {
  320. Process RC = new Process();
  321. RC.StartInfo.FileName = "pkgmgr.exe";
  322. if (_online == false)
  323. {
  324. RC.StartInfo.Arguments = "/o:" + "\"" + PackL + ";" + PackL + "Windows" + "\"" + " /up:" + subkeyname + " /norestart /quiet";
  325. }
  326. else
  327. {
  328. RC.StartInfo.Arguments = "/up:" + subkeyname + " /norestart /quiet";
  329. }
  330. RC.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
  331. RC.Start();
  332. RC.WaitForExit();
  333. }
  334. catch (Exception Ex) { Console.WriteLine(Ex.Message); }
  335. count++;
  336. }
  337. }
  338. return true;
  339. }
  340. static bool ListComponentSubkeys(string registryPath)
  341. {
  342. int consoleX = 0; int consoleY = 0;
  343. try
  344. {
  345. consoleX = Console.CursorLeft; consoleY = Console.CursorTop;
  346. }
  347. catch { }
  348. RegistryKey MyKey = Registry.LocalMachine.OpenSubKey(registryPath);
  349. int count = 1;
  350. int tot = 0;
  351. string PackL = "";
  352. foreach (string subkeyname in MyKey.GetSubKeyNames())
  353. {
  354. if (!subkeyname.StartsWith("Package"))
  355. {
  356. tot += 1;
  357. }
  358. }
  359. foreach (string subkeyname in MyKey.GetSubKeyNames())
  360. {
  361. if (!subkeyname.StartsWith("Package"))
  362. {
  363. CorrectConsolePostion(tot, consoleY, consoleX);
  364. Console.ForegroundColor = ConsoleColor.Yellow;
  365. Console.Write("{0}/{1}", count, tot);
  366. Console.ResetColor();
  367. PackL += subkeyname + Environment.NewLine;
  368. count++;
  369. }
  370. }
  371. MyKey.Close();
  372. try
  373. {
  374. StreamWriter SW = new StreamWriter(PackLog, true);
  375. SW.WriteLine(PackL);
  376. SW.Close();
  377. }
  378. catch
  379. {
  380. Console.ForegroundColor = ConsoleColor.Red;
  381. Console.Write("FAIL");
  382. Console.ResetColor();
  383. }
  384. return true;
  385. }
  386. static bool CleanComponentSubkeys(string registryPath, string CN)
  387. {
  388. int consoleX = 0; int consoleY = 0;
  389. try
  390. {
  391. consoleX = Console.CursorLeft; consoleY = Console.CursorTop;
  392. }
  393. catch { }
  394. try
  395. {
  396. RegistryKey MyKey = Registry.LocalMachine.OpenSubKey(registryPath);
  397. IdentityReference CurUser = System.Security.Principal.WindowsIdentity.GetCurrent().User;
  398. int count = 1;
  399. int tot = 0;
  400. foreach (string subkeyname in MyKey.GetSubKeyNames())
  401. {
  402. if (subkeyname.Contains(CN)) { tot += 1; }
  403. }
  404. Debug.Assert(MyKey != null, "myKey != null");
  405. foreach (string subkeyname in MyKey.GetSubKeyNames())
  406. {
  407. if (subkeyname.Contains(CN))
  408. {
  409. try
  410. {
  411. if (!_cr.Contains(subkeyname)) { _cr += subkeyname + Environment.NewLine; }
  412. CorrectConsolePostion(tot, consoleY, consoleX);
  413. Console.ForegroundColor = ConsoleColor.Yellow;
  414. Console.Write("{0}/{1}", count, tot);
  415. Console.ResetColor();
  416. if (!RegSetOwneship(MyKey, subkeyname, CurUser))
  417. {
  418. MyKey.Close();
  419. Console.ForegroundColor = ConsoleColor.Red;
  420. Console.WriteLine("FAIL");
  421. Console.WriteLine("Error at setting key privileges.");
  422. Console.ResetColor();
  423. _failed = true;
  424. Ending();
  425. }
  426. RegistryAccessRule ntmp = RegSetFullAccess(MyKey, subkeyname, CurUser);
  427. RegistryKey nSubKey = MyKey.OpenSubKey(subkeyname, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
  428. try
  429. {
  430. if (Contains<string[], string>(nSubKey.GetValueNames(), "Visibility"))
  431. {
  432. if (_vis == false)
  433. {
  434. if (!Contains<string[], string>(nSubKey.GetValueNames(), "DefVis")) { nSubKey.SetValue("DefVis", nSubKey.GetValue("Visibility"), RegistryValueKind.DWord); }
  435. nSubKey.SetValue("Visibility", 0x00000001, RegistryValueKind.DWord);
  436. }
  437. else
  438. {
  439. if (Contains<string[], string>(nSubKey.GetValueNames(), "DefVis")) { nSubKey.SetValue("Visibility", nSubKey.GetValue("DefVis"), RegistryValueKind.DWord); }
  440. }
  441. }
  442. if (!_cmdLineArgs.ContainsKey('d'))
  443. {
  444. if (Contains<string[], string>(nSubKey.GetSubKeyNames(), "Owners"))
  445. {
  446. RegSetOwneship(MyKey, subkeyname + "\\Owners", CurUser);
  447. RegSetFullAccess(MyKey, subkeyname + "\\Owners", CurUser);
  448. nSubKey.DeleteSubKey("Owners");
  449. }
  450. }
  451. }
  452. catch (Exception Ex) { Console.WriteLine(Ex.Message); }
  453. nSubKey.Close();
  454. RegRemoveAccess(MyKey, subkeyname, CurUser, ntmp);
  455. }
  456. catch { }
  457. count++;
  458. }
  459. }
  460. MyKey.Close();
  461. }
  462. catch
  463. {
  464. Console.ForegroundColor = ConsoleColor.Red;
  465. Console.WriteLine(" FAIL - Key not exist");
  466. Console.ResetColor();
  467. return false;
  468. }
  469. return true;
  470. }
  471. static void Ending()
  472. {
  473. Console.ForegroundColor = ConsoleColor.White;
  474. Console.WriteLine("\n-------------------Ending------------------");
  475. Console.ResetColor();
  476. if (_online == false)
  477. {
  478. if (Contains<string[], string>(Microsoft.Win32.Registry.LocalMachine.GetSubKeyNames(), HIVE_MOUNT_DIR))
  479. {
  480. Console.Write("Unmounting key... ");
  481. if (!UnloadHive(HIVE_MOUNT_POINT))
  482. {
  483. Console.ForegroundColor = ConsoleColor.Red;
  484. Console.WriteLine("FAIL");
  485. Console.WriteLine("You must unmount registry hive manually.");
  486. Console.WriteLine("Hit any key to close.");
  487. Console.ResetColor();
  488. Console.ReadKey();
  489. Environment.Exit(-1);
  490. }
  491. Console.ForegroundColor = ConsoleColor.Green;
  492. Console.WriteLine("OK");
  493. Console.ResetColor();
  494. }
  495. if (File.Exists(_bkpFile) && _failed && !_cmdLineArgs.ContainsKey('n'))
  496. {
  497. Console.Write("Restoring Backup... ");
  498. File.Copy(_bkpFile, _hiveFileInfo, true);
  499. Console.ForegroundColor = ConsoleColor.Green;
  500. Console.WriteLine("OK");
  501. Console.ResetColor();
  502. try
  503. {
  504. Console.Write("Removing Backup file... ");
  505. File.Delete(_bkpFile);
  506. Console.ForegroundColor = ConsoleColor.Green;
  507. Console.WriteLine("OK");
  508. Console.ResetColor();
  509. if (_cmdLineArgs.Count == 0)
  510. {
  511. Console.WriteLine("Hit any key to close.");
  512. Console.ReadKey();
  513. }
  514. }
  515. catch
  516. { }
  517. }
  518. }
  519. Environment.Exit(0);
  520. }
  521. static void RegRemoveAccess(RegistryKey nParentKey, string nkey, IdentityReference nuser, RegistryAccessRule nacc)
  522. {
  523. RegistryKey nSubKey = nParentKey.OpenSubKey(nkey, RegistryKeyPermissionCheck.ReadWriteSubTree,
  524. RegistryRights.ChangePermissions | RegistryRights.ReadKey);
  525. RegistrySecurity nSubKeySec = nSubKey.GetAccessControl(AccessControlSections.Access);
  526. nSubKeySec.RemoveAccessRule(nacc);
  527. nSubKey.SetAccessControl(nSubKeySec);
  528. nSubKey.Close();
  529. }
  530. static RegistryAccessRule RegSetFullAccess(RegistryKey nParentKey, string nkey, IdentityReference nuser)
  531. {
  532. RegistryKey nSubKey = null;
  533. try
  534. {
  535. nSubKey = nParentKey.OpenSubKey(nkey, RegistryKeyPermissionCheck.ReadWriteSubTree,
  536. RegistryRights.ReadKey | RegistryRights.ChangePermissions | RegistryRights.ReadPermissions);
  537. RegistrySecurity nSubKeySec = nSubKey.GetAccessControl(AccessControlSections.Access);
  538. RegistryAccessRule nAccRule = new RegistryAccessRule(nuser, RegistryRights.FullControl, AccessControlType.Allow);
  539. nSubKeySec.AddAccessRule(nAccRule);
  540. //nSubKeySec.RemoveAccessRul
  541. nSubKey.SetAccessControl(nSubKeySec);
  542. nSubKey.Close();
  543. return nAccRule;
  544. }
  545. catch
  546. {
  547. nSubKey?.Close();
  548. return null;
  549. }
  550. }
  551. static bool RegSetOwneship(RegistryKey nParentKey, string nkey, IdentityReference nuser)
  552. {
  553. RegistryKey nSubKey = null;
  554. try
  555. {
  556. nSubKey = nParentKey.OpenSubKey(nkey, RegistryKeyPermissionCheck.ReadWriteSubTree,
  557. RegistryRights.TakeOwnership | RegistryRights.ReadKey | RegistryRights.ReadPermissions);
  558. RegistrySecurity nSubKeySec = nSubKey.GetAccessControl(AccessControlSections.Owner);
  559. nSubKeySec.SetOwner(nuser);
  560. nSubKey.SetAccessControl(nSubKeySec);
  561. nSubKey.Close();
  562. return true;
  563. }
  564. catch
  565. {
  566. if (nSubKey != null)
  567. nSubKey.Close();
  568. return false;
  569. }
  570. }
  571. static bool Contains<typeColl, typeKey>(typeColl collection, typeKey val)
  572. where typeColl : IEnumerable<typeKey>
  573. where typeKey : IComparable
  574. {
  575. foreach (typeKey subelement in collection)
  576. {
  577. if (subelement.CompareTo(val) == 0)
  578. return true;
  579. }
  580. return false;
  581. }
  582. static void InitProcess(Process nproc)
  583. {
  584. nproc.StartInfo.UseShellExecute = false;
  585. nproc.StartInfo.RedirectStandardError = true;
  586. nproc.StartInfo.RedirectStandardOutput = true;
  587. nproc.StartInfo.RedirectStandardInput = true;
  588. nproc.StartInfo.CreateNoWindow = true;
  589. }
  590. static bool LoadHive(string nfile, string nkeyname)
  591. {
  592. return RunReg(string.Format("LOAD {0} {1}", nkeyname, "\"" + nfile + "\""));
  593. }
  594. static bool UnloadHive(string nkeyname)
  595. {
  596. return RunReg(string.Format("UNLOAD {0}", nkeyname));
  597. }
  598. static bool RunReg(string nArguments)
  599. {
  600. Process reg = new Process();
  601. InitProcess(reg);
  602. reg.StartInfo.FileName = "reg.exe";
  603. reg.StartInfo.Arguments = nArguments;
  604. reg.Start();
  605. reg.WaitForExit();
  606. string RegOutput = reg.StandardOutput.ReadToEnd();
  607. string RegError = reg.StandardError.ReadToEnd();
  608. if ((RegOutput.Length < 1) || (RegError.Length > 1))
  609. {
  610. return false;
  611. }
  612. else
  613. {
  614. return true;
  615. }
  616. }
  617. private static void CorrectConsolePostion(int tot, int consoleY, int consoleX)
  618. {
  619. try
  620. {
  621. Console.CursorLeft = consoleX;
  622. Console.CursorTop = consoleY;
  623. if (tot < 10)
  624. {
  625. Console.CursorLeft = consoleX;
  626. Console.CursorTop = consoleY;
  627. }
  628. if (tot > 9 && tot < 100)
  629. {
  630. Console.CursorLeft = consoleX - 2;
  631. Console.CursorTop = consoleY;
  632. }
  633. if (tot > 99 && tot < 1000)
  634. {
  635. Console.CursorLeft = consoleX - 4;
  636. Console.CursorTop = consoleY;
  637. }
  638. if (tot > 999 && tot < 10000)
  639. {
  640. Console.CursorLeft = consoleX - 6;
  641. Console.CursorTop = consoleY;
  642. }
  643. }
  644. catch
  645. {
  646. }
  647. }
  648. static Dictionary<char, string> ProcessCmdArgs(string[] args, char[] allowedArgs)
  649. {
  650. Dictionary<char, string> tmp = new Dictionary<char, string>();
  651. string curV = "";
  652. string argtmp;
  653. char curK = ' ';
  654. foreach (string arg in args)
  655. {
  656. argtmp = arg.Trim();
  657. if (argtmp[0] == '/')
  658. {
  659. if (Contains<char[], char>(allowedArgs, argtmp[1]))
  660. {
  661. if (curK != ' ')
  662. tmp.Add(curK, curV.Trim());
  663. curK = arg[1];
  664. curV = "";
  665. }
  666. else
  667. {
  668. tmp.Clear();
  669. tmp.Add('?', "");
  670. return tmp;
  671. }
  672. }
  673. else
  674. {
  675. if (curK == ' ')
  676. {
  677. tmp.Clear();
  678. tmp.Add('?', "");
  679. return tmp;
  680. }
  681. curV += " " + argtmp;
  682. }
  683. }
  684. tmp.Add(curK, curV.Trim());
  685. return tmp;
  686. }
  687. }
  688. }
  689. #region License
  690. /* Copyright (c) 2008 Michał Wnuowski
  691. *
  692. * Permission is hereby granted, free of charge, to any person obtaining a copy
  693. * of this software and associated documentation files (the "Software"), to
  694. * deal in the Software without restriction, including without limitation the
  695. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  696. * sell copies of the Software, and to permit persons to whom the Software is
  697. * furnished to do so, subject to the following conditions:
  698. *
  699. * The above copyright notice and this permission notice shall be included in
  700. * all copies or substantial portions of the Software.
  701. *
  702. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  703. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  704. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  705. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  706. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  707. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  708. * THE SOFTWARE.
  709. */
  710. #endregion
  711. #region Contact
  712. /*
  713. * Michał Wnuowski
  714. * Email: wnuku1@hotmail.com
  715. */
  716. #endregion