PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/GrinGlobal.Utility64/Program.cs

https://gitlab.com/GRIN-Global/GRIN-Global-server
C# | 280 lines | 277 code | 3 blank | 0 comment | 16 complexity | bf57fe2a70da50c67bb83b9bc389aad5 MD5 | raw file
  1. using System;
  2. using Microsoft.Win32;
  3. using System.Diagnostics;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. namespace Circaware.AppEngine.Util64 {
  8. class Program {
  9. static void Main(string[] args) {
  10. try {
  11. string parentKeyName = null;
  12. string regPath = null;
  13. string regType = null;
  14. string workingDir = null;
  15. string launchApp = null;
  16. string launchArgs = null;
  17. string regValue = null;
  18. bool writingRegKey = false;
  19. bool showInfo = false;
  20. if (args != null && args.Length > 0) {
  21. for (int i = 0; i < args.Length; i++) {
  22. switch (args[i].ToLower()) {
  23. case "/readregkey":
  24. case "-readregkey":
  25. case "--readregkey":
  26. if (i < args.Length - 1) {
  27. regPath = args[++i];
  28. }
  29. break;
  30. case "/writeregkey":
  31. case "-writeregkey":
  32. case "--writeregkey":
  33. writingRegKey = true;
  34. if (i < args.Length - 1) {
  35. regPath = args[++i];
  36. }
  37. break;
  38. case "/readregsubkeys":
  39. case "-readregsubkeys":
  40. case "--readregsubkeys":
  41. if (i < args.Length - 1) {
  42. parentKeyName = args[++i];
  43. }
  44. break;
  45. case "/type":
  46. case "-type":
  47. case "--type":
  48. if (i < args.Length - 1) {
  49. regType = args[++i];
  50. }
  51. break;
  52. case "/info":
  53. case "-info":
  54. case "--info":
  55. showInfo = true;
  56. break;
  57. case "/value":
  58. case "-value":
  59. case "--value":
  60. if (i < args.Length - 1) {
  61. regValue = args[++i];
  62. }
  63. break;
  64. case "/workingdir":
  65. case "-workingdir":
  66. case "--workingdir":
  67. if (i < args.Length - 1){
  68. workingDir = args[++i];
  69. }
  70. break;
  71. case "/launch":
  72. case "--launch":
  73. case "-launch":
  74. if (i < args.Length - 1) {
  75. launchApp = args[++i];
  76. }
  77. while (i < args.Length - 1) {
  78. i++;
  79. if (args[i].Contains(" ") && args[i][0] != '"') {
  80. // doesn't have quotes, but needs quotes...
  81. launchArgs += @" """ + args[i] + @"""";
  82. } else {
  83. launchArgs += " " + args[i];
  84. }
  85. }
  86. break;
  87. }
  88. }
  89. }
  90. // Console.WriteLine(Toolkit.Is64BitOperatingSystem().ToString());
  91. if (!String.IsNullOrEmpty(regPath)) {
  92. // To debug this portion, add to Command Line in Debug property for the project:
  93. // /readregkey "HKEY_LOCAL_MACHINE\SOFTWARE\Circaware\App Engine\AutoFormatURL" /type int
  94. var pos = regPath.LastIndexOf('\\');
  95. if (pos < 0 || regPath.Length <= pos) {
  96. Console.WriteLine("Invalid registry path. Does not contain '\\'.");
  97. return;
  98. }
  99. var regValueName = regPath.Substring(pos + 1);
  100. regPath = regPath.Substring(0, pos);
  101. switch (("" + regType).ToLower()) {
  102. case "int16":
  103. case "int32":
  104. case "int64":
  105. case "uint16":
  106. case "uint32":
  107. case "uint64":
  108. case "short":
  109. case "ushort":
  110. case "long":
  111. case "ulong":
  112. case "int":
  113. case "integer":
  114. case "number":
  115. case "dword":
  116. if (writingRegKey) {
  117. Registry.SetValue(regPath, regValueName, regValue, RegistryValueKind.DWord);
  118. Console.Write("Success");
  119. } else {
  120. int defaultValue = 0;
  121. int.TryParse(regValue, out defaultValue);
  122. Console.Write(getRegValue(regPath, regValueName, defaultValue));
  123. }
  124. break;
  125. case "string":
  126. default:
  127. if (writingRegKey) {
  128. Registry.SetValue(regPath, regValueName, regValue, RegistryValueKind.String);
  129. Console.Write("Success");
  130. } else {
  131. Console.Write(getRegValue(regPath, regValueName, regValue + ""));
  132. }
  133. break;
  134. case "string[]":
  135. if (writingRegKey) {
  136. var values = regValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
  137. Registry.SetValue(regPath, regValueName, values, RegistryValueKind.MultiString);
  138. Console.Write("Success");
  139. } else {
  140. var values = getRegValue(regPath, regValueName, (string[])null);
  141. Console.Write(String.Join(", ", values));
  142. }
  143. break;
  144. }
  145. //var input = Console.ReadLine();
  146. //if (!String.IsNullOrEmpty(input)) {
  147. // Console.WriteLine("is64 bit process=" + (IntPtr.Size == 8).ToString());
  148. // Console.ReadLine();
  149. //}
  150. return;
  151. } else if (!String.IsNullOrEmpty(parentKeyName)) {
  152. using (var rk = Registry.LocalMachine.OpenSubKey(parentKeyName)) {
  153. if (rk == null) {
  154. // key did not exist
  155. Console.Write("");
  156. } else {
  157. // key exists, spit out keynames
  158. Console.Write(String.Join(", ", rk.GetSubKeyNames()));
  159. }
  160. }
  161. return;
  162. }
  163. if (showInfo) {
  164. if (IntPtr.Size == 4) {
  165. Console.WriteLine("apputil64.exe is running as 32-bit process");
  166. } else {
  167. Console.WriteLine("apputil64.exe is running as 64-bit process");
  168. }
  169. }
  170. if (!String.IsNullOrEmpty(launchApp)) {
  171. // To debug this portion, add following as Command Line to Debug in project properties:
  172. // /launch notepad.exe "C:\hi.txt"
  173. var psi = new ProcessStartInfo(launchApp, launchArgs);
  174. psi.CreateNoWindow = true;
  175. psi.WindowStyle = ProcessWindowStyle.Hidden;
  176. psi.UseShellExecute = false;
  177. psi.RedirectStandardOutput = true;
  178. psi.RedirectStandardError = true;
  179. if (!String.IsNullOrEmpty(workingDir)) {
  180. psi.WorkingDirectory = workingDir;
  181. }
  182. //if (Debugger.IsAttached) {
  183. // if (DialogResult.Cancel == MessageBox.Show("Going to run:\nWorkingDir=" + psi.WorkingDirectory + "\nExe=" + psi.FileName + "\nArgs=" + psi.Arguments, "Is 64bit=" + (IntPtr.Size == 8), MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) {
  184. // return;
  185. // }
  186. //}
  187. __processError = new StringBuilder();
  188. __processOutput = new StringBuilder();
  189. var p = new Process();
  190. p.StartInfo = psi;
  191. p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
  192. p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
  193. p.Start();
  194. p.BeginErrorReadLine();
  195. p.BeginOutputReadLine();
  196. while (!p.HasExited) {
  197. Thread.Sleep(100);
  198. Application.DoEvents();
  199. }
  200. string output = __processOutput.ToString() + " " + __processError.ToString();
  201. Console.WriteLine(output);
  202. return;
  203. }
  204. } catch (Exception ex) {
  205. var msg = "Error: " + ex.Message + " CommandLine: " + String.Join(", ", args);
  206. Console.WriteLine(msg);
  207. EventLog.WriteEntry("apputil64", msg, EventLogEntryType.Error);
  208. }
  209. }
  210. private static StringBuilder __processError;
  211. private static StringBuilder __processOutput;
  212. static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) {
  213. if (__processError != null) {
  214. __processError.AppendLine(e.Data);
  215. }
  216. }
  217. static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) {
  218. if (__processOutput != null) {
  219. __processOutput.AppendLine(e.Data);
  220. }
  221. }
  222. static string getRegValue(string regPath, string regKey, string defaultValue) {
  223. var output = Registry.GetValue(regPath, regKey, defaultValue) as string;
  224. if (output == null) {
  225. output = defaultValue;
  226. }
  227. return output;
  228. }
  229. static int getRegValue(string regPath, string regKey, int defaultValue) {
  230. var output = Registry.GetValue(regPath, regKey, null);
  231. if (output == null) {
  232. return defaultValue;
  233. } else {
  234. var ival = 0;
  235. if (int.TryParse(output.ToString(), out ival)){
  236. return ival;
  237. } else {
  238. return defaultValue;
  239. }
  240. }
  241. }
  242. static string[] getRegValue(string regPath, string regKey, string[] defaultValue) {
  243. var output = Registry.GetValue(regPath, regKey, defaultValue) as string[];
  244. if (output == null) {
  245. output = defaultValue;
  246. }
  247. return output;
  248. }
  249. }
  250. }