Microsoft KB Archive/84556

= VB3 How VB Can Get Windows Status Information via API Calls =

Article ID: 84556

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q84556



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.



Steps to Create Example Program
 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. 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).  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 &quot;Kernel&quot; As Integer Declare Function GetWinFlags Lib &quot;Kernel&quot; As Long 'Enter each of the following Declare statements as one, single line: Declare Function GetFreeSpace Lib &quot;Kernel&quot; (ByVal wFlags As Integer) As Long Declare Function GlobalCompact Lib &quot;Kernel&quot; (ByVal dwMinFree As Long) As Long Declare Function SystemHeapInfo Lib &quot;toolhelp.dll&quot; (shi As     SYSHEAPINFO) As Integer

  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 + &quot;Microsoft Windows NT &quot; Else msg = msg + &quot;Microsoft Windows &quot; End If     ver_major$ = Format$(ver% And &HFF) ver_minor$ = Format$(ver% \ &H100, &quot;00&quot;) msg = msg + ver_major$ + &quot;.&quot; + ver_minor$ + nl

' Get CPU kind and operating mode. msg = msg + &quot;CPU: &quot; If status& And WF_CPU286 Then msg = msg + &quot;80286&quot; If status& And WF_CPU386 Then msg = msg + &quot;80386&quot; If status& And WF_CPU486 Then msg = msg + &quot;80486&quot; If status& And WF_80x87 Then msg = msg + &quot; with 80x87&quot; msg = msg + nl     msg = msg + &quot;Mode: &quot; If status& And WF_STANDARD Then msg = msg + &quot;Standard&quot; + nl     If status& And WF_ENHANCED Then msg = msg + &quot;Enhanced&quot; + nl

' Get free memory. memory& = GetFreeSpace(0) msg = msg + &quot;Memory free: &quot; msg = msg + Format$(memory& \ 1024, &quot;###,###,###&quot;) + &quot;K&quot; + nl     memory& = GlobalCompact(&HFFFFFFFF) msg = msg + &quot;Largest free block: &quot; msg = msg + Format$(memory& \ 1024, &quot;###,###,###&quot;) + &quot;K&quot; + nl

' Get free system resources. ' The API SystemHeapInfo became available in Windows version 3.1. msg = msg + &quot;System resources: &quot; 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) + &quot;%&quot; Else msg = msg + Format$(shi.wGDIFreePercent) + &quot;%&quot; End If        End If      Else msg = msg + &quot;n/a&quot; End If

MsgBox msg, 0, &quot;About &quot; + Caption MousePointer = 0 End Sub

 Press the F5 key to run the program.

Additional query words: 2.00 3.00 3.10 286 386 486

Keywords: kbwndw kbcode KB84556

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.