Parameter in C#

前端 未结 4 541
后悔当初
后悔当初 2021-01-26 09:15

When I want get total value of memory in C# I found a kernel32 function in MSDN to invoke data from system. MSDN declare function this way:

[return: MarshalAs(Un         


        
4条回答
  •  天涯浪人
    2021-01-26 09:54

    The following definition works (define the MEMORYSTATUSEX as a class):

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GlobalMemoryStatusEx(MEMORYSTATUSEX lpBuffer);
    
    [StructLayout(LayoutKind.Sequential)]
    public sealed class MEMORYSTATUSEX {
      public uint dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
      public uint dwMemoryLoad;
      public ulong ullTotalPhys;
      public ulong ullAvailPhys;
      public ulong ullTotalPageFile;
      public ulong ullAvailPageFile;
      public ulong ullTotalVirtual;
      public ulong ullAvailVirtual;
      public ulong ullAvailExtendedVirtual;
    }
    

    Usage

    var status = new MEMORYSTATUSEX();
    GlobalMemoryStatusEx(status);
    

提交回复
热议问题