Visual Basic WMI System Services
Win32_Environment: WMI Environment Settings
     
Posted:   Monday March 25, 2002
Updated:   Monday November 28, 2011
     
Applies to:   VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   Windows NT, 2000, XP. See Prerequisites below.
Author:   VBnet - Randy Birch
     

Related:  

 
     
 Prerequisites
Windows Script Host is built into Microsoft Windows 98, 2000, ME and XP. If you are running Windows 95 or NT4, you can download Windows Script Host from the Microsoft Windows Script Technologies Web site at http://msdn.microsoft.com/scripting/. Some information is not returned on non-NT-based systems.

A reference set in Projects / References to the Microsoft WMI Scripting Library.


The Win32_Environment WMI class represents an environment or system environment setting on a Windows computer system.

This demo and illustration only show some of the available information from the class. For a complete listing see the table in the Comments section below. Note that some systems may not return information in all class properties.

The system variables (ie %systemroot%) are expanded  (column three above) into their actual path information for the current user by calling ExpandEnvironmentStrings().

 BAS Module Code
None.

 Form Code
To a form add a command button (Command1) and a listview (Listview1). Set a reference in Projects / References to the Microsoft WMI Scripting Library, and add the following to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'/* Below used for listview column auto-resizing
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETCOLUMNWIDTH As Long = (LVM_FIRST + 30)
Private Const LVSCW_AUTOSIZE As Long = -1
Private Const LVSCW_AUTOSIZE_USEHEADER As Long = -2
Private Const MAX_PATH As Long = 260

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

Private Declare Function ExpandEnvironmentStrings Lib "kernel32" _
   Alias "ExpandEnvironmentStringsA" _
  (ByVal lpSrc As String, _
   ByVal lpDst As String, _
   ByVal nSize As Long) As Long

Private Declare Function lstrlen Lib "kernel32" _
    Alias "lstrlenW" (ByVal lpString As Long) As Long


Private Sub Form_Load()

   With ListView1
      .ListItems.Clear
      .View = lvwReport
      .Sorted = False
      .ColumnHeaders.Clear
      .ColumnHeaders.Add , , "Variable Name"
      .ColumnHeaders.Add , , "Environment Value"
      .ColumnHeaders.Add , , "Expanded String"
   End With
   
   Command1.Caption = "Environment Info"
      
End Sub


Private Sub Command1_Click()

   ListView1.ListItems.Clear
   Call wmiEnvironmentInfo
   Call lvAutosizeControl(ListView1)
   
End Sub


Private Sub lvAutosizeControl(lv As ListView)

   Dim col2adjust As Long

  'Size each column based on the maximum of
  'either the columnheader text width, or,
  'if the items below it are wider, the
  'widest list item in the column
   For col2adjust = 0 To lv.ColumnHeaders.Count - 1
   
      Call SendMessage(lv.hwnd, _
                       LVM_SETCOLUMNWIDTH, _
                       col2adjust, _
                       ByVal LVSCW_AUTOSIZE_USEHEADER)

   Next
   
End Sub


Private Sub wmiEnvironmentInfo()

   Dim EnvSet As SWbemObjectSet
   Dim env As SWbemObject
   Dim itmx As ListItem

   Set EnvSet = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
                           InstancesOf("Win32_Environment")
   
   On Local Error Resume Next
   
   For Each env In EnvSet
   
      Set itmx = ListView1.ListItems.Add(, , env.Name)
      itmx.SubItems(1) = env.VariableValue
      itmx.SubItems(2) = ChangeEnvironmentToPath(env.VariableValue)

   Next

End Sub


Private Function ChangeEnvironmentToPath(ByVal sEnvironPath As String) As String

  'Parses an input string containing references
  'to one or more environment variables and
  'replaces them with their current values.
  '
  'Case is ignored. Each %VariableName% string is
  'replaced with the variable's current value.
  'The replacement rules are the same as those
  'used by the command interpreter. If the name
  'is not found, the %variableName% string is
  'left intact and returned.
  '
  'NOTE: The environment variables that correspond
  'to file system folders can be mapped to an
  'equivalent CSIDL value and obtained with
  'SHGetFolderLocation. CSIDLs are more reliable
  'than variable names and should be used if at
  'all possible.
   Dim buff As String
   Dim nSize As Long
   
   buff = Space$(MAX_PATH)
   nSize = Len(buff)
   
   Call ExpandEnvironmentStrings(sEnvironPath, buff, nSize)
   ChangeEnvironmentToPath = TrimNull(buff)
   
End Function


Private Function TrimNull(startstr As String) As String

   TrimNull = Left$(startstr, lstrlen(StrPtr(startstr)))
   
End Function
 Comments
All information returned in the Win32_Environment class (note that some systems may not return information in all class properties):
   
string Caption
string Description
datetime InstallDate
string Name
string Status
boolean SystemVariable
string UserName
string VariableValue

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter