Skip to content

Commit

Permalink
both x64 and x86 are supported with the same build.
Browse files Browse the repository at this point in the history
  • Loading branch information
vision57 committed Jul 21, 2021
1 parent caa1129 commit 7ed295f
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 25 deletions.
Binary file added Dlls/GpuzShMem.Win32.dll
Binary file not shown.
Binary file added Dlls/GpuzShMem.Win32.pdb
Binary file not shown.
Binary file renamed Dlls/GpuzShMem.dll → Dlls/GpuzShMem.x64.dll
Binary file not shown.
Binary file added Dlls/GpuzShMem.x64.pdb
Binary file not shown.
14 changes: 11 additions & 3 deletions FanControl.GPUZ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>Dlls\FanControl.Plugins.dll</HintPath>
</Reference>
<Reference Include="GpuzShMem">
<HintPath>Dlls\GpuzShMem.dll</HintPath>
<Reference Include="GpuzShMem.Win32, Version=0.0.0.0, Culture=neutral, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<HintPath>Dlls\GpuzShMem.Win32.dll</HintPath>
</Reference>
<Reference Include="GpuzShMem.x64, Version=0.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>Dlls\GpuzShMem.x64.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -59,7 +64,10 @@
<ItemGroup>
<Content Include="Dlls\FanControl.Plugins.dll" />
<Content Include="Dlls\FanControl.Plugins.xml" />
<Content Include="Dlls\GpuzShMem.dll" />
<Content Include="Dlls\GpuzShMem.Win32.dll" />
<Content Include="Dlls\GpuzShMem.Win32.pdb" />
<Content Include="Dlls\GpuzShMem.x64.dll" />
<Content Include="Dlls\GpuzShMem.x64.pdb" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Expand Down
138 changes: 116 additions & 22 deletions GpuzWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,129 @@ Here is a short overview of all fields accessible via the shared memory.
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace GpuzDemo
{
class AMD64
{
[DllImport(@"GpuzShMem.x64.dll", SetLastError = true)]
public static extern int InitGpuzShMem();

[DllImport(@"GpuzShMem.x64.dll", SetLastError = true)]
public static extern int RemGpuzShMem();

[DllImport(@"GpuzShMem.x64.dll", SetLastError = true)]
public static extern IntPtr GetSensorName(int index);

[DllImport(@"GpuzShMem.x64.dll", SetLastError = true)]
public static extern double GetSensorValue(int index);

[DllImport(@"GpuzShMem.x64.dll", SetLastError = true)]
public static extern IntPtr GetSensorUnit(int index);

[DllImport(@"GpuzShMem.x64.dll", SetLastError = true)]
public static extern IntPtr GetDataKey(int index);

[DllImport(@"GpuzShMem.x64.dll", SetLastError = true)]
public static extern IntPtr GetDataValue(int index);
}

class Win32
{
[DllImport(@"GpuzShMem.Win32.dll", SetLastError = true)]
public static extern int InitGpuzShMem();

[DllImport(@"GpuzShMem.Win32.dll", SetLastError = true)]
public static extern int RemGpuzShMem();

[DllImport(@"GpuzShMem.Win32.dll", SetLastError = true)]
public static extern IntPtr GetSensorName(int index);

[DllImport(@"GpuzShMem.Win32.dll", SetLastError = true)]
public static extern double GetSensorValue(int index);

[DllImport(@"GpuzShMem.Win32.dll", SetLastError = true)]
public static extern IntPtr GetSensorUnit(int index);

[DllImport(@"GpuzShMem.Win32.dll", SetLastError = true)]
public static extern IntPtr GetDataKey(int index);

[DllImport(@"GpuzShMem.Win32.dll", SetLastError = true)]
public static extern IntPtr GetDataValue(int index);
}

public class GpuzWrapper
{
[DllImport(@"GpuzShMem.dll")]
private static extern int InitGpuzShMem();
[DllImport(@"GpuzShMem.dll")]
private static extern int RemGpuzShMem();
[DllImport(@"GpuzShMem.dll")]
private static extern IntPtr GetSensorName(int index);
[DllImport(@"GpuzShMem.dll")]
private static extern double GetSensorValue(int index);
[DllImport(@"GpuzShMem.dll")]
private static extern IntPtr GetSensorUnit(int index);
[DllImport(@"GpuzShMem.dll")]
private static extern IntPtr GetDataKey(int index);
[DllImport(@"GpuzShMem.dll")]
private static extern IntPtr GetDataValue(int index);

delegate int InitGpuzShMem();

static readonly InitGpuzShMem initGpuzShMem;

delegate int RemGpuzShMem();

static readonly RemGpuzShMem remGpuzShMem;

delegate IntPtr GetSensorName(int index);

static readonly GetSensorName getSensorName;

delegate double GetSensorValue(int index);

static readonly GetSensorValue getSensorValue;

delegate IntPtr GetSensorUnit(int index);

static readonly GetSensorUnit getSensorUnit;

delegate IntPtr GetDataKey(int index);

static readonly GetDataKey getDataKey;

delegate IntPtr GetDataValue(int index);

static readonly GetDataValue getDataValue;

static GpuzWrapper()
{
if (Environment.Is64BitOperatingSystem)
{
initGpuzShMem = AMD64.InitGpuzShMem;
remGpuzShMem = AMD64.RemGpuzShMem;
getSensorName = AMD64.GetSensorName;
getSensorValue = AMD64.GetSensorValue;
getSensorUnit = AMD64.GetSensorUnit;
getDataKey = AMD64.GetDataKey;
getDataValue = AMD64.GetDataValue;
} else
{
initGpuzShMem = Win32.InitGpuzShMem;
remGpuzShMem = Win32.RemGpuzShMem;
getSensorName = Win32.GetSensorName;
getSensorValue = Win32.GetSensorValue;
getSensorUnit = Win32.GetSensorUnit;
getDataKey = Win32.GetDataKey;
getDataValue = Win32.GetDataValue;
}
}


/// <summary>
/// Opens the shared memory interface for reading. Don't forget to close it if you don't need it anymore!
/// </summary>
/// <exception cref="Exception">If the shared memory could not be opened.</exception>
public void Open()
{
if (InitGpuzShMem() != 0)

if (initGpuzShMem() != 0)
{
throw new Exception("An error occured while opening the GPUZ shared memory!");
var ec = Marshal.GetLastWin32Error();
if (ec == 0x2)
{
throw new Win32Exception(ec, "Ensure GPU-Z is running, and then restart FanControl.");
}

throw new Win32Exception(ec);
}
}

Expand All @@ -93,7 +187,7 @@ public void Open()
/// </summary>
public void Close()
{
RemGpuzShMem();
remGpuzShMem();
}


Expand All @@ -104,7 +198,7 @@ public void Close()
/// <returns>Name of the sensor field.</returns>
public string SensorName(int index)
{
return Marshal.PtrToStringUni(GetSensorName(index));
return Marshal.PtrToStringUni(getSensorName(index));
}


Expand All @@ -115,7 +209,7 @@ public string SensorName(int index)
/// <returns>Value of the sensor field.</returns>
public double SensorValue(int index)
{
return GetSensorValue(index);
return getSensorValue(index);
}


Expand All @@ -126,7 +220,7 @@ public double SensorValue(int index)
/// <returns>Unit of the sensor field.</returns>
public string SensorUnit(int index)
{
return Marshal.PtrToStringUni(GetSensorUnit(index));
return Marshal.PtrToStringUni(getSensorUnit(index));
}


Expand All @@ -137,7 +231,7 @@ public string SensorUnit(int index)
/// <returns>Key of the data field.</returns>
public string DataKey(int index)
{
return Marshal.PtrToStringUni(GetDataKey(index));
return Marshal.PtrToStringUni(getDataKey(index));
}


Expand All @@ -148,7 +242,7 @@ public string DataKey(int index)
/// <returns>Value of the data field.</returns>
public string DataValue(int index)
{
return Marshal.PtrToStringUni(GetDataValue(index));
return Marshal.PtrToStringUni(getDataValue(index));
}

/// <summary>
Expand Down

0 comments on commit 7ed295f

Please sign in to comment.