Using the SystemInfo Class

This section describes each of the categories of properties in the SystemInfo class, explaining both how to use them and how they were implemented.

Computer and User Information

The two properties ComputerName and UserName provide information about the network name for the computer and the logged-in user’s name. Both properties return strings, and the ComputerName property also allows you to set the name of the computer. For example, you might write code like this to use the properties:

Dim si As New SystemInfo
Dim strOut As String
strOut = si.UserName & " is logged into " & si.ComputerName
MsgBox strOut
si.ComputerName = "CompuLand"

Retrieving these properties is simple: the Windows API provides the GetComputerName and GetUserName functions. In both cases, you pass in a buffer to contain the name and a long integer variable containing the length of the buffer. Windows fills in the buffer and places the length of the string it returned into the long integer variable. If the function returns a nonzero value, the code can use the Left function to retrieve as many characters from the buffer as Windows said it filled in. Listing 9.14 shows the code for retrieving the ComputerName property; the code for the UserName is almost identical.

Listing 9.14: Code for the ComputerName Property

Property Get ComputerName() As String
    Dim strBuffer As String
    Dim lngLen As Long
    strBuffer = Space(dhcMaxComputerName + 1)
    lngLen = Len(strBuffer)
    If CBool(GetComputerName(strBuffer, lngLen)) Then
        ComputerName = Left$(strBuffer, lngLen)
    Else
        ComputerName = ""
    End If
End Property

The code to set the computer name is even simpler. It calls the SetComputerName API function:

Property Let ComputerName(Name As String)
    Call SetComputerName(Name)
End Property

The SetComputerName API call only writes the new computer name to the Registry. It doesn’t (and it really can’t) change the name of the computer as it’s currently used on the network. The next time you restart the computer, it will use the new name.

Path Information

The SystemDirectory, TempPath, and WindowsDirectory properties retrieve information about where you can expect to find files on your computer. In each case, Windows provides a single function to call in order to retrieve the information, and in each case, the code is almost identical. For example, Listing 9.15 includes the code for the WindowsDirectory property. You should be familiar with this code if you’ve ever done any work with the Windows API that involves strings. In the WindowsDirectory property procedure, the code first creates a buffer to hold the output string and makes sure it’s large enough for the largest expected result, using the String function. Then it calls the GetWindowsDirectory API function, passing the buffer and the length of the buffer. GetWindowsDirectory attempts to place the path into the buffer and returns the length of the string it placed into the buffer. If the buffer wasn’t large enough, the function returns the length it would need to place into the buffer. If the function returns a value larger than the length passed into it, the property procedure resizes the buffer and tries again. This time, the string is guaranteed to fit.

Listing 9.15: Code for the WindowsDirectory Property

Property Get WindowsDirectory() As String
    ' Retrieve the Windows directory.
    Dim strBuffer As String
    Dim lngLen As Long
    strBuffer = Space(dhcMaxPath)
    lngLen = dhcMaxPath
    lngLen = GetWindowsDirectory(strBuffer, lngLen)
    ' If the path is longer than dhcMaxPath, then
    ' lngLen contains the correct length. Resize the
    ' buffer and try again.
    If lngLen > dhcMaxPath Then
        strBuffer = Space(lngLen)
        lngLen = GetWindowsDirectory(strBuffer, lngLen)
    End If
    WindowsDirectory = Left$(strBuffer, lngLen)
End Property

The three functions used in these three properties provide a perfect example of the non-uniformity of Windows API functions. For example, GetWindowsDirectory and GetSystemDirectory accept first a string and then its length. GetTempPath takes its parameters in the opposite order. In addition, GetTempPath returns a path that always ends with a trailing backslash, yet both the others return paths without the trailing backslash.

Processor Information

To retrieve processor information, the SystemInfo class uses the GetSystemInfo API function. This function fills a SYSTEM_INFO data structure with data. (See the class module for the gory details.) The Initialize event procedure of the SystemInfo class calls the API function, and the various properties retrieve information from the elements of the SYSTEM_INFO structure.

Although the processor information returned by the GetSystemInfo API function isn’t necessary for every application, it can be useful. The next few sections provide the details necessary to interpret the information provided by these properties.

NumberOfProcessors

Specifies the number of processors in the system.

ActiveProcessorMask

Specifies a mask value representing the processors in the system. The bit or bits set in the mask indicate the active processor (bit 0 is processor 0; bit 31 is processor 31). This value will be 1 for most computers.

PageSize

Specifies the page size and the granularity of page protection and commitment. This isn’t generally of much interest to VBA programmers.

AllocationGranularity

Specifies the granularity with which virtual memory is allocated. This value was hard coded as 64K in the past; since the Windows environment expands to different hardware platforms, other values may be necessary. Again, this value isn’t of much interest to VBA programmers.

MinimumApplicationAddress, MaximumApplicationAddress

Pointers to the lowest and highest memory addresses accessible to applications and Dynamic Link Libraries. Not generally needed for VBA programmers unless they’re making serious use of the Windows API functions that care about these addresses.

ProcessorType

Not relevant to Windows NT, which uses the ProcessorArchitecture, ProcessorLevel, and ProcessorRevision properties to provide information about the processor. This property provides the only means, in Windows 95, to gather such information. The value will be one of the items in the following list:

Value Processor
386 Intel 386
486 Intel 486
586 Intel Pentium
4000 MIPS R4000 (NT only)
21064 Alpha 21064 (NT only)

ProcessorArchitecture

Specifies the system’s processor architecture. For Windows 95, this value will always be 0. For Windows NT, the value can be any item from the following list:

Value Processor
0 Intel
1 MIPS
2 Alpha
3 PPC
–1 Unknown

ProcessorLevel  Not used in Windows 95, but in Windows NT it returns the system’s architecture-dependent processor level. The values can be any of the items in the first column of the following list. Use the ProcessorArchitecture value in the second column to determine the actual processor level.

Value Processor Architecture Description
3 0 Intel 80386
4 0 Intel 80486
5 0 Intel Pentium
6 0 Intel Pentium Pro
4 1 MIPS R4000
21064 2 Alpha 21064
21066 2 Alpha 21066
21164 2 Alpha 21164
1 3 PPC 601
3 3 PPC 603
4 3 PPC 604
6 3 PPC 603+
9 3 PPC 604+
20 3 PPC 620

ProcessorRevision

Not used in Windows 95, but in Windows NT this property specifies an architecture-dependent processor revision.

Version Information

The properties in this area all use the GetVersionEx API function to fill in an OSVERSIONINFO structure with information about the operating system. In the Initialize event procedure for the SystemInfo class, the code calls GetVersionEx, so all the various properties need do is retrieve information from a module-level variable.

© 1997 by SYBEX Inc. All rights reserved.