Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows CE上GlobalMemoryStatusEx的PInvoke_C#_Compact Framework_Windows Ce - Fatal编程技术网

C# Windows CE上GlobalMemoryStatusEx的PInvoke

C# Windows CE上GlobalMemoryStatusEx的PInvoke,c#,compact-framework,windows-ce,C#,Compact Framework,Windows Ce,如果您不使用或不了解Windows CE和Compact Framework,请不要否决此建议。谢谢 有人知道WinCE上是否有此功能,如果有,DLL是什么吗?我试着从coredll.dll和kernel.dll中PInvoke这个。Win32版本来自kernel32.dll [return: MarshalAs(UnmanagedType.Bool)] [DllImport("kernel.dll")] // and "coredll.dll" also doesn't work public

如果您不使用或不了解Windows CE和Compact Framework,请不要否决此建议。谢谢

有人知道WinCE上是否有此功能,如果有,DLL是什么吗?我试着从coredll.dll和kernel.dll中PInvoke这个。Win32版本来自kernel32.dll

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel.dll")] // and "coredll.dll" also doesn't work
public static extern bool GlobalMemoryStatusEx([In,Out] MEMORYSTATUSEX lpBuffer);
当我尝试使用上面的函数时失败了,异常是找不到PInvoke DLL“kernel.DLL”

PS:我使用了大量Pinvoke函数,例如:

