Search
Enterprise

Programming Windows Services with Microsoft Visual Basic 2008 : Extending the WMI Implementation

Published 7 min readBy Programming4us Editorial Team

The current WMI implementation isn’t a very scalable solution, so we’ll implement the same strategies that we used for file monitoring.

Extending the WMI Class

We need to create a more robust and scalable WMI class that allows for multiple instances of monitored servers. In addition, we want to be able to predefine a query that we can use to retrieve specific information.

Because of the extensive nature of changes to the WMI class, I will reprint the code in Listing 1 and then review the changes.

Listing 1. Extended WMI class.

New WMI Member Variables

As shown in Listing 2, we are going to add several new WMI member variables, which will hold the pointers to our internal WMI implementation, and our new thread-state management class instance.

Listing 2. WMI class member variables.

As you can see, we have only one thread per WMI instance because no e-mail or incoming file processing is required.

New and Updated WMI Class Member Methods

The new WMI class implementation has several properties that are used to relate to the WMI Management namespace in Microsoft Visual Studio. The following list includes these changes to the WMI class and a brief description of how they correlate to their WMI representations.

The <New> Constructor

Because we are now using thread state management, I have updated the <New> constructor so that, we can pass in a shared instance of the thread state management object, as well as the newly created WMIWorkerOptions class.

The WMIWorkerOptions Class

Note that the WMIWorkerOptions class is very close in design and detail to the FileWorkerOptions class. It is used for the same purpose—to store WMI specific configuration information on a per-class instance basis.

The WMI Properties

As with the FileWorker class, we need to store certain pieces of information outside the scope of our WMIWorkerOptions class, so we will implement it directly in our WMI class.

  • The WMI Path The WMI path represents the physical and logical connection to the local or remote computer where the WMI classes that you want to work with are implemented. ManagementPath is given a string to represent the FQDN of the root node and object you want to query. In our class, we defined this as the property Path.

  • The WMI Scope The WMI scope represents the connection to the root node as defined in the ManagementPath variable associated with the scope. This is where you will implement the underlying details of the connection to the computer and therefore to the WMI objects you want to interface with. In our class, we define this as the property Scope.

  • The <ProcessWMIRequest> Method The <ProcessWMIRequest> is like the <ProcessFiles> method in our FileWorker class. It defines the entry point for the thread created on a per-WMI instance basis. This is where we connect to the local or remote WMI services, query the data we are looking for, and then process the data.

Creating the WMIWorkerOptions Class

We need to be able to pass into our new WMI class the properties that we will define in our configuration file, which we will discuss in the next section. For this I have created the class shown in Listing 3.

Listing 3. WMIWorkerOptions class.

The Query Property

The Query property represents the WMI query that will be passed to this instance from our configuration file. The query may differ slightly based on the operating system.

The WMIRoot Property

The WMIRoot property represents the root node of the local or remote WMI class instance that you want to query. From here you can query or use other subordinate classes or methods.

The Server Property

The Server property represents the server that we want to connect to. It can also represent the local computer by either using the local computer name or by using a period, which also represents the local computer.

Creating the Configuration File

We will create a configuration file that we can use to identify the preceding properties to our WMI class instances. Listing 4 shows the XML file that we are using. 

Listing 4. WMI configuration XML file.

The configuration file shown previously in Listing 3 contains the matching properties from our WMIWorkerOptions class. You will notice that I have included the WMIWorkerOptions section twice. This is to test that we can get multiple threads to perform the work. Future implementations of the service can either point to different servers or use different queries to customize the service usage.

WMI Service Account

WMI communicates over RPC to the local and remote servers. You need to ensure that on the computer you want to communicate with, the service or the threads using WMI are running in the context of a user with the proper privileges.

After installing the service and implementing your own configuration file, you will see at least one Application log entry for each entry in the configuration file. The intent here is to show that you can use WMI to query both local and remote information.