我正在使用GlobalMemoryStatusEx函数来检索有关内存的信息,但是此功能无法正常工作.它为所有属性返回0.我不认为这个功能在我的
Windows 7环境中有效.
[StructLayout(LayoutKind.Sequential)] internal struct MEMORYSTATUSEX { internal uint dwLength; internal uint dwMemoryLoad; internal ulong ullTotalPhys; internal ulong ullAvailPhys; internal ulong ullTotalPageFile; internal ulong ullAvailPageFile; internal ulong ullTotalVirtual; internal ulong ullAvailVirtual; internal ulong ullAvailExtendedVirtual; } [return: MarshalAs(UnmanagedType.Bool)] [DllImport("Kernel32.dll",CharSet = CharSet.Auto,SetLastError = true)] internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer); private void btnGlobalMemoryStatusEX_Click(object sender,EventArgs e) { MEMORYSTATUSEX statEX = new MEMORYSTATUSEX(); GlobalMemoryStatusEx(ref statEX); double d = (double)statEX.ullTotalPhys; }
解决方法
我发现我的错误:
http://www.pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html
http://www.pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html
我变了
internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
至
static extern bool GlobalMemoryStatusEx([In,Out] MEMORYSTATUSEX lpBuffer);
并改变了
GlobalMemoryStatusEx(ref statEX);
至
GlobalMemoryStatusEx(statEX);
它正常工作坦克