MemoryInfoDlg.cpp:
void CMemoryInfoDlg::OnBnClickedRefresh()
{
TCHAR szBuffer[256];
DWORD dwSize = 256;
GetComputerName(szBuffer, &dwSize);
m_strComputerName = szBuffer;
MEMORYSTATUS mem_stat;
GlobalMemoryStatus(&mem_stat);
m_strTotalMemory.Format(_T("%ld KB"), mem_stat.dwTotalPhys/1024);
m_strFreeMemory.Format(_T("%ld KB"), mem_stat.dwAvailPhys/1024);
m_strMemoryLoad.Format(_T("%d %%"), mem_stat.dwMemoryLoad);
UpdateData(FALSE);
}
注:
DWORD
32-bit unsigned integer.
This type is declared in WinDef.h as follows:
typedef unsigned long DWORD;
TCHAR 和 _T( ) http://www.cnblogs.com/aquariusgx/archive/2010/02/12/1667923.html
GetComputerName
在GetComputerName函数检索本地计算机的NetBIOS名称。这个名字是建立在系统启动时,当系统从注册表中读取它。
BOOL GetComputerName( LPTSTR lpBuffer, LPDWORD lpnSize );
Parameters
- lpBuffer
- [out] Pointer to a buffer that receives a null-terminated string containing the computer name or the cluster virtual server name. The buffer size should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.
- lpnSize
- [in, out] On input, specifies the size of the buffer, in TCHARs. On output, the number of TCHARs copied to the destination buffer, not including the terminating null character.
If the buffer is too small, the function fails and GetLastError returns ERROR_BUFFER_OVERFLOW. The lpnSize parameter specifies the size of the buffer required, not including the terminating null character.
Windows Me/98/95: GetComputerName fails if the input size is less than MAX_COMPUTERNAME_LENGTH + 1.
Return Values
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
The GetComputerName function retrieves the NetBIOS name established at system startup. Name changes made by the SetComputerName or SetComputerNameEx functions do not take effect until the user restarts the computer.
MEMORYSTATUS
该MEMORYSTATUS中结构包含了物理和虚拟内存的当前状态信息。 GlobalMemoryStatus函数的信息存储在一个MEMORYSTATUS中的结构。
typedef struct _MEMORYSTATUS {
DWORD dwLength;
DWORD dwMemoryLoad;
SIZE_T dwTotalPhys;
SIZE_T dwAvailPhys;
SIZE_T dwTotalPageFile;
SIZE_T dwAvailPageFile;
SIZE_T dwTotalVirtual;
SIZE_T dwAvailVirtual; } MEMORYSTATUS, *LPMEMORYSTATUS;
Members
- dwLength
- Size of the MEMORYSTATUS data structure, in bytes. You do not need to set this member before calling the GlobalMemoryStatus function; the function sets it.
- dwMemoryLoad
- Number between 0 and 100 that specifies the approximate percentage of physical memory that is in use (0 indicates no memory use and 100 indicates full memory use).
Windows NT: Percentage of approximately the last 1000 pages of physical memory that is in use.
- dwTotalPhys
- Total size of physical memory, in bytes.
- dwAvailPhys
- Size of physical memory available, in bytes.
- dwTotalPageFile
- Size of the committed memory limit, in bytes.
- dwAvailPageFile
- Size of available memory to commit, in bytes.
- dwTotalVirtual
- Total size of the user mode portion of the virtual address space of the calling process, in bytes.
- dwAvailVirtual
- Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes.
Remarks
MEMORYSTATUS reflects the state of memory at the time of the call. It reflects the size of the paging file at that time. The operating system can enlarge the paging file up to the maximum size set by the administrator.
On computers with more than 4 GB of memory, the MEMORYSTATUS structure can return incorrect information. Windows reports a value of -1 to indicate an overflow, while Windows NT reports a value that is the real amount of memory, modulo 4 GB. If your application is at risk for this behavior, use the GlobalMemoryStatusEx function instead of the GlobalMemoryStatus function.
GlobalMemoryStatus
该GlobalMemoryStatus函数获取信息系统的物理和虚拟内存的当前使用情况。
void GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer );
Parameters
- lpBuffer
- [out] Pointer to a MEMORYSTATUS structure. The GlobalMemoryStatus function stores information about current memory availability into this structure.
Return Values
This function does not return a value.
Remarks
You can use the GlobalMemoryStatus function to determine how much memory your application can allocate without severely impacting other applications.
The information returned by the GlobalMemoryStatus function is volatile. There is no guarantee that two sequential calls to this function will return the same information.
On computers with more than 4 GB of memory, the GlobalMemoryStatus function can return incorrect information. Windows 2000 and later report a value of -1 to indicate an overflow. Earlier versions of Windows NT report a value that is the real amount of memory, modulo 4 GB. For this reason, use the GlobalMemoryStatusEx function instead.
On Intel x86 computers with more than 2 GB and less than 4 GB of memory, the GlobalMemoryStatus function will always return 2 GB in the dwTotalPhys member of the MEMORYSTATUS structure. Similarly, if the total available memory is between 2 and 4 GB, the dwAvailPhys member of the MEMORYSTATUS structure will be rounded down to 2 GB. If the executable is linked using the /LARGEADDRESSAWARE linker option, then the GlobalMemoryStatus function will return the correct amount of physical memory in both members.