As usual, fire up VB and create a Standard EXE project. Copy this code into the General Declarations section of the form that is added to the project by default:
Private Type MEMORYSTATUS
dwLength As Long
dwMemoryLoad As Long
dwTotalPhys As Long
dwAvailPhys As Long
dwTotalPageFile As Long
dwAvailPageFile As Long
dwTotalVirtual As Long
dwAvailVirtual As Long
End Type
Private Declare Sub GlobalMemoryStatus Lib "kernel32" Alias "GlobalMemoryStatus" (lpBuffer As MEMORYSTATUS)
Here we first declare a user defined type structure, MEMORYSTATUS. It contains eight members all of the type Long. A variable of this Type is passed ByRef as argument to the GlobalMemoryStatus API function. (See Article 1 and Article 2 of this series for more info on ByVal, ByRef and other such creatures associated with API programming.
The GlobalMemoryStatus function retrieves information about current available memory. The function returns information about both physical and virtual memory. The function populates it with the memory information parameters, which we can then determine by examining the appropriate variable.
Now, add the following code to any suitable event in your app where you want to display the memory status:
Dim memInfo As MEMORYSTATUS
GlobalMemoryStatus memInfo 'In this step we invoked the GlobalMemoryStatus function and pass
it meminfo as parameter
MsgBox "Total memory (in bytes): " & memInfo.dwTotalPhys
MsgBox "Available memory (in bytes): " & memInfo.dwAvailPhys
Comments