GlobalMemoryStatus获取内存使用情况只适用于内存小于2G的机器,我机器的内存是3G,获取的总物理内存和可用内存数据都是2G。逼不得已只能尝试使用GlobalMemoryStatusEx函数,其为Kernel32.dll提供的接口,Kernel32.dll在程序启动时就已经加载进来了,但是直接使用会报错,“无法识别”,最终只能尝试以下方法:typedef BOOL (CALLBACK* GLOBALMEMORYSTATUSEX)(MEMORYSTATUS* pMemStatus);
GLOBALMEMORYSTATUSEX pGlobalMemoryStatusEx;pGlobalMemoryStatusEx = (GLOBALMEMORYSTATUSEX)GetProcAddress(GetModuleHandle(_T("Kernel32.dll")), ("GlobalMemoryStatusEx"));
if (pGlobalMemoryStatusEx)
{
    MEMORYSTATUSEX MemStatEx;
    MemStatEx.dwLength = sizeof(MEMORYSTATUSEX);
    pGlobalMemoryStatusEx(&MemStatEx);
    dwError = GetLastError();
}函数GlobalMemoryStatusEx是可以使用了,但还是有问题,MEMORYSTATUSEX又是无法识别的了,实在不知道怎么办了,请有经验的帮帮忙,谢谢了!有其他的办法也行,只要让我能正确获得大于2G机器的内存情况就可以,貌似这些函数的使用,NT系统与其他系统还不一样,不管这么多了,只要能正确获取就行。

解决方案 »

  1.   

    typedef struct _MEMORYSTATUSEX {
      DWORD     dwLength;
      DWORD     dwMemoryLoad;
      DWORDLONG ullTotalPhys;
      DWORDLONG ullAvailPhys;
      DWORDLONG ullTotalPageFile;
      DWORDLONG ullAvailPageFile;
      DWORDLONG ullTotalVirtual;
      DWORDLONG ullAvailVirtual;
      DWORDLONG ullAvailExtendedVirtual;
    } MEMORYSTATUSEX, *LPMEMORYSTATUSEX;详见http://msdn.microsoft.com/en-us/library/aa366770%28VS.85%29.aspx
      

  2.   

    你需要定义下这个结构体typedef struct _MEMORYSTATUSEX {
        DWORD dwLength;
        DWORD dwMemoryLoad;
        DWORDLONG ullTotalPhys;
        DWORDLONG ullAvailPhys;
        DWORDLONG ullTotalPageFile;
        DWORDLONG ullAvailPageFile;
        DWORDLONG ullTotalVirtual;
        DWORDLONG ullAvailVirtual;
        DWORDLONG ullAvailExtendedVirtual;
    } MEMORYSTATUSEX, *LPMEMORYSTATUSEX;
     你既然连函数都没找到 其结构体肯定也没有 楼主应该用的低版本的VS,建议用高版本的。。
    还有 楼主如果没有的话 可以 去MSDN在线帮助文档上面看这个函数 这个函数有微软的示例//  Sample output:
    //  There is       51 percent of memory in use.
    //  There are 2029968 total Kbytes of physical memory.
    //  There are  987388 free Kbytes of physical memory.
    //  There are 3884620 total Kbytes of paging file.
    //  There are 2799776 free Kbytes of paging file.
    //  There are 2097024 total Kbytes of virtual memory.
    //  There are 2084876 free Kbytes of virtual memory.
    //  There are       0 free Kbytes of extended memory.#include <windows.h>
    #include <stdio.h>// Use to convert bytes to KB
    #define DIV 1024// Specify the width of the field in which to print the numbers. 
    // The asterisk in the format specifier "%*I64d" takes an integer 
    // argument and uses it to pad and right justify the number.
    #define WIDTH 7void main(int argc, char *argv[])
    {
      MEMORYSTATUSEX statex;  statex.dwLength = sizeof (statex);  GlobalMemoryStatusEx (&statex);  printf ("There is  %*ld percent of memory in use.\n",
              WIDTH, statex.dwMemoryLoad);
      printf ("There are %*I64d total Kbytes of physical memory.\n",
              WIDTH, statex.ullTotalPhys/DIV);
      printf ("There are %*I64d free Kbytes of physical memory.\n",
              WIDTH, statex.ullAvailPhys/DIV);
      printf ("There are %*I64d total Kbytes of paging file.\n",
              WIDTH, statex.ullTotalPageFile/DIV);
      printf ("There are %*I64d free Kbytes of paging file.\n",
              WIDTH, statex.ullAvailPageFile/DIV);
      printf ("There are %*I64d total Kbytes of virtual memory.\n",
              WIDTH, statex.ullTotalVirtual/DIV);
      printf ("There are %*I64d free Kbytes of virtual memory.\n",
              WIDTH, statex.ullAvailVirtual/DIV);  // Show the amount of extended memory available.  printf ("There are %*I64d free Kbytes of extended memory.\n",
              WIDTH, statex.ullAvailExtendedVirtual/DIV);
    }
      

  3.   

    这个结构体我前面自定义过了,但  pGlobalMemoryStatusEx(&MemStatEx);
    这句编译提示有错误,'int (struct _MEMORYSTATUS *)' : cannot convert parameter 1 from 'struct _MEMORYSTATUSEX *' to 'struct _MEMORYSTATUS *'不知道是个什么情况,貌似还要将Ex的结构体转化为普通的?
      

  4.   

    MEMORYSTATUS MemStat;
    MemStat.dwLength = sizeof(MEMORYSTATUS);
    pGlobalMemoryStatusEx(&MemStat);用这个编译不会提示错误,但GetLastError的错误码是提示参数不正确,而用自定义的MEMORYSTATUSEX又会出现上面参数不能转化的错误,难道是pGlobalMemoryStatusEx = (GLOBALMEMORYSTATUSEX)GetProcAddress(GetModuleHandle(_T("Kernel32.dll")), ("GlobalMemoryStatusEx"));
    获取的指针有问题?这到底是个什么问题,求解!
      

  5.   

    对于GlobalMemoryStatus函数应当使用MEMORYSTATUS参数
    对于GlobalMemoryStatusEx 函数应当使用MEMORYSTATUSEX 参数,两个函数的参数是不一样的。MEMORYSTATUS statex;
    statex.dwLength = sizeof (statex);
    GlobalMemoryStatus (&statex);
    MEMORYSTATUSEX statex;
    statex.dwLength = sizeof (statex);
    GlobalMemoryStatusEx (&statex);
      

  6.   

    大虾,我也在找这个问题,你知道怎么获取系统”核心内存“中的”分页数与未分页数“吗?
     这个是windows中 任务管理器中——》性能选项卡——》核心内存(中的分页数与未分页数)
      

  7.   

    typedef BOOL (CALLBACK* GLOBALMEMORYSTATUSEX)(MEMORYSTATUS* pMemStatus);↓typedef BOOL (CALLBACK* GLOBALMEMORYSTATUSEX)(MEMORYSTATUSEX* pMemStatus);