Skip to content

Commit f8562a0

Browse files
committed
Add in C#-based method to retrieve total system memory as part of CIM code removal.
1 parent 240570b commit f8562a0

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/PSADT/PSADT.LibraryInterfaces/Kernel32.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,5 +867,23 @@ internal static FILE_TYPE GetFileType(SafeHandle hFile)
867867
? throw ExceptionUtilities.GetExceptionForLastWin32Error((WIN32_ERROR)lastWin32Error)
868868
: res;
869869
}
870+
871+
/// <summary>
872+
/// Retrieves information about the system's current usage of both physical and virtual memory.
873+
/// </summary>
874+
/// <remarks>This method wraps the native GlobalMemoryStatusEx function. The output parameter must
875+
/// not be used until the method returns successfully. If the call fails, an exception is thrown.</remarks>
876+
/// <param name="lpBuffer">When this method returns, contains a structure that receives information about current memory availability.
877+
/// The structure's fields are populated with memory status data.</param>
878+
/// <returns>true if the memory status information was successfully retrieved; otherwise, false.</returns>
879+
internal static BOOL GlobalMemoryStatusEx(out MEMORYSTATUSEX lpBuffer)
880+
{
881+
lpBuffer = new MEMORYSTATUSEX
882+
{
883+
dwLength = (uint)Marshal.SizeOf<MEMORYSTATUSEX>()
884+
};
885+
BOOL res = PInvoke.GlobalMemoryStatusEx(ref lpBuffer);
886+
return !res ? throw ExceptionUtilities.GetExceptionForLastWin32Error() : res;
887+
}
870888
}
871889
}

src/PSADT/PSADT.LibraryInterfaces/NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ GetTokenInformation
6161
GetWindowText
6262
GetWindowTextLength
6363
GetWindowThreadProcessId
64+
GlobalMemoryStatusEx
6465
HIWORD
6566
HKEY_CLASSES_ROOT
6667
HKEY_CURRENT_CONFIG

src/PSADT/PSADT/DeviceManagement/DeviceUtilities.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Windows.Win32.Foundation;
99
using Windows.Win32.Media.Audio;
1010
using Windows.Win32.System.Com;
11+
using Windows.Win32.System.SystemInformation;
1112

1213
namespace PSADT.DeviceManagement
1314
{
@@ -103,5 +104,15 @@ public static DomainStatus GetDomainStatus()
103104
return new((NETSETUP_JOIN_STATUS)bufferType, nameBuffer.ToStringUni());
104105
}
105106
}
107+
108+
/// <summary>
109+
/// Retrieves the total amount of physical memory installed on the system, in bytes.
110+
/// </summary>
111+
/// <returns>The total physical memory, in bytes, available on the system.</returns>
112+
public static ulong GetTotalSystemMemory()
113+
{
114+
_ = Kernel32.GlobalMemoryStatusEx(out MEMORYSTATUSEX lpBuffer);
115+
return lpBuffer.ullTotalPhys;
116+
}
106117
}
107118
}

0 commit comments

Comments
 (0)