Skip to content

Instantly share code, notes, and snippets.

@Seraksab
Created November 29, 2022 20:46
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 Seraksab/c275d9b5963ea1178e5ca43bd6b06433 to your computer and use it in GitHub Desktop.
Save Seraksab/c275d9b5963ea1178e5ca43bd6b06433 to your computer and use it in GitHub Desktop.
.NET reader to parse the content of the shared memory file published by Aida64
using System;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Text;
using System.Xml;
namespace Aida64SharedMemoryReader;
/// <summary>
/// Reads and parses the content of the shared memory file published by Aida64.
/// https://aida64.co.uk/user-manual/file-menu/preferences/hardware-monitoring/external-applications
/// </summary>
public static class Aida64SharedMemoryReader
{
private const string SharedMemoryFileName = "Global\\AIDA64_SensorValues";
public static IEnumerable<SensorReading> Read()
{
using var mmf = MemoryMappedFile.OpenExisting(SharedMemoryFileName, MemoryMappedFileRights.Read);
using var accessor = mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);
Span<byte> bytes = stackalloc byte[(int)accessor.Capacity];
accessor.SafeMemoryMappedViewHandle.ReadSpan(0, bytes);
var endIdx = bytes.IndexOf(Convert.ToByte('\x00'));
var sharedMemoryXml = Encoding.ASCII.GetString(bytes[..endIdx]);
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml($"<root>{sharedMemoryXml}</root>");
if (xmlDoc.FirstChild == null) yield break;
foreach (XmlNode node in xmlDoc.FirstChild.ChildNodes)
{
yield return new SensorReading(
node.SelectSingleNode("id")?.InnerText.Trim(),
node.SelectSingleNode("label")?.InnerText.Trim(),
node.SelectSingleNode("value")?.InnerText.Trim(),
node.Name.Trim()
);
}
}
}
public readonly record struct SensorReading(
string Id,
string Label,
string Value,
string Type
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment