Program.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using MSI.Afterburner;
  2. using System;
  3. using GpuFanControl.MsiAfterburnerWrap;
  4. namespace GpuFanControl
  5. {
  6. internal class Program
  7. {
  8. private static int Main(string[] args)
  9. {
  10. TryingSandBox.Go();
  11. return -9999;
  12. if (args.Length == 0)
  13. {
  14. PrintUsage();
  15. return -1;
  16. }
  17. if (args[0].ToLower().StartsWith("-q"))
  18. {
  19. int? index = null;
  20. if (int.TryParse(args[0].Substring(2), out int parsedIndex))
  21. {
  22. index = parsedIndex;
  23. }
  24. var cm = ConnectToAfterburner(out var errCode);
  25. if (cm == null) return errCode;
  26. void PrintSpeedTableHeader() => Console.WriteLine("index\tmode\tmin\tval\tmax\t%");
  27. void PrintGpuFanSpeed(ControlMemoryGpuEntry g)
  28. {
  29. if (!g.Flags.HasFlag(MACM_SHARED_MEMORY_GPU_ENTRY_FLAG.FAN_SPEED))
  30. {
  31. Console.WriteLine($"{g.Index}\t** No support fan speed **");
  32. }
  33. else
  34. {
  35. var gfr = g.FanSpeedMax - g.FanSpeedMin;
  36. Console.WriteLine($"{g.Index}\t{g.FanFlagsCur}\t{g.FanSpeedMin}\t{g.FanSpeedCur}\t{g.FanSpeedMax}\t{((float)g.FanSpeedCur - g.FanSpeedMin) / gfr * 100:N2}%");
  37. }
  38. }
  39. if (index.HasValue)
  40. {
  41. if (index < 0 || index > cm.GpuEntries.Length - 1)
  42. {
  43. Console.WriteLine("Argument Error: index out of GPU entries");
  44. return -1;
  45. }
  46. //show specified
  47. PrintSpeedTableHeader();
  48. var g = cm.GpuEntries[index.Value];
  49. PrintGpuFanSpeed(g);
  50. }
  51. else
  52. {
  53. //show all
  54. PrintSpeedTableHeader();
  55. foreach (var g in cm.GpuEntries)
  56. {
  57. PrintGpuFanSpeed(g);
  58. }
  59. }
  60. }
  61. if (args[0].ToLower().StartsWith("-s"))
  62. {
  63. float? value = null;
  64. var sParam = args[0].Substring(2);
  65. if (sParam.ToLower() == "auto")
  66. {
  67. }
  68. else if (float.TryParse(sParam, out var parsedValue))
  69. {
  70. value = parsedValue;
  71. }
  72. else
  73. {
  74. Console.WriteLine("Argument Error: -s ");
  75. return -1;
  76. }
  77. int? index = null;
  78. if (args.Length > 1 && args[1].ToLower().StartsWith("-i") && int.TryParse(args[1].Substring(2), out int parsedIndex))
  79. {
  80. index = parsedIndex;
  81. }
  82. var cm = ConnectToAfterburner(out var errCode);
  83. if (cm == null) return errCode;
  84. void SetGpuFanSpeed(ControlMemoryGpuEntry g)
  85. {
  86. Console.WriteLine($"Try change parameter for GPU {g.Index}");
  87. try
  88. {
  89. if (value.HasValue)
  90. {
  91. g.FanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.None;
  92. var gfr = g.FanSpeedMax - g.FanSpeedMin;
  93. var targetValue = (uint)Math.Round(g.FanSpeedMin + gfr * value.Value / 100);
  94. if (targetValue < g.FanSpeedMin) g.FanSpeedCur = g.FanSpeedMin;
  95. else if (targetValue > g.FanSpeedMax) g.FanSpeedCur = g.FanSpeedMax;
  96. else g.FanSpeedCur = targetValue;
  97. Console.WriteLine($"G{g.Index} min:{g.FanSpeedMin} max:{g.FanSpeedMax} set:{g.FanSpeedCur}");
  98. }
  99. else
  100. {
  101. g.FanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.AUTO;
  102. }
  103. Console.WriteLine("Success.");
  104. }
  105. catch (Exception e)
  106. {
  107. Console.WriteLine($"Failure to change parameter for GPU {g.Index}, {e.Message}");
  108. return;
  109. }
  110. Console.WriteLine($"Commiting Change For GPU {g.Index}");
  111. try
  112. {
  113. cm.CommitChanges(g.Index);
  114. Console.WriteLine($"Success.");
  115. }
  116. catch (Exception ex)
  117. {
  118. Console.WriteLine($"Failure Commit Change For GPU {g.Index}, {ex.Message}");
  119. }
  120. Console.WriteLine($"Reloading Control Memory...");
  121. try
  122. {
  123. cm.ReloadAll();
  124. Console.WriteLine($"Success.");
  125. }
  126. catch (Exception e)
  127. {
  128. Console.WriteLine($"Failure Reloading Control Memory, {e.Message}");
  129. }
  130. }
  131. if (index.HasValue)
  132. {
  133. if (index < 0 || index > cm.GpuEntries.Length - 1)
  134. {
  135. Console.WriteLine("Argument Error: index out of GPU entries");
  136. return -1;
  137. }
  138. //set specified
  139. var g = cm.GpuEntries[index.Value];
  140. SetGpuFanSpeed(g);
  141. }
  142. else
  143. {
  144. //set all
  145. foreach (var g in cm.GpuEntries)
  146. {
  147. SetGpuFanSpeed(g);
  148. }
  149. }
  150. }
  151. return 0;
  152. }
  153. private static ControlMemory ConnectToAfterburner(out int error)
  154. {
  155. Console.Write("Connecting to MSI Afterburner...");
  156. try
  157. {
  158. var cm = new ControlMemory();
  159. try
  160. {
  161. var l = Console.CursorLeft;
  162. Console.CursorLeft = 0;
  163. Console.Write("".PadLeft(l));
  164. Console.CursorLeft = 0;
  165. }
  166. catch (Exception e)
  167. {
  168. Console.WriteLine();
  169. }
  170. error = 0;
  171. return cm;
  172. }
  173. catch (Exception e)
  174. {
  175. Console.WriteLine();
  176. Console.WriteLine(e.Message);
  177. error = -2;
  178. return null;
  179. }
  180. }
  181. private static void PrintUsage()
  182. {
  183. Console.WriteLine("GPU Fan Control Utility");
  184. Console.WriteLine("\tBased on MSI Afterburner 2.1 or later");
  185. Console.WriteLine();
  186. Console.WriteLine("Query GPU Fan Speed");
  187. Console.WriteLine("\t-q[index] blank for all");
  188. Console.WriteLine("\toutput: index\tmode\tmin\tval(%)\tmax");
  189. Console.WriteLine();
  190. Console.WriteLine("Set GPU Fan Speed");
  191. Console.WriteLine("\t-s<auto|percent>");
  192. Console.WriteLine("\t[-i<index>] blank for all");
  193. Console.WriteLine();
  194. Console.WriteLine("Exit Code:");
  195. Console.WriteLine("\tSuccess:\t0");
  196. Console.WriteLine("\tArgument Error:\t-1");
  197. Console.WriteLine("\tConnect Fail:\t-2");
  198. Console.WriteLine("\tUnknown Fail:\t-3");
  199. }
  200. }
  201. }