Skip to content

Commit af7d035

Browse files
mjr4077auDanGough
authored andcommitted
Add in C#-based method to retrieve total system memory as part of CIM code removal.
1 parent 3bd7236 commit af7d035

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/PSADT/PSADT.Interop/NativeMethods.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,6 +3757,24 @@ internal static WIN32_ERROR NetGetJoinInformation(out SafeNetApiBufferFreeHandle
37573757
return NetGetJoinInformation(null, out lpNameBuffer, out BufferType);
37583758
}
37593759

3760+
/// <summary>
3761+
/// Retrieves information about the system's current usage of both physical and virtual memory.
3762+
/// </summary>
3763+
/// <remarks>This method wraps the native GlobalMemoryStatusEx function. The output parameter must
3764+
/// not be used until the method returns successfully. If the call fails, an exception is thrown.</remarks>
3765+
/// <param name="lpBuffer">When this method returns, contains a structure that receives information about current memory availability.
3766+
/// The structure's fields are populated with memory status data.</param>
3767+
/// <returns>true if the memory status information was successfully retrieved; otherwise, false.</returns>
3768+
internal static BOOL GlobalMemoryStatusEx(out MEMORYSTATUSEX lpBuffer)
3769+
{
3770+
lpBuffer = new MEMORYSTATUSEX
3771+
{
3772+
dwLength = (uint)Marshal.SizeOf<MEMORYSTATUSEX>()
3773+
};
3774+
BOOL res = PInvoke.GlobalMemoryStatusEx(ref lpBuffer);
3775+
return !res ? throw ExceptionUtilities.GetExceptionForLastWin32Error() : res;
3776+
}
3777+
37603778
/// <summary>
37613779
/// Lookup table for system information class struct sizes.
37623780
/// </summary>

src/PSADT/PSADT.Interop/NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ GetTokenInformation
6666
GetWindowText
6767
GetWindowTextLength
6868
GetWindowThreadProcessId
69+
GlobalMemoryStatusEx
6970
HIWORD
7071
HKEY_CLASSES_ROOT
7172
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
{
@@ -143,5 +144,15 @@ public static DomainStatus GetDomainStatus()
143144
return new((NETSETUP_JOIN_STATUS)bufferType, nameBuffer.ToStringUni());
144145
}
145146
}
147+
148+
/// <summary>
149+
/// Retrieves the total amount of physical memory installed on the system, in bytes.
150+
/// </summary>
151+
/// <returns>The total physical memory, in bytes, available on the system.</returns>
152+
public static ulong GetTotalSystemMemory()
153+
{
154+
_ = NativeMethods.GlobalMemoryStatusEx(out MEMORYSTATUSEX lpBuffer);
155+
return lpBuffer.ullTotalPhys;
156+
}
146157
}
147158
}

0 commit comments

Comments
 (0)