XL5: Getting Windows Status Information from Windows API

ID: Q112393

The information in this article applies to:

SUMMARY

The Visual Basic, Applications Edition, example below demonstrates how to obtain system status information similar to the information displayed in the Microsoft Windows Program Manager About box. The example procedure displays the following information using the Windows application programming interface (API) functions indicated:

This Statement/

Property            Displays This Information

GetWinFlags          The kind of CPU (80286, 80386, or 80486) and
                     whether a math coprocessor is present

GetWinFlags          Whether Microsoft Windows is running in
                     enhanced mode or standard mode

GetFreeSpace         The amount of free memory
and
GlobalCompact

SystemHeapInfo       The percentage of free system resources

OperatingSystem      The version of Windows

MORE INFORMATION

To Create a sample macro

1. Start Microsoft Excel for Windows 5.0.

2. On the Insert menu, click Macro, and then click Module. Module 1

   is created by default.

3. Type the following code into the newly created 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 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

   Sub GetWindowsInfo()
       Dim Status As Long
       Dim Memory As Long
       Dim msg As String         ' Status information.
       Dim nl As String          ' New-line.
       Dim shi As SYSHEAPINFO
       nl = Chr$(13) + Chr$(10)  ' New-line.

       Status = GetWinFlags()

       ' Get operating system version.
       ' (Uses Excel's built-in OperatingSystem function rather
       ' than Windows API calls.)
       msg = "OS: " + Application.OperatingSystem

       ' Get CPU kind and operating mode.
       msg = msg + nl + "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(&hffff)
       msg = msg + "Largest free block: "
       msg = msg + Format$(Memory \ 1024, "###,###,###") + "K" + nl

       ' Get free system resources.
       msg = msg + "System resources: "
       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

       MsgBox msg, vbOKOnly, "About This PC"
   End Sub

4. On the Tools menu, click Macro.

5. In the Macro Name/Reference list, click GetWindowsInfo, and

   click OK to run the macro.

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide."

Additional query words: 5.00 5.00c

Keywords          : kbprg 
Version           : 5.00 5.00c
Platform          : WINDOWS

Last Reviewed: May 17, 1999