[DllImport("coredll.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int nMsg, IntPtr wParam, IntPtr lParam);

只有电话,不是扩展版的。它位于coredll.dll中,请参见

只有调用,没有扩展的…Ex版本。它位于coredll.dll中,请参见

我们已经实现了C.Evenhuis建议的答案,并且运行良好。以下是将来可能需要此功能的任何人的代码:

// Structure to allow getting memory usage
public struct MEMORYSTATUS
{
        public int nLength;
        public int nMemoryLoad;
        public uint uTotalPhys;
        public uint uAvailPhys;
        public uint uTotalPageFile;
        public uint uAvailPageFile;
        public uint uTotalVirtual;
        public uint uAvailVirtual;
}

[DllImport("coredll", EntryPoint="GlobalMemoryStatus", SetLastError = false)]
public static extern void GlobalMemoryStatus(out MEMORYSTATUS memCE);

MEMORYSTATUS mem = new MEMORYSTATUS();
mem.nLength = Marshal.SizeOf(typeof(MEMORYSTATUS));
GlobalMemoryStatus(out mem);
// Label1.Text = (mem.uAvailPhys.ToString() + "Bytes");

我们已经实现了C.Evenhuis建议的答案,并且效果良好。以下是将来可能需要此功能的任何人的代码:

// Structure to allow getting memory usage
public struct MEMORYSTATUS
{
        public int nLength;
        public int nMemoryLoad;
        public uint uTotalPhys;
        public uint uAvailPhys;
        public uint uTotalPageFile;
        public uint uAvailPageFile;
        public uint uTotalVirtual;
        public uint uAvailVirtual;
}

[DllImport("coredll", EntryPoint="GlobalMemoryStatus", SetLastError = false)]
public static extern void GlobalMemoryStatus(out MEMORYSTATUS memCE);

MEMORYSTATUS mem = new MEMORYSTATUS();
mem.nLength = Marshal.SizeOf(typeof(MEMORYSTATUS));
GlobalMemoryStatus(out mem);
// Label1.Text = (mem.uAvailPhys.ToString() + "Bytes");
我是这样用的

/// <summary>
/// https://msdn.microsoft.com/en-us/library/ee488368.aspx
/// This structure contains information about current memory availability. The GlobalMemoryStatus function uses this structure.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MemoryStatus
{
    /// <summary>
    /// Specifies the size, in bytes, of the MEMORYSTATUS structure.
    /// Set this member to sizeof(MEMORYSTATUS) when passing it to the GlobalMemoryStatus function.
    /// </summary>
    public int Length;
    /// <summary>
    /// Specifies a number between zero and 100 that gives a general idea of current memory use, in which zero indicates no memory use and 100 indicates full memory use.
    /// </summary>
    public uint MemoryLoad;
    /// <summary>
    /// Indicates the total number of bytes of physical memory.
    /// </summary>
    public uint TotalPhys;
    /// <summary>
    /// Indicates the number of bytes of physical memory available.
    /// </summary>
    public uint AvailPhys;
    /// <summary>
    /// Indicates the total number of bytes that can be stored in the paging file.
    /// This number does not represent the physical size of the paging file on disk.
    /// </summary>
    public uint TotalPageFile;
    /// <summary>
    /// Indicates the number of bytes available in the paging file.
    /// </summary>
    public uint AvailPageFile;
    /// <summary>
    /// Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.
    /// </summary>
    public uint TotalVirtual;
    /// <summary>
    /// Indicates the number of bytes of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process.
    /// </summary>
    public uint AvailVirtual;
    [DllImport("coredll.dll")]
    private static extern void GlobalMemoryStatus(ref MemoryStatus ms);
    public static string GetMemoryStatus()
    {
        var retValue = new StringBuilder();
        MemoryStatus ms = GlobalMemoryStatus();
        retValue.AppendLine(string.Format("Memory Load {0} %", ms.MemoryLoad));
        retValue.AppendLine(string.Format("Total Phys {0} Kb", ms.TotalPhys / 1024));
        retValue.AppendLine(string.Format("Avail Phys {0} Kb", ms.AvailPhys / 1024));
        retValue.AppendLine(string.Format("Tota PFile {0} bytes", ms.TotalPageFile));
        retValue.AppendLine(string.Format("Avai PFile {0} bytes", ms.AvailPageFile));
        retValue.AppendLine(string.Format("Total Virt {0} Kb", ms.TotalVirtual / 1024));
        retValue.AppendLine(string.Format("Avail Virt {0} Kb", ms.AvailVirtual / 1024));
        return retValue.ToString();
    }

    public static MemoryStatus GlobalMemoryStatus()
    {
        MemoryStatus ms = new MemoryStatus();
        ms.Length = Marshal.SizeOf(ms);
        GlobalMemoryStatus(ref ms);
        return ms;
    }
    public static uint GetMemoryLoad()
    {
        var ms = GlobalMemoryStatus();
        return ms.MemoryLoad;
    }
}
因为它在SDK中不存在。我使用了ref参数修饰符

这一切都是关于。

我是这样使用的

/// <summary>
/// https://msdn.microsoft.com/en-us/library/ee488368.aspx
/// This structure contains information about current memory availability. The GlobalMemoryStatus function uses this structure.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MemoryStatus
{
    /// <summary>
    /// Specifies the size, in bytes, of the MEMORYSTATUS structure.
    /// Set this member to sizeof(MEMORYSTATUS) when passing it to the GlobalMemoryStatus function.
    /// </summary>
    public int Length;
    /// <summary>
    /// Specifies a number between zero and 100 that gives a general idea of current memory use, in which zero indicates no memory use and 100 indicates full memory use.
    /// </summary>
    public uint MemoryLoad;
    /// <summary>
    /// Indicates the total number of bytes of physical memory.
    /// </summary>
    public uint TotalPhys;
    /// <summary>
    /// Indicates the number of bytes of physical memory available.
    /// </summary>
    public uint AvailPhys;
    /// <summary>
    /// Indicates the total number of bytes that can be stored in the paging file.
    /// This number does not represent the physical size of the paging file on disk.
    /// </summary>
    public uint TotalPageFile;
    /// <summary>
    /// Indicates the number of bytes available in the paging file.
    /// </summary>
    public uint AvailPageFile;
    /// <summary>
    /// Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.
    /// </summary>
    public uint TotalVirtual;
    /// <summary>
    /// Indicates the number of bytes of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process.
    /// </summary>
    public uint AvailVirtual;
    [DllImport("coredll.dll")]
    private static extern void GlobalMemoryStatus(ref MemoryStatus ms);
    public static string GetMemoryStatus()
    {
        var retValue = new StringBuilder();
        MemoryStatus ms = GlobalMemoryStatus();
        retValue.AppendLine(string.Format("Memory Load {0} %", ms.MemoryLoad));
        retValue.AppendLine(string.Format("Total Phys {0} Kb", ms.TotalPhys / 1024));
        retValue.AppendLine(string.Format("Avail Phys {0} Kb", ms.AvailPhys / 1024));
        retValue.AppendLine(string.Format("Tota PFile {0} bytes", ms.TotalPageFile));
        retValue.AppendLine(string.Format("Avai PFile {0} bytes", ms.AvailPageFile));
        retValue.AppendLine(string.Format("Total Virt {0} Kb", ms.TotalVirtual / 1024));
        retValue.AppendLine(string.Format("Avail Virt {0} Kb", ms.AvailVirtual / 1024));
        return retValue.ToString();
    }

    public static MemoryStatus GlobalMemoryStatus()
    {
        MemoryStatus ms = new MemoryStatus();
        ms.Length = Marshal.SizeOf(ms);
        GlobalMemoryStatus(ref ms);
        return ms;
    }
    public static uint GetMemoryLoad()
    {
        var ms = GlobalMemoryStatus();
        return ms.MemoryLoad;
    }
}
因为它在SDK中不存在。我使用了ref参数修饰符

这一切都是关于