Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GlobalMemoryStatusEx (kernel32)
 
.
Summary

C# Signature:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport(kernel32, CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);

User-Defined Types:

MEMORYSTATUSEX

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

private void DisplayMemory()

        {
            string[] Drives = System.IO.Directory.GetLogicalDrives();
            long tm = System.GC.GetTotalMemory(true);
            UInt64 UserAvailable=0,Capacity=0,Available=0;
            if(Drives!=null)
            {
                int retval = GetDiskFreeSpaceExA(Drives[0],ref UserAvailable,ref Capacity,ref Available);
                this.lblDiskFree.Text = Available.ToString();
                this.lblDiskTotal.Text = Capacity.ToString();
            }
            MEMORYSTATUSEX msex=new MEMORYSTATUSEX();
            int retval2 = GlobalMemoryStatusEx(msex);
            this.lblMemoryFree.Text = msex.ullAvailPhys.ToString();
            this.lblMemoryTotal.Text = msex.ullTotalPhys.ToString();
        }

        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
        private class MEMORYSTATUSEX
        {
                  public uint dwLength;
            public uint dwMemoryLoad;
                  public ulong ullTotalPhys;
                  public ulong ullAvailPhys;
                  public ulong ullTotalPageFile;
                  public ulong ullAvailPageFile;
                  public ulong ullTotalVirtual;
                  public ulong ullAvailVirtual;
                  public ulong ullAvailExtendedVirtual;
                  public MEMORYSTATUSEX()
            {
                      this.dwLength = (uint) Marshal.SizeOf(typeof(NativeMethods.MEMORYSTATUSEX));
            }
        }


        [DllImport("kernel32.dll")]
        private static extern int GetDiskFreeSpaceExA(  string lpDirectoryName,
            ref System.UInt64 lpFreeBytesAvailable,
            ref System.UInt64 lpTotalNumberOfBytes,
            ref System.UInt64 lpTotalNumberOfFreeBytes
            );

        [DllImport("kernel32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         [DllImport(kernel32, CharSet = CharSet.Auto, SetLastError = true)]
         static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions