Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //AVCS SENS - AIDA64 External Applications Shared Memory Monitoring Function
- // -Gets new data string from memory every 2 seconds
- // by SemlerPDX Mar2022 (with much help from Pfeil!)
- // VETERANS-GAMING.COM
- using System;
- using System.Text;
- using System.Linq;
- using System.Windows.Forms;
- using System.IO.MemoryMappedFiles;
- using System.Runtime.InteropServices;
- using System.Collections.Generic;
- public class VAInline
- {
- double SharedMemTimerDelay = 2000;
- bool debugging = false;
- private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- if (VA.GetBoolean("AVCS_SENS_Test_SharedMem") == true)
- {
- SharedMemTest();
- VA.SetBoolean("AVCS_SENS_Test_SharedMem", false);
- if (VA.GetBoolean("AVCS_SENS_Test_OnlyTest") == true)
- VA.SetBoolean("AVCS_SENS_Monitoring", false);
- }
- else
- {
- SharedMemGet(debugging);
- }
- if (VA.GetBoolean("AVCS_SENS_Monitoring") != true)
- {
- (sender as System.Timers.Timer).Stop();
- VA.SetBoolean("AVCS_SENS_Logging", false);
- VA.WriteToLog("AVCS AIDA64 Shared Memory Monitor has been terminated...", "black");
- }
- if (VA.Stopped)
- {
- VA.SetBoolean("AVCS_SENS_Diagnostics_Active", false);
- VA.SetBoolean("AVCS_SENS_Diagnosing", false);
- VA.ResetStopFlag();
- }
- }
- private void SharedMemGet(bool debugging)
- {
- int? faultCount;
- try
- {
- if (VA.GetInt("~avcs_memfault_count") != null)
- {
- faultCount = VA.GetInt("~avcs_memfault_count");
- if (faultCount >= 2)
- throw new AVCSexception();
- }
- SharedMemSaver sf = new SharedMemSaver();
- sf.OpenView(VA);
- if (sf.GetData(VA) != "")
- {
- VA.SetText("AVCS_SENS_SHAREDMEM_XML", sf.GetData(VA));
- if (debugging || VA.GetBoolean("AVCS_SENS_DebugSensors") == true)
- VA.WriteToLog(sf.GetData(VA), "blank");
- }
- if (VA.GetBoolean("AVCS_SharedMem_Monitor_Startup") != null)
- {
- if (String.IsNullOrEmpty(VA.GetText("AVCS_SENS_SHAREDMEM_XML")))
- throw new AVCSexception();
- }
- }
- catch (AVCSexception e)
- {
- e.SharedMemGetException(VA);
- }
- }
- private void SharedMemTest()
- {
- int? faultCount;
- try
- {
- if (VA.GetInt("~avcs_memfault_count") != null)
- {
- faultCount = VA.GetInt("~avcs_memfault_count");
- if (faultCount >= 2)
- throw new AVCSexception();
- }
- SharedMemSaver sf = new SharedMemSaver();
- sf.OpenView(VA);
- if (sf.GetData(VA) != "")
- {
- VA.WriteToLog(sf.GetData(VA), "blank");
- VA.SetText("AVCS_SENS_SHAREDMEM_XML", sf.GetData(VA));
- VA.WriteToLog("AVCS AIDA64 Shared Memory Test has succeeded!", "green");
- VA.SetText("AVCS_SENS_Test_ReturnTTS", "Test succeeded.");
- }
- }
- catch (AVCSexception e)
- {
- e.SharedMemTestException(VA);
- }
- }
- private void SharedMemTimer()
- {
- System.Timers.Timer t = new System.Timers.Timer(SharedMemTimerDelay);
- t.Elapsed += t_Elapsed;
- VA.WriteToLog("AVCS AIDA64 Shared Memory Monitor is now running...", "black");
- t.Start();
- }
- public class AVCSexception : Exception
- {
- public void SharedMemTestException(dynamic VA)
- {
- VA.SetText("AVCS_SENS_SHAREDMEM_XML", null);
- VA.SetBoolean("AVCS_SENS_Logging", false);
- VA.SetBoolean("AVCS_SENS_Monitoring", false);
- VA.SetText("AVCS_SENS_Test_ReturnTTS", "Test failed. Please check settings.");
- VA.WriteToLog("AVCS AIDA64 Shared Memory Test has failed!", "red");
- VA.WriteToLog("Please check AIDA64 settings. See user guide for more information.", "blank");
- }
- public void SharedMemGetException(dynamic VA)
- {
- VA.SetText("AVCS_SENS_SHAREDMEM_XML", null);
- VA.SetBoolean("AVCS_SENS_Logging", false);
- VA.SetBoolean("AVCS_SENS_Monitoring", false);
- VA.WriteToLog("AVCS AIDA64 Shared Memory Monitor has failed to get data!", "red");
- VA.WriteToLog("Please check AIDA64 settings. See user guide for more information.", "blank");
- if (VA.GetBoolean("AVCS_SharedMem_Monitor_Startup") != null)
- {
- VA.SetText("AVCS_SENS_TTS_WILDCARD", "Sensor Monitor startup has failed. Please check settings.");
- VA.Command.Execute("F_SAY_TTS", false);
- }
- }
- }
- public void main()
- {
- if (VA.GetBoolean("AVCS_SENS_Monitoring") != true && VA.GetBoolean("AVCS_SharedMem_Monitor_Startup") != true)
- {
- VA.SetBoolean("AVCS_SharedMem_Monitor_Startup", true);
- SharedMemGet(debugging);
- if (String.IsNullOrEmpty(VA.GetText("AVCS_SENS_SHAREDMEM_XML")))
- {
- VA.SetText("AVCS_SENS_SHAREDMEM_XML", null);
- VA.SetBoolean("AVCS_SENS_Logging", false);
- VA.SetBoolean("AVCS_SENS_Monitoring", false);
- }
- else
- {
- VA.SetBoolean("AVCS_SENS_Monitoring", true);
- SharedMemTimer();
- }
- }
- VA.SetBoolean("AVCS_SharedMem_Monitor_Startup", null);
- }
- }
- class SharedMemSaver
- {
- #region Win32 API stuff
- public const int FILE_MAP_READ = 0x0004;
- private static dynamic VA;
- [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
- internal static extern IntPtr OpenFileMapping(int dwDesiredAccess,
- bool bInheritHandle, StringBuilder lpName);
- [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
- internal static extern IntPtr MapViewOfFile(IntPtr hFileMapping,
- int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow,
- int dwNumberOfBytesToMap);
- [DllImport("Kernel32.dll")]
- internal static extern bool UnmapViewOfFile(IntPtr map);
- [DllImport("kernel32.dll")]
- internal static extern bool CloseHandle(IntPtr hObject);
- #endregion
- private bool fileOpen = false;
- private IntPtr map;
- private IntPtr handle;
- ~SharedMemSaver()
- {
- CloseView(VA);
- }
- public bool OpenView(dynamic VA)
- {
- int? memfaultCount;
- try
- {
- if (!fileOpen)
- {
- StringBuilder sharedMemFile = new StringBuilder("AIDA64_SensorValues");
- handle = OpenFileMapping(FILE_MAP_READ, false, sharedMemFile);
- if (handle == IntPtr.Zero)
- {
- if (VA.GetInt("~avcs_memfault_count") != null)
- {
- memfaultCount = VA.GetInt("~avcs_memfault_count");
- memfaultCount += 1;
- VA.SetInt("~avcs_memfault_count", memfaultCount);
- }
- else
- {
- memfaultCount = 1;
- VA.SetInt("~avcs_memfault_count", memfaultCount);
- }
- throw new Exception("Unable to open file mapping.");
- }
- map = MapViewOfFile(handle, FILE_MAP_READ, 0, 0, 0);
- if (map == IntPtr.Zero)
- {
- if (VA.GetInt("~avcs_memfault_count") != null)
- {
- memfaultCount = VA.GetInt("~avcs_memfault_count");
- memfaultCount += 1;
- VA.SetInt("~avcs_memfault_count", memfaultCount);
- }
- else
- {
- memfaultCount = 1;
- VA.SetInt("~avcs_memfault_count", memfaultCount);
- }
- throw new Exception("Unable to read shared memory.");
- }
- fileOpen = true;
- }
- return fileOpen;
- }
- catch
- {
- return false;
- }
- }
- public void CloseView(dynamic VA)
- {
- if (fileOpen)
- {
- UnmapViewOfFile(map);
- CloseHandle(handle);
- }
- }
- public String GetData(dynamic VA)
- {
- if (fileOpen)
- {
- try
- {
- String data = (String)Marshal.PtrToStringAnsi(map);
- return data;
- }
- catch
- {
- return null;
- }
- }
- else
- {
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment