Browse Source

switch to my own implement

HOME 11 months ago
parent
commit
3384640d36

+ 7 - 8
GpuFanControl/GpuFanControl.csproj

@@ -40,10 +40,6 @@
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
   </PropertyGroup>
   <ItemGroup>
-    <Reference Include="MSIAfterburner.NET, Version=1.1.1.0, Culture=neutral, processorArchitecture=MSIL">
-      <SpecificVersion>False</SpecificVersion>
-      <HintPath>ReferenceAssemblies\MSIAfterburner.NET.dll</HintPath>
-    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
@@ -54,6 +50,13 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="MsiAfterburnerWrap\ShareMemoryStructs\MACM_SHARED_MEMORY_COMMAND.cs" />
+    <Compile Include="MsiAfterburnerWrap\ShareMemoryStructs\MACM_SHARED_MEMORY_FLAG.cs" />
+    <Compile Include="MsiAfterburnerWrap\ShareMemoryStructs\MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.cs" />
+    <Compile Include="MsiAfterburnerWrap\ShareMemoryStructs\MACM_SHARED_MEMORY_GPU_ENTRY_FLAG.cs" />
+    <Compile Include="MsiAfterburnerWrap\ShareMemoryStructs\MACM_SHARED_MEMORY_HEADER.cs" />
+    <Compile Include="MsiAfterburnerWrap\MsiAfterBurnControlWrap.cs" />
+    <Compile Include="MsiAfterburnerWrap\ShareMemoryStructs\MACM_SHARED_MEMORY_GPU_ENTRY.cs" />
     <Compile Include="MsiAfterburnerWrap\TryingSandBox.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
@@ -61,12 +64,8 @@
   <ItemGroup>
     <None Include="App.config" />
     <None Include="packages.config" />
-    <None Include="ReferenceAssemblies\MSIAfterburner.NET.chm" />
   </ItemGroup>
   <ItemGroup />
-  <ItemGroup>
-    <Content Include="ReferenceAssemblies\MSIAfterburner.NET.dll" />
-  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
     <PropertyGroup>

+ 159 - 0
GpuFanControl/MsiAfterburnerWrap/MsiAfterBurnControlWrap.cs

