システムメモリ情報GlobalMemoryStatusおよびGlobalMemoryStatusExを取得するためのWindowsプログラミングwin32API



Windows Programming Win32 Api Obtain System Memory Information Globalmemorystatus



1.システムメモリ情報を取得するための2つのAPI、つまりGlobalMemoryStatusとGlobalMemoryStatusExがあります
GlobalMemoryStatus関数はC言語で直接呼び出すことができますが、GlobalMemoryStatusExを直接呼び出すことはできません。

2.これら2つの機能の違いを見てください。一般に、これら2つの関数はメモリ情報を取得できますが、GlobalMemoryStatusEx関数はGlobalMemoryStatus関数の拡張機能です。 GlobalMemoryStatusは取得できますが、マシンに2つインストールすると、メモリバーは1つのメモリ情報容量しか取得できませんが、GlobalMemoryStatusEx関数は、システムで認識されるメモリサイズがあれば、すべてを取得できます。

3.2つの機能の使い方をそれぞれ見てみましょう。

3.1。 GlobalMemoryStatus関数を最初に見てください。この関数は直接呼び出すことができます。

MEMORYSTATUS status//Define the variable that stores the memory information GlobalMemoryStatus(&status)//Call GlobalMemoryStatus function to get memory information //Process the actual memory information obtained TCHAR ss[256] ZeroMemory(ss,sizeof(ss)/sizeof(TCHAR)) wsprintf(ss,'%d KB',status.dwTotalPhys/1000)//Realistic memory size MessageBox(hwndDlg,ss,TEXT(''),MB_OK) 3.2。 GlobalMemoryStatusEx関数を呼び出すのはそれほど簡単ではありません。 C言語で直接呼び出すことはできません。これはシステムのkernel32.dllダイナミックリンクライブラリに記述されているため、システムのダイナミックリンクライブラリDLLをC言語で呼び出す必要があります。

コードをアップロードするだけです:



/* *Here is a function that calls the GlobalMemoryStatusEx function in the kernel32.dll dynamic link library **/ #include typedef void(WINAPI* FunctionGlobalMemoryStatusEx)(LPMEMORYSTATUS)//Declare the function prototype pointer MEMORYSTATUS GetMemoryStatus() { HMODULE hModule FunctionGlobalMemoryStatusEx GlobalMemoryStatusEx MEMORYSTATUS status status.dwLength = sizeof(status) hModule = LoadLibrary('kernel32.dll')//Load the dynamic link library kernel32.dll and return its handle if(NULL==hModule)//Determine whether loading dll is successful MB_ICONERROR) return //Find the GlobalMemoryStatusEx function in the kernel32.dll handle and return the pointer to the changed function GlobalMemoryStatusEx =(FunctionGlobalMemoryStatusEx)GetProcAddress(hModule,'GlobalMemoryStatusEx') if(NULL==GlobalMemoryStatusEx)//Determine whether the function is found //error //MessageBox(hwndDlg,TEXT('The function cannot be found'),TEXT('error'),MB_OK GlobalMemoryStatusEx(&status)//Call the function to get the memory status of the system FreeLibrary(hModule)//Release the link library handle return status }効果の写真を見てください。私のマシンには4Gメモリ、2および2Gメモリがあります。XPシステムがインストールされているため、3Gメモリしか認識できません。

GlobalMemoryStatus:




GlobalMemoryStatusEx:


転載元:https://www.cnblogs.com/llz5023/archive/2012/12/30/2839776.html