11using PLP_SystemInfo . Collections ;
22using System ;
33using System . Management ;
4+ using System . Runtime . InteropServices ;
45
56namespace PLP_SystemInfo . ComponentInfo
67{
78 public class RamInfo
89 {
910 //Ram
11+ [ StructLayout ( LayoutKind . Sequential , CharSet = CharSet . Auto ) ]
12+ private class MEMORYSTATUSEX
13+ {
14+ public uint dwLength ;
15+ public uint dwMemoryLoad ;
16+ public ulong ullTotalPhys ;
17+ public ulong ullAvailPhys ;
18+ public ulong ullTotalPageFile ;
19+ public ulong ullAvailPageFile ;
20+ public ulong ullTotalVirtual ;
21+ public ulong ullAvailVirtual ;
22+ public ulong ullAvailExtendedVirtual ;
23+
24+ public MEMORYSTATUSEX ( )
25+ {
26+ dwLength = ( uint ) Marshal . SizeOf ( typeof ( MEMORYSTATUSEX ) ) ;
27+ }
28+ }
29+
30+ [ DllImport ( "kernel32.dll" , CharSet = CharSet . Auto , SetLastError = true ) ]
31+ [ return : MarshalAs ( UnmanagedType . Bool ) ]
32+ private static extern bool GlobalMemoryStatusEx ( [ In , Out ] MEMORYSTATUSEX lpBuffer ) ;
33+
34+ private static double ConvertBytesToGB ( ulong bytes )
35+ {
36+ return Math . Round ( bytes / ( 1024.0 * 1024.0 * 1024.0 ) , 3 ) ;
37+ }
38+
39+ private static void GetMemoryStatus ( out double totalMemoryGB , out double availableMemoryGB )
40+ {
41+ var memoryStatus = new MEMORYSTATUSEX ( ) ;
42+ if ( GlobalMemoryStatusEx ( memoryStatus ) )
43+ {
44+ totalMemoryGB = ConvertBytesToGB ( memoryStatus . ullTotalPhys ) ;
45+ availableMemoryGB = ConvertBytesToGB ( memoryStatus . ullAvailPhys ) ;
46+ }
47+ else
48+ {
49+ throw new InvalidOperationException ( "Failed to retrieve memory status." ) ;
50+ }
51+ }
52+
53+ /// <summary>
54+ /// Returns a long value of the installed GB of RAM.
55+ /// </summary>
56+ /// <returns></returns>
1057 public static long GetInstalledRAMSize ( )
1158 {
1259 var oMs = new ManagementScope ( ) ;
@@ -29,21 +76,52 @@ public static long GetInstalledRAMSize()
2976 return memSize ;
3077 }
3178
32- public static long GetTotalUsableRam ( )
79+ /// <summary>
80+ /// Returns the total useable ram in GB.
81+ /// </summary>
82+ /// <returns></returns>
83+ public static double GetTotalUsableRam ( )
84+ {
85+ double memTotal , memAvailable ;
86+ GetMemoryStatus ( out memTotal , out memAvailable ) ;
87+ return memTotal ;
88+ }
89+
90+ /// <summary>
91+ /// Returns the harware reserved ram in MB.
92+ /// </summary>
93+ /// <returns></returns>
94+ public static double GetHardwareReservedRam ( )
3395 {
34- throw new NotImplementedException ( ) ;
96+ return Math . Round ( ( GetInstalledRAMSize ( ) - GetTotalUsableRam ( ) ) * 1000 ) ;
3597 }
3698
37- public static long GetRamInUse ( )
99+ /// <summary>
100+ /// Returns the used ram in GB.
101+ /// </summary>
102+ /// <returns></returns>
103+ public static double GetRamInUse ( )
38104 {
39- throw new NotImplementedException ( ) ;
105+ double memTotal , memAvailable ;
106+ RamInfo . GetMemoryStatus ( out memTotal , out memAvailable ) ;
107+ return memTotal - memAvailable ;
40108 }
41109
42- public static long GetAvailableRam ( )
110+ /// <summary>
111+ /// Returns the available ram.
112+ /// </summary>
113+ /// <returns></returns>
114+ public static double GetAvailableRam ( )
43115 {
44- throw new NotImplementedException ( ) ;
116+ double memTotal , memAvailable ;
117+ RamInfo . GetMemoryStatus ( out memTotal , out memAvailable ) ;
118+ return memAvailable ;
45119 }
46120
121+ /// <summary>
122+ /// Returns a collection of type **RAM** containing information for each installed ram module such as manufacturer, frequency, voltage and capacity.
123+ /// </summary>
124+ /// <returns></returns>
47125 public static RamCollection GetRamInfo ( )
48126 {
49127 RamCollection rams = new RamCollection ( ) ;
0 commit comments