@@ -0,0 +1,159 @@
+using System;
+using System.IO;
+using System.IO.MemoryMappedFiles;
+using System.Runtime.InteropServices;
+using System.Threading;
+using GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs;
+
+namespace GpuFanControl.MsiAfterburnerWrap
+{
+    internal class MsiAfterBurnControlWrap : IDisposable
+    {
+        //const
+        private const string ShareMemoryName = "MACMSharedMemory";
+
+        private const string MutexName = "Global\\Access_MACMSharedMemory";
+        private const uint HeaderSignature = ('M' << 24 | ('A' << 16) | ('C' << 8) | ('M'));
+
+        private MemoryMappedFile _hMapFile;
+        private MemoryMappedViewStream _pMapAddr;
+
+        private byte[] _bufSnap;
+
+        public void Connect()
+        {
+            //connect , failure throws FileNotFoundException
+            _hMapFile = MemoryMappedFile.OpenExisting(ShareMemoryName, MemoryMappedFileRights.FullControl);
+            _pMapAddr = _hMapFile.CreateViewStream();
+        }
+
+        public void Disconnect()
+        {
+            _pMapAddr?.Close();
+            _hMapFile?.Dispose();
+        }
+
+        public void Refresh()
+        {
+            using (var m = new Mutex(false, MutexName))
+            {
+                m.WaitOne();
+
+                var lenHeader = Marshal.SizeOf(typeof(MACM_SHARED_MEMORY_HEADER));
+                var bufHeader = new byte[lenHeader];
+
+                _pMapAddr.Seek(0, SeekOrigin.Begin);
+                _pMapAddr.Read(bufHeader, 0, lenHeader);
+
+                var stHeader = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(bufHeader);
+                if (stHeader.Signature == HeaderSignature)
+                {
+                    var lenSnap = stHeader.HeaderSize + stHeader.GpuEntryCount * stHeader.GpuEntrySize;
+
+                    var bufSnap = new byte[lenSnap];
+
+                    _pMapAddr.Seek(0, SeekOrigin.Begin);
+                    _pMapAddr.Read(bufSnap, 0, (int)lenSnap);
+                    _bufSnap = bufSnap;
+                }
+                m.ReleaseMutex();
+            }
+        }
+
+        public void Commit()
+        {
+            using (var m = new Mutex(false, MutexName))
+            {
+                m.WaitOne();
+
+                _pMapAddr.Seek(0, SeekOrigin.Begin);
+                _pMapAddr.Write(_bufSnap, 0, _bufSnap.Length);
+                _pMapAddr.Flush();
+
+                _pMapAddr.Seek(0, SeekOrigin.Begin);
+                var header = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
+                header.Command = MACM_SHARED_MEMORY_COMMAND.FLUSH;
+                StructToByteArray(header, _bufSnap);
+
+                _pMapAddr.Seek(0, SeekOrigin.Begin);
+                _pMapAddr.Write(_bufSnap, 0, _bufSnap.Length);
+                _pMapAddr.Flush();
+
+                m.ReleaseMutex();
+            }
+
+            Refresh();
+        }
+
+        public int GetNumberOfGpu()
+        {
+            var st = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
+            return (int)st.GpuEntryCount;
+        }
+
+        public MACM_SHARED_MEMORY_GPU_ENTRY? GetGpuEntry(int index, out string error)
+        {
+            var header = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
+            if (index > header.GpuEntryCount || index < 0)
+            {
+                error = "index out of range";
+                return null;
+            }
+            var entry = ByteArrayToStruct<MACM_SHARED_MEMORY_GPU_ENTRY>(_bufSnap, (int)header.HeaderSize + index * (int)header.GpuEntrySize);
+
+            error = null;
+            return entry;
+        }
+
+        public string SetGpuEntry(MACM_SHARED_MEMORY_GPU_ENTRY entry, int index, bool flush = false)
+        {
+            var header = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
+            if (index > header.GpuEntryCount || index < 0) return "index out of range";
+
+            StructToByteArray(entry, _bufSnap, (int)(header.HeaderSize + index * header.GpuEntrySize));
+
+            if (flush) Commit();
+
+            return null;
+        }
+
+        private T ByteArrayToStruct<T>(byte[] bytes, int offset = 0)
+        {
+            GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
+            T stuff;
+            try
+            {
+                stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject() + offset, typeof(T));
+            }
+            finally
+            {
+                handle.Free();
+            }
+            return stuff;
+        }
+
+        private static void StructToByteArray<T>(T str, byte[] snap, int offset = 0)
+        {
+            var h = default(GCHandle);
+
+            try
+            {
+                h = GCHandle.Alloc(snap, GCHandleType.Pinned);
+
+                Marshal.StructureToPtr<T>(str, h.AddrOfPinnedObject() + offset, false);
+            }
+            finally
+            {
+                if (h.IsAllocated)
+                {
+                    h.Free();
+                }
+            }
+        }
+
+        public void Dispose()
+        {
+            Disconnect();
+        }
+    }
+}

+ 12 - 0
GpuFanControl/MsiAfterburnerWrap/ShareMemoryStructs/MACM_SHARED_MEMORY_COMMAND.cs

@@ -0,0 +1,12 @@
+using System;
+
+namespace GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs
+{
+    [Flags]
+    public enum MACM_SHARED_MEMORY_COMMAND : uint
+    {
+        None = 0,
+        INIT = 11206656, // 0x00AB0000
+        FLUSH = 11206657, // 0x00AB0001
+    }
+}

+ 13 - 0
GpuFanControl/MsiAfterburnerWrap/ShareMemoryStructs/MACM_SHARED_MEMORY_FLAG.cs

@@ -0,0 +1,13 @@
+using System;
+
+namespace GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs
+{
+    [Flags]
+    public enum MACM_SHARED_MEMORY_FLAG : uint
+    {
+        None = 0,
+        LINK = 1,
+        SYNC = 2,
+        THERMAL = 4,
+    }
+}

