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