API Programming Series #5

The Code

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

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic