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

AutoStartup.cs 5.7 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
7 years ago
7 years ago
7 years ago
7 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using Microsoft.Win32;
  6. using NLog;
  7. using Shadowsocks.Util;
  8. namespace Shadowsocks.Controller
  9. {
  10. static class AutoStartup
  11. {
  12. private static Logger logger = LogManager.GetCurrentClassLogger();
  13. // Don't use Application.ExecutablePath
  14. // see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue
  15. private static string Key = "Shadowsocks_" + Program.ExecutablePath.GetHashCode();
  16. public static bool Set(bool enabled)
  17. {
  18. RegistryKey runKey = null;
  19. try
  20. {
  21. runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
  22. if (runKey == null)
  23. {
  24. logger.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run");
  25. return false;
  26. }
  27. if (enabled)
  28. {
  29. runKey.SetValue(Key, Program.ExecutablePath);
  30. }
  31. else
  32. {
  33. runKey.DeleteValue(Key);
  34. }
  35. // When autostartup setting change, change RegisterForRestart state to avoid start 2 times
  36. RegisterForRestart(!enabled);
  37. return true;
  38. }
  39. catch (Exception e)
  40. {
  41. logger.LogUsefulException(e);
  42. return false;
  43. }
  44. finally
  45. {
  46. if (runKey != null)
  47. {
  48. try
  49. {
  50. runKey.Close();
  51. runKey.Dispose();
  52. }
  53. catch (Exception e)
  54. { logger.LogUsefulException(e); }
  55. }
  56. }
  57. }
  58. public static bool Check()
  59. {
  60. RegistryKey runKey = null;
  61. try
  62. {
  63. runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
  64. if (runKey == null)
  65. {
  66. logger.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run");
  67. return false;
  68. }
  69. string[] runList = runKey.GetValueNames();
  70. foreach (string item in runList)
  71. {
  72. if (item.Equals(Key, StringComparison.OrdinalIgnoreCase))
  73. return true;
  74. else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions
  75. {
  76. string value = Convert.ToString(runKey.GetValue(item));
  77. if (Program.ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase))
  78. {
  79. runKey.DeleteValue(item);
  80. runKey.SetValue(Key, Program.ExecutablePath);
  81. return true;
  82. }
  83. }
  84. }
  85. return false;
  86. }
  87. catch (Exception e)
  88. {
  89. logger.LogUsefulException(e);
  90. return false;
  91. }
  92. finally
  93. {
  94. if (runKey != null)
  95. {
  96. try
  97. {
  98. runKey.Close();
  99. runKey.Dispose();
  100. }
  101. catch (Exception e)
  102. { logger.LogUsefulException(e); }
  103. }
  104. }
  105. }
  106. [DllImport("kernel32.dll", SetLastError = true)]
  107. static extern int RegisterApplicationRestart([MarshalAs(UnmanagedType.LPWStr)] string commandLineArgs, int Flags);
  108. [DllImport("kernel32.dll", SetLastError = true)]
  109. static extern int UnregisterApplicationRestart();
  110. [Flags]
  111. enum ApplicationRestartFlags
  112. {
  113. RESTART_ALWAYS = 0,
  114. RESTART_NO_CRASH = 1,
  115. RESTART_NO_HANG = 2,
  116. RESTART_NO_PATCH = 4,
  117. RESTART_NO_REBOOT = 8,
  118. }
  119. // register restart after system reboot/update
  120. public static void RegisterForRestart(bool register)
  121. {
  122. // requested register and not autostartup
  123. if (register && !Check())
  124. {
  125. // escape command line parameter
  126. string[] args = new List<string>(Program.Args)
  127. .Select(p => p.Replace("\"", "\\\"")) // escape " to \"
  128. .Select(p => p.IndexOf(" ") >= 0 ? "\"" + p + "\"" : p) // encapsule with "
  129. .ToArray();
  130. string cmdline = string.Join(" ", args);
  131. // first parameter is process command line parameter
  132. // needn't include the name of the executable in the command line
  133. RegisterApplicationRestart(cmdline, (int)(ApplicationRestartFlags.RESTART_NO_CRASH | ApplicationRestartFlags.RESTART_NO_HANG));
  134. logger.Debug("Register restart after system reboot, command line:" + cmdline);
  135. }
  136. // requested unregister, which has no side effect
  137. else if (!register)
  138. {
  139. UnregisterApplicationRestart();
  140. logger.Debug("Unregister restart after system reboot");
  141. }
  142. }
  143. }
  144. }