Skip to content

Instantly share code, notes, and snippets.

@yamamaya
Last active June 2, 2019 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamamaya/aabfa16daf35cee4059fc0b74c83b2d8 to your computer and use it in GitHub Desktop.
Save yamamaya/aabfa16daf35cee4059fc0b74c83b2d8 to your computer and use it in GitHub Desktop.
Retrieve hardware information from MSI Afterburner
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.MemoryMappedFiles;
namespace MAHMTest {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private const int MAX_PATH = 260;
private void Form1_Load( object sender, EventArgs e ) {
float? GpuTemperature = null;
float? CpuTemperature = null;
StringBuilder builder = new StringBuilder();
try {
using ( var mf = MemoryMappedFile.OpenExisting( "MAHMSharedMemory", MemoryMappedFileRights.FullControl ) )
using ( Stream view = mf.CreateViewStream() ) {
byte[] buff = new byte[ 32 ];
view.Read( buff, 0, buff.Length );
UInt32 dwSignature = BitConverter.ToUInt32( buff, 0 );
if ( dwSignature != 0x4D41484D ) {
throw new Exception( "Signature not found" );
}
UInt32 dwVersion = BitConverter.ToUInt32( buff, 4 );
builder.AppendLine( string.Format( "dwVersion: {0}.{1}", dwVersion >> 16, dwVersion & 0xffff ) );
UInt32 dwHeaderSize = BitConverter.ToUInt32( buff, 8 );
builder.AppendLine( string.Format( "dwHeaderSize: {0}", dwHeaderSize ) );
UInt32 dwNumEntries = BitConverter.ToUInt32( buff, 12 );
builder.AppendLine( string.Format( "dwNumEntries: {0}", dwNumEntries ) );
UInt32 dwEntrySize = BitConverter.ToUInt32( buff, 16 );
builder.AppendLine( string.Format( "dwEntrySize: {0}", dwEntrySize ) );
UInt32 time = BitConverter.ToUInt32( buff, 20 );
builder.AppendLine( string.Format( "time: {0}", time ) );
UInt32 dwNumGpuEntries = BitConverter.ToUInt32( buff, 24 );
builder.AppendLine( string.Format( "dwNumGpuEntries: {0}", dwNumGpuEntries ) );
UInt32 dwGpuEntrySize = BitConverter.ToUInt32( buff, 28 );
builder.AppendLine( string.Format( "dwGpuEntrySize: {0}", dwGpuEntrySize ) );
for ( int gpu = 0 ; gpu < dwNumGpuEntries ; gpu++ ) {
view.Seek( dwHeaderSize + dwNumEntries * dwEntrySize + gpu * dwGpuEntrySize, SeekOrigin.Begin );
byte[] GpuEntry = new byte[ dwGpuEntrySize ];
view.Read( GpuEntry, 0, (int)dwGpuEntrySize );
builder.AppendLine();
builder.AppendLine( string.Format( "GPU #{0}", gpu ) );
int index = 0;
string szGpuId = Encoding.ASCII.GetString( GpuEntry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szGpuId: {0}", szGpuId ) );
index += MAX_PATH;
string szFamily = Encoding.ASCII.GetString( GpuEntry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szFamily: {0}", szFamily ) );
index += MAX_PATH;
string szDevice = Encoding.ASCII.GetString( GpuEntry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szDevice: {0}", szDevice ) );
index += MAX_PATH;
string szDriver = Encoding.ASCII.GetString( GpuEntry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szDriver: {0}", szDriver ) );
index += MAX_PATH;
string szBIOS = Encoding.ASCII.GetString( GpuEntry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szBIOS: {0}", szBIOS ) );
index += MAX_PATH;
UInt32 dwMemAmount = BitConverter.ToUInt32( GpuEntry, index );
builder.AppendLine( string.Format( "dwMemAmount: {0}", dwMemAmount ) );
}
for ( int source = 0 ; source < dwNumEntries ; source++ ) {
view.Seek( dwHeaderSize + source * dwEntrySize, SeekOrigin.Begin );
byte[] Entry = new byte[ dwEntrySize ];
view.Read( Entry, 0, (int)dwEntrySize );
builder.AppendLine();
builder.AppendLine( string.Format( "Source #{0}", source ) );
int index = 0;
string szSrcName = Encoding.ASCII.GetString( Entry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szSrcName: {0}", szSrcName ) );
index += MAX_PATH;
string szSrcUnits = Encoding.ASCII.GetString( Entry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szSrcUnits: {0}", szSrcUnits ) );
index += MAX_PATH;
//string szLocalizedSrcName = Encoding.GetEncoding( "shift_jis" ).GetString( Entry, index, MAX_PATH ).TrimEnd( '\0' ); // for Japanese
string szLocalizedSrcName = Encoding.ASCII.GetString( Entry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szLocalizedSrcName: {0}", szLocalizedSrcName ) );
index += MAX_PATH;
//string szLocalizedSrcUnits = Encoding.GetEncoding( "shift_jis" ).GetString( Entry, index, MAX_PATH ).TrimEnd( '\0' ); // for Japanese
string szLocalizedSrcUnits = Encoding.ASCII.GetString( Entry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szLocalizedSrcUnits: {0}", szLocalizedSrcUnits ) );
index += MAX_PATH;
string szRecommendedFormat = Encoding.ASCII.GetString( Entry, index, MAX_PATH ).TrimEnd( '\0' );
builder.AppendLine( string.Format( "szRecommendedFormat: {0}", szRecommendedFormat ) );
index += MAX_PATH;
float data = BitConverter.ToSingle( Entry, index );
builder.AppendLine( string.Format( "data: {0}", data ) );
index += 4;
float minLimit = BitConverter.ToSingle( Entry, index );
builder.AppendLine( string.Format( "minLimit: {0}", minLimit ) );
index += 4;
float maxLimit = BitConverter.ToSingle( Entry, index );
builder.AppendLine( string.Format( "maxLimit: {0}", maxLimit ) );
index += 4;
UInt32 dwFlags = BitConverter.ToUInt32( Entry, index );
builder.AppendLine( string.Format( "dwFlags: {0:X8}", dwFlags ) );
index += 4;
UInt32 dwGpu = BitConverter.ToUInt32( Entry, index );
builder.AppendLine( string.Format( "dwGpu: {0:X8}", dwGpu ) );
index += 4;
UInt32 dwSrcId = BitConverter.ToUInt32( Entry, index );
builder.AppendLine( string.Format( "dwSrcId: {0:X8}", dwSrcId ) );
index += 4;
if ( dwSrcId == 0x80 ) {
CpuTemperature = data;
} else if ( dwSrcId == 0x00 ) {
GpuTemperature = data;
}
}
}
} catch ( Exception ex ) {
builder.AppendLine( ex.Message );
}
textBox1.Text = builder.ToString();
textBox1.SelectionStart = 0;
textBoxCpuTemperature.Text = CpuTemperature.ToString();
textBoxGpuTemperature.Text = GpuTemperature.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment