How VB Can Get Windows Status Information via API Calls

ID Number: Q84556

1.00

WINDOWS

Summary:

The Visual Basic program example below demonstrates how you can obtain

system status information similar to the information displayed in the

Windows Program Manager About box. The example program displays the

following information using the Windows API function(s) indicated:

- The Windows version number with GetVersion

- The kind of CPU (80286, 80386, or 80486) and whether a math

coprocessor is present with GetWinFlags

- Whether Windows is running in enhanced mode or standard mode with

GetWinFlags

- The amount of free memory with GetFreeSpace and GlobalCompact

- The percentage of free system resources with SystemHeapInfo

Note: The API function SystemHeapInfo is new to Windows version 3.1

and is not available in Windows 3.0. All other API functions listed

above are available in both Windows 3.0 and 3.1.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

Steps to Create Example Program

-------------------------------

1. Run Visual Basic or, if Visual Basic is already running, choose New

Project from the File menu (ALT, F, N). Form1 will be created by

default.

2. Enter the following code into the Global module:

' Constants for GetWinFlags

Global Const WF_CPU286 = &H2

Global Const WF_CPU386 = &H4

Global Const WF_CPU486 = &H8

Global Const WF_80x87 = &H400

Global Const WF_STANDARD = &H10

Global Const WF_ENHANCED = &H20

' Type for SystemHeapInfo

Type SYSHEAPINFO

dwSize As Long

wUserFreePercent As Integer

wGDIFreePercent As Integer

hUserSegment As Integer

hGDISegment As Integer

End Type

Declare Function GetVersion Lib "Kernel" () As Integer

Declare Function GetWinFlags Lib "Kernel" () As Long

Declare Function GetFreeSpace Lib "Kernel" (ByVal wFlags As Integer)

As Long

Declare Function GlobalCompact Lib "Kernel" (ByVal dwMinFree As Long)

As Long

Declare Function SystemHeapInfo Lib "toolhelp.dll" (shi As

SYSHEAPINFO) As Integer

' Each Declare statement above must appear on a single line.

3. Enter the following code into Form1:

Sub Form_Load ()

Dim msg As String ' status information

Dim nl As String ' new-line

nl = Chr$(13) + Chr$(10) ' new-line

Show

mp% = MousePointer

MousePointer = 11 ' hour glass

' Get operating system version

ver% = GetVersion()

ver_major$ = Format$(ver% And &HFF)

ver_minor$ = Format$(ver% \ &H100, "00")

msg = msg + "Microsoft Windows version "

msg = msg + ver_major$ + "." + ver_minor$ + nl

' Get CPU kind and operating mode

msg = msg + "CPU: "

status& = GetWinFlags()

If status& And WF_CPU286 Then msg = msg + "80286"

If status& And WF_CPU386 Then msg = msg + "80386"

If status& And WF_CPU486 Then msg = msg + "80486"

If status& And WF_80x87 Then msg = msg + " with 80x87"

msg = msg + nl

msg = msg + "Mode: "

If status& And WF_STANDARD Then msg = msg + "Standard" + nl

If status& And WF_ENHANCED Then msg = msg + "Enhanced" + nl

' Get free memory

memory& = GetFreeSpace(0)

msg = msg + "Memory free: "

msg = msg + Format$(memory& \ 1024, "###,###,###") + "K" + nl

memory& = GlobalCompact(&HFFFFFFFF)

msg = msg + "Largest free block: "

msg = msg + Format$(memory& \ 1024, "###,###,###") + "K" + nl

' Get free system resources

' The API SystemHeapInfo became available in Windows version 3.1

msg = msg + "System resources: "

If ver% >= &H310 Then

Dim shi As SYSHEAPINFO

shi.dwSize = Len(shi)

If SystemHeapInfo(shi) Then

If shi.wUserFreePercent < shi.wGDIFreePercent Then

msg = msg + Format$(shi.wUserFreePercent) + "%"

Else

msg = msg + Format$(shi.wGDIFreePercent) + "%"

End If

End If

Else

msg = msg + "n/a"

End If

MsgBox msg, 0, "About " + Caption

MousePointer = mp%

End Sub

3. Press F5 to run the program.

Additional reference words: 1.00 3.00 3.10 286 386 486