Program.cs 8.1 KB

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