4. Creating Code for Properties

Applies to TestComplete 14.72, last modified on April 22, 2021

Now we can create the code for the methods and properties of the WMI object.

To add a property to the custom object, you should perform the following tasks:

  • Create script functions that retrieve property values, assign property values and that initialize the property with a default value.

  • Add information about object properties to the description file.

In this topic we will describe the creation of the functions that retrieve and set property values. The next topic will describe the creation of the initialization code. Then we will modify the description file.

In programming languages like C# or Delphi, you typically store property values by using private or protected fields. To store property values in script extensions, you use global script variables. These variables will act as private fields of the custom object: since the variables are global, they will be available within any method defined in the unit, but they will not be available outside of the object. Let’s create variables that will store values of the WMI object’s properties:

  • Open the wmiCode unit for editing.

  • Add the following code into the beginning of the unit:

    VBScript

    [wmiCode.vbs]
     

    Dim ComputerName
    Dim WMIService
    Dim MaxEventCount

Our object contains three properties, so we need three variables to store the properties’ values. The ComputerName and MaxEventCount variables will store the values of the object’s ComputerName and MaxEventCount properties. The WMIService variable will store a reference to the SWbemServices object.

In our example the names of the variables coincide with the name of the object’s properties. This will not cause a naming conflict, because the variables will not be available outside of the object. We used the same names to help you understand the code.

To obtain and specify the property values, you must create special script functions. The variables that contain property values will not be available outside of the object, so the property value can only be accessed via these functions. These functions can have any name, but according to their purpose, we call them get and set functions. In most cases the code of these functions is trivial: it simply returns a value of or assigns a value to the appropriate variable.

Let’s write the get and set functions for the properties of our object.

The Service property is read-only, so it only needs the get function to retrieve its value. Add the following code to the wmiCode unit:

VBScript

[wmiCode.vbs]

' The get method for the WMI.Service property
Function GetWMIService
    Set GetWMIService = WMIService
End Function

As you can see, the function’s code is trivial: it simply returns the value of the WMIService variable (this variable stores a SWbemServices object reference).

We would like to note that the get function can have any name. We used the string GetWMIService (that is, Get + property_name) just to make the name meaningful.

The MaxEventCount property is read-write, so we will create two functions for it:

VBScript

[wmiCode.vbs]

' The get method for the WMI.MaxEventCount property
Function GetMaxEventCount
    GetMaxEventCount = MaxEventCount
End Function

' The set method for the WMI.MaxEventCount property
Sub SetMaxEventCount(Count)
    MaxEventCount = Count
End Sub

The GetMaxEventCount function is the get function. It returns the value of the property. SetMaxEventCount is the set function. Its Count parameter specifies the value to be assigned to the property.

The ComputerName property is read-write, so again, we will need to create the get and set functions for it. The get function, GetComputerName, simply returns the value of the ComputerName variable:

VBScript

[wmiCode.vbs]

' The get method for the WMI.ComputerName property
Function GetComputerName
    GetComputerName = ComputerName
End Function

The set function is a bit more complex: besides assigning its Name parameter value to the ComputerName property, it needs to connect to the WMI services on the specified computer. We’ll place the code that establishes the WMI connection into a helper routine, ConnectToComputerInternal, and call this routine from the set function. We’ll also define a number of global constants that will be used in the ConnectToComputerInternal routine. We’ll place the constant definitions at the beginning of the wmiCode.vbs unit so that it is easier to maintain the constants and change their values if needed:

VBScript

[wmiCode.vbs]

Const CLocalComputer         = "."
Const CDefaultNamespace      = "Root\CIMv2"
Const CDefaultLocale         = Null
Const CDefaultAuthority      = Null
Const CZeroConnectionTimeout = &H0
Const AuthenticationLevel_Default    = 0
Const ImpersonationLevel_Impersonate = 3
Const Privilege_ChangeNotify   = "SeChangeNotifyPrivilege"
Const Privilege_RemoteShutdown = "SeRemoteShutdownPrivilege"
Const Privilege_Restore        = "SeRestorePrivilege"
Const Privilege_Shutdown       = "SeShutdownPrivilege"

...

' The set method for the WMI.ComputerName property
Sub SetComputerName(Name)
  ComputerName = Name
  ConnectToComputerInternal Null, Null
End Sub

' Connects to WMI on a computer using the specified credentials and security settings
Sub ConnectToComputerInternal(User, Password)
  ' Specify the local computer's name by default
  If IsNull(ComputerName) Then
    ComputerName = CLocalComputer
  End If

  ' Specify security settings for the WMI connection
  Dim Locator
  Set Locator = CreateObject("WbemScripting.SWbemLocator")
  Locator.Security_.AuthenticationLevel = AuthenticationLevel_Default    ' Use default server-side settings
  Locator.Security_.ImpersonationLevel = ImpersonationLevel_Impersonate  ' Use the client's security credentials
  Locator.Security_.Privileges.AddAsString(Privilege_ChangeNotify)
  Locator.Security_.Privileges.AddAsString(Privilege_Shutdown)
  Locator.Security_.Privileges.AddAsString(Privilege_RemoteShutdown)
  Locator.Security_.Privileges.AddAsString(Privilege_Restore)

  ' Connect to WMI on the specified computer
  Set WMIService = Nothing
  Set WMIService = Locator.ConnectServer _
    (ComputerName, CDefaultNamespace, User, Password, CDefaultLocale, CDefaultAuthority, CZeroConnectionTimeout)
  Set Locator = Nothing
End Sub

The ConnectToComputerInternal routine has two parameters that specify the user name and password to use when connecting to WMI on the computer. We pass nulls as parameter values so that the routine uses the user account that TestComplete is running under.

We created code for the get and set functions of all the properties. In the next topic we will describe how to specify the default values for the properties.

Prev     Next

See Also

Creating Runtime Objects - Basic Concepts
Creating Object Properties
Creating Runtime Objects

Highlight search results