MsiAfterBurnControlWrap.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.IO;
  3. using System.IO.MemoryMappedFiles;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs;
  7. namespace GpuFanControl.MsiAfterburnerWrap
  8. {
  9. internal class MsiAfterBurnControlWrap : IDisposable
  10. {
  11. //const
  12. private const string ShareMemoryName = "MACMSharedMemory";
  13. private const string MutexName = "Global\\Access_MACMSharedMemory";
  14. private const uint HeaderSignature = ('M' << 24 | ('A' << 16) | ('C' << 8) | ('M'));
  15. private MemoryMappedFile _hMapFile;
  16. private MemoryMappedViewStream _pMapAddr;
  17. private byte[] _bufSnap;
  18. public void Connect()
  19. {
  20. //connect , failure throws FileNotFoundException
  21. _hMapFile = MemoryMappedFile.OpenExisting(ShareMemoryName, MemoryMappedFileRights.FullControl);
  22. _pMapAddr = _hMapFile.CreateViewStream();
  23. }
  24. public void Disconnect()
  25. {
  26. _pMapAddr?.Close();
  27. _hMapFile?.Dispose();
  28. }
  29. public void Refresh()
  30. {
  31. using (var m = new Mutex(false, MutexName))
  32. {
  33. m.WaitOne();
  34. var lenHeader = Marshal.SizeOf(typeof(MACM_SHARED_MEMORY_HEADER));
  35. var bufHeader = new byte[lenHeader];
  36. _pMapAddr.Seek(0, SeekOrigin.Begin);
  37. _pMapAddr.Read(bufHeader, 0, lenHeader);
  38. var stHeader = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(bufHeader);
  39. if (stHeader.Signature == HeaderSignature)
  40. {
  41. var lenSnap = stHeader.HeaderSize + stHeader.GpuEntryCount * stHeader.GpuEntrySize;
  42. var bufSnap = new byte[lenSnap];
  43. _pMapAddr.Seek(0, SeekOrigin.Begin);
  44. _pMapAddr.Read(bufSnap, 0, (int)lenSnap);
  45. _bufSnap = bufSnap;
  46. }
  47. m.ReleaseMutex();
  48. }
  49. }
  50. public void Commit()
  51. {
  52. using (var m = new Mutex(false, MutexName))
  53. {
  54. m.WaitOne();
  55. _pMapAddr.Seek(0, SeekOrigin.Begin);
  56. _pMapAddr.Write(_bufSnap, 0, _bufSnap.Length);
  57. _pMapAddr.Flush();
  58. _pMapAddr.Seek(0, SeekOrigin.Begin);
  59. var header = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
  60. header.Command = MACM_SHARED_MEMORY_COMMAND.FLUSH;
  61. StructToByteArray(header, _bufSnap);
  62. _pMapAddr.Seek(0, SeekOrigin.Begin);
  63. _pMapAddr.Write(_bufSnap, 0, _bufSnap.Length);
  64. _pMapAddr.Flush();
  65. m.ReleaseMutex();
  66. }
  67. Refresh();
  68. }
  69. public int GetNumberOfGpu()
  70. {
  71. var st = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
  72. return (int)st.GpuEntryCount;
  73. }
  74. public MACM_SHARED_MEMORY_GPU_ENTRY? GetGpuEntry(int index, out string error)
  75. {
  76. var header = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
  77. if (index > header.GpuEntryCount || index < 0)
  78. {
  79. error = "index out of range";
  80. return null;
  81. }
  82. var entry = ByteArrayToStruct<MACM_SHARED_MEMORY_GPU_ENTRY>(_bufSnap, (int)header.HeaderSize + index * (int)header.GpuEntrySize);
  83. error = null;
  84. return entry;
  85. }
  86. public string SetGpuEntry(MACM_SHARED_MEMORY_GPU_ENTRY entry, int index, bool flush = false)
  87. {
  88. var header = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
  89. if (index > header.GpuEntryCount || index < 0) return "index out of range";
  90. StructToByteArray(entry, _bufSnap, (int)(header.HeaderSize + index * header.GpuEntrySize));
  91. if (flush) Commit();
  92. return null;
  93. }
  94. private T ByteArrayToStruct<T>(byte[] bytes, int offset = 0)
  95. {
  96. GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  97. T stuff;
  98. try
  99. {
  100. stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject() + offset, typeof(T));
  101. }
  102. finally
  103. {
  104. handle.Free();
  105. }
  106. return stuff;
  107. }
  108. private static void StructToByteArray<T>(T str, byte[] snap, int offset = 0)
  109. {
  110. var h = default(GCHandle);
  111. try
  112. {
  113. h = GCHandle.Alloc(snap, GCHandleType.Pinned);
  114. Marshal.StructureToPtr<T>(str, h.AddrOfPinnedObject() + offset, false);
  115. }
  116. finally
  117. {
  118. if (h.IsAllocated)
  119. {
  120. h.Free();
  121. }
  122. }
  123. }
  124. public void Dispose()
  125. {
  126. Disconnect();
  127. }
  128. }
  129. }