+ 70 - 0
GpuFanControl/MsiAfterburnerWrap/ShareMemoryStructs/MACM_SHARED_MEMORY_GPU_ENTRY.cs

@@ -0,0 +1,70 @@
+using System;
+
+namespace GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs
+{
+    [Serializable]
+    public struct MACM_SHARED_MEMORY_GPU_ENTRY
+    {
+        public MACM_SHARED_MEMORY_GPU_ENTRY_FLAG Flags;
+        public uint CoreClockCur;
+        public uint CoreClockMin;
+        public uint CoreClockMax;
+        public uint CoreClockDef;
+        public uint ShaderClockCur;
+        public uint ShaderClockMin;
+        public uint ShaderClockMax;
+        public uint ShaderClockDef;
+        public uint MemoryClockCur;
+        public uint MemoryClockMin;
+        public uint MemoryClockMax;
+        public uint MemoryClockDef;
+        public uint FanSpeedCur;
+        public MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG FanFlagsCur;
+        public uint FanSpeedMin;
+        public uint FanSpeedMax;
+        public uint FanSpeedDef;
+        public MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG FanFlagsDef;
+        public uint CoreVoltageCur;
+        public uint CoreVoltageMin;
+        public uint CoreVoltageMax;
+        public uint CoreVoltageDef;
+        public uint MemoryVoltageCur;
+        public uint MemoryVoltageMin;
+        public uint MemoryVoltageMax;
+        public uint MemoryVoltageDef;
+        public uint AuxVoltageCur;
+        public uint AuxVoltageMin;
+        public uint AuxVoltageMax;
+        public uint AuxVoltageDef;
+        public int CoreVoltageBoostCur;
+        public int CoreVoltageBoostMin;
+        public int CoreVoltageBoostMax;
+        public int CoreVoltageBoostDef;
+        public int MemoryVoltageBoostCur;
+        public int MemoryVoltageBoostMin;
+        public int MemoryVoltageBoostMax;
+        public int MemoryVoltageBoostDef;
+        public int AuxVoltageBoostCur;
+        public int AuxVoltageBoostMin;
+        public int AuxVoltageBoostMax;
+        public int AuxVoltageBoostDef;
+        public int PowerLimitCur;
+        public int PowerLimitMin;
+        public int PowerLimitMax;
+        public int PowerLimitDef;
+        public int CoreClockBoostCur;
+        public int CoreClockBoostMin;
+        public int CoreClockBoostMax;
+        public int CoreClockBoostDef;
+        public int MemoryClockBoostCur;
+        public int MemoryClockBoostMin;
+        public int MemoryClockBoostMax;
+        public int MemoryClockBoostDef;
+        public int ThermalLimitCur;
+        public int ThermalLimitMin;
+        public int ThermalLimitMax;
+        public int ThermalLimitDef;
+        public uint ThermalPrioritizeCur;
+        public uint ThermalPrioritizeDef;
+    }
+}

+ 11 - 0
GpuFanControl/MsiAfterburnerWrap/ShareMemoryStructs/MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.cs

@@ -0,0 +1,11 @@
+using System;
+
+namespace GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs
+{
+    [Flags]
+    public enum MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG : uint
+    {
+        None = 0,
+        AUTO = 1,
+    }
+}

+ 26 - 0
GpuFanControl/MsiAfterburnerWrap/ShareMemoryStructs/MACM_SHARED_MEMORY_GPU_ENTRY_FLAG.cs

@@ -0,0 +1,26 @@
+using System;
+
+namespace GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs
+{
+    [Flags]
+    public enum MACM_SHARED_MEMORY_GPU_ENTRY_FLAG : uint
+    {
+        None = 0,
+        CORE_CLOCK = 1,
+        SHADER_CLOCK = 2,
+        MEMORY_CLOCK = 4,
+        FAN_SPEED = 8,
+        CORE_VOLTAGE = 16, // 0x00000010
+        MEMORY_VOLTAGE = 32, // 0x00000020
+        AUX_VOLTAGE = 64, // 0x00000040
+        CORE_VOLTAGE_BOOST = 128, // 0x00000080
+        MEMORY_VOLTAGE_BOOST = 256, // 0x00000100
+        AUX_VOLTAGE_BOOST = 512, // 0x00000200
+        POWER_LIMIT = 1024, // 0x00000400
+        CORE_CLOCK_BOOST = 2048, // 0x00000800
+        MEMORY_CLOCK_BOOST = 4096, // 0x00001000
+        THERMAL_LIMIT = 8192, // 0x00002000
+        THERMAL_PRIORITIZE = 16384, // 0x00004000
+        SYNCHRONIZED_WITH_MASTER = 2147483648, // 0x80000000
+    }
+}

