Skip to content

Commit ef651cd

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

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
@@ -944,5 +944,23 @@ internal static FILE_FLAGS_AND_ATTRIBUTES GetFileAttributes(string lpFileName)
944944
uint res = PInvoke.GetFileAttributes(lpFileName);
945945
return res == PInvoke.INVALID_FILE_ATTRIBUTES ? throw ExceptionUtilities.GetExceptionForLastWin32Error() : (FILE_FLAGS_AND_ATTRIBUTES)res;
946946
}
947+
948+
/// <summary>
949+
/// Retrieves information about the system's current usage of both physical and virtual memory.
950+
/// </summary>
951+
/// <remarks>This method wraps the native GlobalMemoryStatusEx function. The output parameter must
952+
/// not be used until the method returns successfully. If the call fails, an exception is thrown.</remarks>
953+
/// <param name="lpBuffer">When this method returns, contains a structure that receives information about current memory availability.
954+
/// The structure's fields are populated with memory status data.</param>
955+
/// <returns>true if the memory status information was successfully retrieved; otherwise, false.</returns>
956+
internal static BOOL GlobalMemoryStatusEx(out MEMORYSTATUSEX lpBuffer)
957+
{
958+
lpBuffer = new MEMORYSTATUSEX
959+
{
960+
dwLength = (uint)Marshal.SizeOf<MEMORYSTATUSEX>()
961+
};
962+
BOOL res = PInvoke.GlobalMemoryStatusEx(ref lpBuffer);
963+
return !res ? throw ExceptionUtilities.GetExceptionForLastWin32Error() : res;
964+
}
947965
}
948966
}

src/PSADT/PSADT.LibraryInterfaces/NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ GetTokenInformation
6565
GetWindowText
6666
GetWindowTextLength
6767
GetWindowThreadProcessId
68+
GlobalMemoryStatusEx
6869
HIWORD
6970
HKEY_CLASSES_ROOT
7071
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)