views:

1293

answers:

5

hi all I using GlobalMemoryStatusEx function to retrieves information about memory, but this function don't work correctly, it return 0 for all properies. I have windows 7, I think this function don't work in WIN 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;
    }

Can any body tell me that where I wirte wrong code? tanx

A: 

Hello, you can use this templates:

long memory = Process.GetCurrentProcess().PeakVirtualMemorySize64;

And another properties with names Peak*64

Manushin Igor
I believe the OP is asking about physical memory, so Process isn't that useful.
Brian Rasmussen
+4  A: 

How about:

My.Computer.Info.TotalPhysicalMemory
My.Computer.Info.AvailablePhysicalMemory
Bobby
tanks, but I write in C#.Net.
Amir Borzoei
@Amir: Add a reference to Microsoft.VisualBasic.dll.
dalle
@Amir, sorry, I always forget that the My-Namespace does not exist 'natively' in C#. :(
Bobby
@Amir: you can still access the property: add a reference to "Microsoft.VisualBasic", then create an instance of the type `Microsoft.VisualBasic.Devices.ComputerInfo`. That corresponds to `My.Computer.Info` in @Bobby's answer.
Fredrik Mörk
A: 

If c# you can:

Reference the Microsoft.VisualBasic assembly. Then import Microsoft.VisualBasic.Devices namespace.
And finally use ComputerInfo to get the total physical memory.

ComputerInfo myCompInfo = new ComputerInfo();
string physicalMemory = "Physical Memory: " + myCompInfo.TotalPhysicalMemory / 1000000 + " MB";
Mez
+1  A: 

I find my mistake from: http://www.pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html

I Changed

internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);

To

static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);

and changed

GlobalMemoryStatusEx(ref statEX);

To

GlobalMemoryStatusEx(statEX);

It work correctly. Tanks

Amir Borzoei
A: 

You forgot to set statEX.dwLength before calling GlobalMemoryStatusEx.

MEMORYSTATUSEX statEX = new MEMORYSTATUSEX();
statEX.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
GlobalMemoryStatusEx(ref statEX);
dalle
I set this but it don't work, neither.
Amir Borzoei
Ok, we should set this but the main problem is "ref" keyword. You should remove it from delaretion and usage of function.
Amir Borzoei