+ 18 - 0
GpuFanControl/MsiAfterburnerWrap/ShareMemoryStructs/MACM_SHARED_MEMORY_HEADER.cs

@@ -0,0 +1,18 @@
+using System;
+
+namespace GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs
+{
+    [Serializable]
+    public struct MACM_SHARED_MEMORY_HEADER
+    {
+        public uint Signature;
+        public uint Version;
+        public uint HeaderSize;
+        public uint GpuEntryCount;
+        public uint GpuEntrySize;
+        public uint MasterGpu;
+        public MACM_SHARED_MEMORY_FLAG Flags;
+        public uint Time;
+        public MACM_SHARED_MEMORY_COMMAND Command;
+    }
+}

+ 5 - 285
GpuFanControl/MsiAfterburnerWrap/TryingSandBox.cs

@@ -1,288 +1,8 @@
 using System;
-using System.IO;
-using System.IO.MemoryMappedFiles;
-using System.Runtime.InteropServices;
-using System.Threading;
+using GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs;
 
 namespace GpuFanControl.MsiAfterburnerWrap
 {
-    using ShareMemoryStructs;
-
-    namespace ShareMemoryStructs
-    {
-        [Serializable]
-        public struct MACM_SHARED_MEMORY_HEADER
-        {
-            public uint signature;
-            public uint version;
-            public uint headerSize;
-            public uint gpuEntryCount;
-            public uint gpuEntrySize;
-            public uint masterGpu;
-            public MACM_SHARED_MEMORY_FLAG flags;
-            public uint time;
-            public MACM_SHARED_MEMORY_COMMAND command;
-        }
-
-        [Flags]
-        public enum MACM_SHARED_MEMORY_FLAG : uint
-        {
-            None = 0,
-            LINK = 1,
-            SYNC = 2,
-            THERMAL = 4,
-        }
-
-        [Flags]
-        public enum MACM_SHARED_MEMORY_COMMAND : uint
-        {
-            None = 0,
-            INIT = 11206656, // 0x00AB0000
-            FLUSH = 11206657, // 0x00AB0001
-        }
-
-        [Serializable]
-        public struct MACM_SHARED_MEMORY_GPU_ENTRY
-        {
-            public MACM_SHARED_MEMORY_GPU_ENTRY_FLAG flags;
-            public uint coreClockCur;
-            public uint coreClockMin;
-            public uint coreClockMax;
-            public uint coreClockDef;
-            public uint shaderClockCur;
-            public uint shaderClockMin;
-            public uint shaderClockMax;
-            public uint shaderClockDef;
-            public uint memoryClockCur;
-            public uint memoryClockMin;
-            public uint memoryClockMax;
-            public uint memoryClockDef;
-            public uint fanSpeedCur;
-            public MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG fanFlagsCur;
-            public uint fanSpeedMin;
-            public uint fanSpeedMax;
-            public uint fanSpeedDef;
-            public MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG fanFlagsDef;
-            public uint coreVoltageCur;
-            public uint coreVoltageMin;
-            public uint coreVoltageMax;
-            public uint coreVoltageDef;
-            public uint memoryVoltageCur;
-            public uint memoryVoltageMin;
-            public uint memoryVoltageMax;
-            public uint memoryVoltageDef;
-            public uint auxVoltageCur;
-            public uint auxVoltageMin;
-            public uint auxVoltageMax;
-            public uint auxVoltageDef;
-            public int coreVoltageBoostCur;
-            public int coreVoltageBoostMin;
-            public int coreVoltageBoostMax;
-            public int coreVoltageBoostDef;
-            public int memoryVoltageBoostCur;
-            public int memoryVoltageBoostMin;
-            public int memoryVoltageBoostMax;
-            public int memoryVoltageBoostDef;
-            public int auxVoltageBoostCur;
-            public int auxVoltageBoostMin;
-            public int auxVoltageBoostMax;
-            public int auxVoltageBoostDef;
-            public int powerLimitCur;
-            public int powerLimitMin;
-            public int powerLimitMax;
-            public int powerLimitDef;
-            public int coreClockBoostCur;
-            public int coreClockBoostMin;
-            public int coreClockBoostMax;
-            public int coreClockBoostDef;
-            public int memoryClockBoostCur;
-            public int memoryClockBoostMin;
-            public int memoryClockBoostMax;
-            public int memoryClockBoostDef;
-            public int thermalLimitCur;
-            public int thermalLimitMin;
-            public int thermalLimitMax;
-            public int thermalLimitDef;
-            public uint thermalPrioritizeCur;
-            public uint thermalPrioritizeDef;
-        }
-
-        [Flags]
-        public enum MACM_SHARED_MEMORY_GPU_ENTRY_FLAG : uint
-        {
-            None = 0,
-            CORE_CLOCK = 1,
-            SHADER_CLOCK = 2,
-            MEMORY_CLOCK = 4,
-            FAN_SPEED = 8,
-            CORE_VOLTAGE = 16, // 0x00000010
-            MEMORY_VOLTAGE = 32, // 0x00000020
-            AUX_VOLTAGE = 64, // 0x00000040
-            CORE_VOLTAGE_BOOST = 128, // 0x00000080
-            MEMORY_VOLTAGE_BOOST = 256, // 0x00000100
-            AUX_VOLTAGE_BOOST = 512, // 0x00000200
-            POWER_LIMIT = 1024, // 0x00000400
-            CORE_CLOCK_BOOST = 2048, // 0x00000800
-            MEMORY_CLOCK_BOOST = 4096, // 0x00001000
-            THERMAL_LIMIT = 8192, // 0x00002000
-            THERMAL_PRIORITIZE = 16384, // 0x00004000
-            SYNCHRONIZED_WITH_MASTER = 2147483648, // 0x80000000
-        }
-
-        [Flags]
-        public enum MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG : uint
-        {
-            None = 0,
-            AUTO = 1,
-        }
-    }
-
-    internal class MsiAfterBurnControlWrap
-    {
-        //const
-        private const string ShareMemoryName = "MACMSharedMemory";
-
-        private const string MutexName = "Global\\Access_MACMSharedMemory";
-        private const uint HeaderSignature = ('M' << 24 | ('A' << 16) | ('C' << 8) | ('M'));
-
-        private MemoryMappedFile _hMapFile;
-        private MemoryMappedViewStream _pMapAddr;
-
-        private byte[] _bufSnap;
-
-        public void Connect()
-        {
-            //connect , failure throws FileNotFoundException
-            _hMapFile = MemoryMappedFile.OpenExisting(ShareMemoryName, MemoryMappedFileRights.FullControl);
-            _pMapAddr = _hMapFile.CreateViewStream();
-            //var x_pMapAddr = _hMapFile.CreateViewAccessor();
-        }
-
-        public void Disconnect()
-        {
-            _pMapAddr?.Close();
-            _hMapFile?.Dispose();
-        }
-
-        public void Refresh()
-        {
-            using (var m = new Mutex(false, MutexName))
-            {
-                m.WaitOne();
-
-                var lenHeader = Marshal.SizeOf(typeof(MACM_SHARED_MEMORY_HEADER));
-                var bufHeader = new byte[lenHeader];
-
-                _pMapAddr.Seek(0, SeekOrigin.Begin);
-                _pMapAddr.Read(bufHeader, 0, lenHeader);
-
-                var stHeader = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(bufHeader);
-                if (stHeader.signature == HeaderSignature)
-                {
-                    var lenSnap = stHeader.headerSize + stHeader.gpuEntryCount * stHeader.gpuEntrySize;
-
-                    var bufSnap = new byte[lenSnap];
-
-                    _pMapAddr.Seek(0, SeekOrigin.Begin);
-                    _pMapAddr.Read(bufSnap, 0, (int)lenSnap);
-                    _bufSnap = bufSnap;
-                }
-                m.ReleaseMutex();
-            }
-        }
-
-        public void Commit()
-        {
-            using (var m = new Mutex(false, MutexName))
-            {
-                m.WaitOne();
-
-                _pMapAddr.Seek(0, SeekOrigin.Begin);
-                _pMapAddr.Write(_bufSnap, 0, _bufSnap.Length);
-                _pMapAddr.Flush();
-
-                _pMapAddr.Seek(0, SeekOrigin.Begin);
-                var header = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
-                header.command = MACM_SHARED_MEMORY_COMMAND.FLUSH;
-                StructToByteArray(header, _bufSnap);
-
-                _pMapAddr.Seek(0, SeekOrigin.Begin);
-                _pMapAddr.Write(_bufSnap, 0, _bufSnap.Length);
-                _pMapAddr.Flush();
-
-                m.ReleaseMutex();
-            }
-
-            Refresh();
-        }
-
-        public int GetNumberOfGpu()
-        {
-            var st = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
-            return (int)st.gpuEntryCount;
-        }
-
-        public MACM_SHARED_MEMORY_GPU_ENTRY? GetGpuEntry(int index, out string error)
-        {
-            var header = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
-            if (index > header.gpuEntryCount || index < 0)
-            {
-                error = "index out of range";
-                return null;
-            }
-            var entry = ByteArrayToStruct<MACM_SHARED_MEMORY_GPU_ENTRY>(_bufSnap, (int)header.headerSize + index * (int)header.gpuEntrySize);
-
-            error = null;
-            return entry;
-        }
-
-        public string SetGpuEntry(MACM_SHARED_MEMORY_GPU_ENTRY entry, int index, bool flush = false)
-        {
-            var header = ByteArrayToStruct<MACM_SHARED_MEMORY_HEADER>(_bufSnap);
-            if (index > header.gpuEntryCount || index < 0) return "index out of range";
-
-            StructToByteArray(entry, _bufSnap, (int)(header.headerSize + index * header.gpuEntrySize));
-
-            if (flush) Commit();
-
-            return null;
-        }
-
-        private T ByteArrayToStruct<T>(byte[] bytes, int offset = 0)
-        {
-            GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
-            T stuff;
-            try
-            {
-                stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject() + offset, typeof(T));
-            }
-            finally
-            {
-                handle.Free();
-            }
-            return stuff;
-        }
-
-        private static void StructToByteArray<T>(T str, byte[] snap, int offset = 0)
-        {
-            var h = default(GCHandle);
-
-            try
-            {
-                h = GCHandle.Alloc(snap, GCHandleType.Pinned);
-
-                Marshal.StructureToPtr<T>(str, h.AddrOfPinnedObject() + offset, false);
-            }
-            finally
-            {
-                if (h.IsAllocated)
-                {
-                    h.Free();
-                }
-            }
-        }
-    }
-
     internal class TryingSandBox
     {
         public static void Go()
@@ -308,14 +28,14 @@ namespace GpuFanControl.MsiAfterburnerWrap
                 }
 
                 var entry = g.Value;
