VB3 How VB Can Get Windows Status Information via API Calls

ID: Q84556


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0


SUMMARY

The Visual Basic for Windows 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, version 3.0. All other API functions listed above are available in both Windows versions 3.0 or 3.1.


MORE INFORMATION

Steps to Create Example Program

  1. Run Visual Basic for Windows, or if Visual Basic for Windows is already running, choose New Project from the File menu (press ALT, F, N). Form1 will be created by default.


  2. From the File menu, choose Add Module (press ALT, F, M). Module 1 is created by default (In Visual Basic version 1.0 for Windows, this step is unnecessary).


  3. Enter the following code into the general declarations section of a code module (In Visual Basic version 1.0 for Windows, place the following in 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
       Global Const WF_WINNT = &H4000
    
       ' 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
       'Enter each of the following Declare statements as one, single line:
       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
     


  4. Enter the following code into the Form_Load procedure of Form1:
    
       Sub Form_Load ()
          Dim msg As String         ' Status information.
          Dim nl As String          ' New-line.
          nl = Chr$(13) + Chr$(10)  ' New-line.
    
          Show
          MousePointer = 11   ' Hourglass.
          ver% = GetVersion()
          status& = GetWinFlags()
    
          ' Get operating system and version.
          If status& And WF_WINNT Then
             msg = msg + "Microsoft Windows NT "
          Else
             msg = msg + "Microsoft Windows "
          End If
          ver_major$ = Format$(ver% And &HFF)
          ver_minor$ = Format$(ver% \ &H100, "00")
          msg = msg + ver_major$ + "." + ver_minor$ + nl
    
          ' Get CPU kind and operating mode.
          msg = msg + "CPU: "
          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 = 0
       End Sub
     


  5. Press the F5 key to run the program.


Additional query words: 2.00 3.00 3.10 286 386 486

Keywords : kbcode kbWndw
Version : 1.00 2.00 3.00
Platform : WINDOWS
Issue type :


Last Reviewed: June 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.