2015-04-23 105 views
1

如果您不熟悉或瞭解Windows CE和Compact Framework,請不要投票。謝謝。PInvoke for Windows CE上的GlobalMemoryStatusEx

有人知道這個函數是否可用在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:我用的PInvoked功能負載,如這一個:

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

回答

2

已經實現了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"); 
0

我以這種方式使用。

/// <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; 
    } 
} 

string memory = Device.MemoryStatus.GetMemoryStatus(); MessageBox.Show(memory, "Memory");

你可以在你的Windows CE SDK

typedef struct _MEMORYSTATUS { 
    DWORD dwLength; 
    DWORD dwMemoryLoad; 
    DWORD dwTotalPhys; 
    DWORD dwAvailPhys; 
    DWORD dwTotalPageFile; 
    DWORD dwAvailPageFile; 
    DWORD dwTotalVirtual; 
    DWORD dwAvailVirtual; 
} MEMORYSTATUS, *LPMEMORYSTATUS; 

VOID 
WINAPI 
GlobalMemoryStatus(
    __inout LPMEMORYSTATUS lpBuffer 
    ); 

因爲WINBASE.H檢查是_inout在SDK。我已經使用「ref」參數修飾符。

這是所有關於。