Tutorials2u.com

Tutorials on various topics

A windows API example

windows API example, findmemory available on your system
Windows API example in Visual Basic

Displaying the total memory on the system.

This simple API example we are going to get the total memory available on the system.

Start up Visual Basic as usual and enter the code below in the general declarations section of the form.

Option Explicit

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″ (lpBuffer As MEMORYSTATUS)

Now enter the following in the Form_Load event procedure

Private Sub Form_Load()
Dim memStat As MEMORYSTATUS
GlobalMemoryStatus memStat

MsgBox “Total memory : ” & memStat.dwTotalPhys / 1024 / 1024 & “megabytes”
End Sub

You should now have a layout like this

Now run the program (Press F5) and all going well you should see the total memory of your system. Here is what was displayed
on the test system.

Analysis

The first thing is a user defined type MEMORYSTATUS, this contains eight members all of which are Longs. The member we are interested in
is dwTotalPhys.

Next we insert the GlobalMemoryStatus API function which is used to retrieve the memory information.

Now in the Form_Load event we declare various code, the first snippet declares a variable memStat of type MEMORYSTATUS. The MEMSTAT
variable is passed to the GlobalMemoryStatus API function which fills in the information in to the MEMORYSTATUS user defined type.
We then display the memory size in a msgbox . The result is in bytes so we divide it twice by 1024 to first convert to kilobytes and then megabytes,
you could of course do this again if you have lots of memory in your system.

Further Projects

Display all of the MEMORYSTATUS members on a form.
Build a resource meter which shows features such as memory load and page file usage.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Sponsors


Links