首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

GlobalMemoryStatusEx 获取内存使用情况的有关问题

2013-07-04 
GlobalMemoryStatusEx 获取内存使用情况的问题GlobalMemoryStatus获取内存使用情况只适用于内存小于2G的机

GlobalMemoryStatusEx 获取内存使用情况的问题
    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系统与其他系统还不一样,不管这么多了,只要能正确获取就行。

[解决办法]
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

[解决办法]
你需要定义下这个结构体


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 7

void 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);
}

热点排行