-                if (entry.flags.HasFlag(MACM_SHARED_MEMORY_GPU_ENTRY_FLAG.FAN_SPEED))
+                if (entry.Flags.HasFlag(MACM_SHARED_MEMORY_GPU_ENTRY_FLAG.FAN_SPEED))
                 {
                     Console.WriteLine($"Updating fan speed of GPU #{i}");
 
                     //entry.fanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.AUTO;
 
-                    entry.fanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.None;
-                    entry.fanSpeedCur = entry.fanSpeedMax;
+                    entry.FanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.None;
+                    entry.FanSpeedCur = entry.FanSpeedMax;
 
 
                     var err=ctl.SetGpuEntry(entry, i);
@@ -334,4 +54,4 @@ namespace GpuFanControl.MsiAfterburnerWrap
             ctl.Disconnect();
         }
     }
-}
+}

+ 64 - 59
GpuFanControl/Program.cs

@@ -1,6 +1,6 @@
-using MSI.Afterburner;
+using GpuFanControl.MsiAfterburnerWrap;
+using GpuFanControl.MsiAfterburnerWrap.ShareMemoryStructs;
 using System;
-using GpuFanControl.MsiAfterburnerWrap;
 
 namespace GpuFanControl
 {
@@ -8,17 +8,12 @@ namespace GpuFanControl
     {
         private static int Main(string[] args)
         {
-            TryingSandBox.Go();
-
-            return -9999;
-
             if (args.Length == 0)
             {
                 PrintUsage();
                 return -1;
             }
 
-
             if (args[0].ToLower().StartsWith("-q"))
             {
                 int? index = null;
@@ -32,43 +27,53 @@ namespace GpuFanControl
 
                 void PrintSpeedTableHeader() => Console.WriteLine("index\tmode\tmin\tval\tmax\t%");
 
-                void PrintGpuFanSpeed(ControlMemoryGpuEntry g)
+                void PrintGpuFanSpeed(MACM_SHARED_MEMORY_GPU_ENTRY g, int gpuIndex)
                 {
                     if (!g.Flags.HasFlag(MACM_SHARED_MEMORY_GPU_ENTRY_FLAG.FAN_SPEED))
                     {
-                        Console.WriteLine($"{g.Index}\t** No support fan speed **");
+                        Console.WriteLine($"{gpuIndex}\t** No support fan speed **");
                     }
                     else
                     {
                         var gfr = g.FanSpeedMax - g.FanSpeedMin;
-                        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}%");
+                        Console.WriteLine($"{gpuIndex}\t{g.FanFlagsCur}\t{g.FanSpeedMin}\t{g.FanSpeedCur}\t{g.FanSpeedMax}\t{((float)g.FanSpeedCur - g.FanSpeedMin) / gfr * 100:N2}%");
                     }
                 }
 
                 if (index.HasValue)
                 {
-                    if (index < 0 || index > cm.GpuEntries.Length - 1)
+                    var g = cm.GetGpuEntry(index.Value, out var err);
+                    if (g.HasValue)
                     {
-                        Console.WriteLine("Argument Error: index out of GPU entries");
+                        //show specified
+                        PrintSpeedTableHeader();
+                        PrintGpuFanSpeed(g.Value, index.Value);
+                    }
+                    else
+                    {
+                        Console.WriteLine("Argument Error: err");
                         return -1;
                     }
-                    //show specified
-                    PrintSpeedTableHeader();
-                    var g = cm.GpuEntries[index.Value];
-                    PrintGpuFanSpeed(g);
                 }
                 else
                 {
                     //show all
                     PrintSpeedTableHeader();
-                    foreach (var g in cm.GpuEntries)
+                    for (int i = 0; i < cm.GetNumberOfGpu(); i++)
                     {
-                        PrintGpuFanSpeed(g);
+                        var entry = cm.GetGpuEntry(i, out var err);
+                        if (entry.HasValue)
+                        {
+                            PrintGpuFanSpeed(entry.Value, i);
+                        }
+                        else
+                        {
+                            Console.WriteLine($"{i}\tErr:{err}");
+                        }
                     }
                 }
             }
-
-            if (args[0].ToLower().StartsWith("-s"))
+            else if (args[0].ToLower().StartsWith("-s"))
             {
                 float? value = null;
                 var sParam = args[0].Substring(2);
@@ -94,11 +99,11 @@ namespace GpuFanControl
                 var cm = ConnectToAfterburner(out var errCode);
                 if (cm == null) return errCode;
 
-                void SetGpuFanSpeed(ControlMemoryGpuEntry g)
+                void SetGpuFanSpeed(MACM_SHARED_MEMORY_GPU_ENTRY g, int gpuIndex)
                 {
-                    Console.WriteLine($"Try change parameter for GPU {g.Index}");
-                    try
+                    if (g.Flags.HasFlag(MACM_SHARED_MEMORY_GPU_ENTRY_FLAG.FAN_SPEED))
                     {
+                        Console.WriteLine($"Set parameter for GPU {gpuIndex}");
                         if (value.HasValue)
                         {
                             g.FanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.None;
@@ -110,76 +115,76 @@ namespace GpuFanControl
                             else if (targetValue > g.FanSpeedMax) g.FanSpeedCur = g.FanSpeedMax;
                             else g.FanSpeedCur = targetValue;
 
-                            Console.WriteLine($"G{g.Index} min:{g.FanSpeedMin} max:{g.FanSpeedMax} set:{g.FanSpeedCur}");
+                            Console.WriteLine($"G{gpuIndex} min:{g.FanSpeedMin} max:{g.FanSpeedMax} set:{g.FanSpeedCur}");
                         }
                         else
                         {
                             g.FanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.AUTO;
+                            Console.WriteLine($"G{gpuIndex} set:AUTO");
                         }
 
-                        Console.WriteLine("Success.");
-                    }
-                    catch (Exception e)
-                    {
-                        Console.WriteLine($"Failure to change parameter for GPU {g.Index}, {e.Message}");
-                        return;
-                    }
-
-                    Console.WriteLine($"Commiting Change For GPU {g.Index}");
-                    try
-                    {
-                        cm.CommitChanges(g.Index);
-                        Console.WriteLine($"Success.");
+                        cm.SetGpuEntry(g, gpuIndex);
                     }
-                    catch (Exception ex)
-                    {
-                        Console.WriteLine($"Failure Commit Change For GPU {g.Index}, {ex.Message}");
-                    }
-
-                    Console.WriteLine($"Reloading Control Memory...");
-                    try
-                    {
-                        cm.ReloadAll();
-                        Console.WriteLine($"Success.");
-                    }
-                    catch (Exception e)
+                    else
                     {
-                        Console.WriteLine($"Failure Reloading Control Memory, {e.Message}");
+                        Console.WriteLine($"No fan speed support on GPU {gpuIndex}");
                     }
                 }
 
                 if (index.HasValue)
                 {
-                    if (index < 0 || index > cm.GpuEntries.Length - 1)
+                    var g = cm.GetGpuEntry(index.Value, out var err);
+                    if (g.HasValue)
+                    {
+                        //set specified
+                        SetGpuFanSpeed(g.Value, index.Value);
+                    }
+                    else
                     {
                         Console.WriteLine("Argument Error: index out of GPU entries");
                         return -1;
                     }
-
-                    //set specified
-                    var g = cm.GpuEntries[index.Value];
-                    SetGpuFanSpeed(g);
                 }
                 else
                 {
                     //set all
-                    foreach (var g in cm.GpuEntries)
+                    for (int i = 0; i < cm.GetNumberOfGpu(); i++)
                     {
-                        SetGpuFanSpeed(g);
+                        var entry = cm.GetGpuEntry(i, out var err);
+                        if (entry.HasValue)
+                        {
+                            SetGpuFanSpeed(entry.Value, i);
+                        }
+                        else
+                        {
+                            Console.WriteLine($"{i}\tErr:{err}");
+                        }
                     }
                 }
+
+                Console.WriteLine("Commit changes...");
+                cm.Commit();
+                Console.WriteLine("Success.");
+                cm.Disconnect();
+            }
+            else
+            {
+                PrintUsage();
+                return -1;
             }
 
             return 0;
         }
 
-        private static ControlMemory ConnectToAfterburner(out int error)
+        private static MsiAfterBurnControlWrap ConnectToAfterburner(out int error)
         {
             Console.Write("Connecting to MSI Afterburner...");
 
             try
             {
-                var cm = new ControlMemory();
+                var cm = new MsiAfterBurnControlWrap();
+                cm.Connect();
+                cm.Refresh();
                 try
                 {
                     var l = Console.CursorLeft;

BIN
GpuFanControl/ReferenceAssemblies/MSIAfterburner.NET.chm


BIN
GpuFanControl/ReferenceAssemblies/MSIAfterburner.NET.dll