Upon revisiting the issue it seems that the issue was due to the buffer size I was using. The "AccessViolationException" is probably from trying to read past the end of the file.
I guess that changes the question to: How do I get the length of the string? Alternatively, what's the max buffer size? The help doc said 10KB, but that seems to be what's causing the exception. A smaller value (3K) works and I can read the XML string, but how do I know for sure I've gotten the entire file?
internal const string SHARED_MEM_NAME = "AIDA64_SensorValues";
internal const int FILE_MAP_READ = 0x0004;
internal const int BUFFER_SIZE = 3000;
public static SensorData GetData()
{
// Read sensor values from shared memory
StringBuilder sharedMemFile = new StringBuilder(SHARED_MEM_NAME);
IntPtr handle = Kernel32.OpenFileMapping(FILE_MAP_READ, false, sharedMemFile);
IntPtr mem = Kernel32.MapViewOfFile(handle, FILE_MAP_READ, 0, 0, 0);
if (mem == IntPtr.Zero)
{
throw new Exception("Unable to read shared memory.");
}
string xml = Marshal.PtrToStringAnsi(mem, BUFFER_SIZE);
Console.WriteLine(xml);
Kernel32.UnmapViewOfFile(handle);
Kernel32.CloseHandle(handle);
return null;
}