Network programming made easy using WMI - Part One
tutorials
WMI
home /

Network programming made easy using WMI � Part One

Contents

Abstract of this Article

For long, software engineers have avoided network programming for 2 reasons. They either think that they
don�t need it or think that it is too complicated. This article attempts to

  • Help us understand that network programming is slowly becoming part of application programming
  • Make engineers say �Wow! I never knew that Network programming is so easy.�


Though WMI is a very vast concept, which cannot be explained in one single article, I�ve tried to do
my best to give you a picture. Part Two of this article would focus on using WMI through C# and would
focus more on the technical aspects.

The need

Network programming has for long been in the domain of system programmers and geeks. Application
programmers have ignored it till date, as they did not have to use it at all. But now, the scope of application
software is increasing and the client�s requirements are focusing more on convergence and distributed systems.
They are also interested in monitoring their application�s performance and obtaining the statistics. What if the
application is distributed on several machines on the network? It is slowly becoming mandatory for an application
programmer to code for networked systems and for hardware interfaces too. For example, a lot of customers
want their application to be able to send and receive SMS within their application itself. A few other customers
want hardware interfacing.Yet others want to be able to automate tasks on a network. If you need to verify if a
particular hardware interface is available or check the status of a windows service on a machine on the network
or for starting and stopping of services/processes on a server, you have to be familiar with network programming.

The Solution

Traditionally, a client tool would have to be written which has to be installed on the remote machine, which
you are planning to access, and you would use socket programming to communicate with your client. This is
not practically feasible, as you have to install your client on all machines that you plan to access. WMI solves
this problem by making the remote machine�s OS itself act as the client. This totally eliminates the need for the
client software. Also the need to understand socket programming as well as system programming is eliminated
using WMI.Using WMI, it becomes child�s play to write code for local and remote machines.

Microsoft has introduced WMI (Windows Management Instrumentation) as a set of scriptable COM objects to
handle the above-mentioned tasks. It means that you can use it from scripting languages such as VB script as
well as from VC++,C#, VB, Delphi, Python etc.,. WMI is preinstalled in Windows Server 2003, Windows XP,
Windows ME and Windows 2000.

To use WMI, all we need to know is basic ANSI SQL and any one of the languages mentioned above. WMI
uses a script called WQL (WMI Query Language) which is a subset of standard ANSI SQL. The use of WQL
makes it easier for application programmers to query for data. For example, if you want to select the number of
logical drives on a machine, you can simply use �Select * from Win32_SerialPort� or you can even use a query
like �Select Caption, DeviceId from Win32_SerialPort where Caption = �COM1��.

Pretty simple, isn�t it? To make it much simpler for programmers, Microsoft has brought out a automatic code
generator tool available for download Here.

Real time applications

  1. To query a remote machine about its hardware.
  2. To perform a backup on a remote server on a timely basis.
  3. To start / stop services or processes on a local/remote machine.
  4. To monitor the event log / IIS log / MSSQL server log on a local/remote machine.
  5. To monitor a MSSQL server for number of active connections / deadlocks.
  6. To verify if a particular user account (E.g. ASP.NET) has been created on a remote machine.
  7. To check if IIS is running on a remote machine.
On the darker side, it is possible for the system administrator to check if yahoo messenger has been installed
on any machine on the network and uninstall it sitting on his/her console. Imagine an automated application
doing this every night :( .

The possibilities are endless. In short, you can query a remote machine for any data that it contains IF you
have the access rights to that machine. You can also execute tasks on the remote machine.

WMI Architecture




From the diagram above, we can see that using the System.Management namespace, we can access the
functionality of WMI. System.Management greatly simplifies the usage. Web applications as well as
windows applications can use its functionality.

C++ applications can interact with the WMI core directly through the WMI COM API and Java
applications can interact with it through JNI.

WMI Code Creator

The WMI Code Creator is a WYCIWYG (What You Choose Is What You Get) tool. It can generate
the code if you specify the details you need. For e.g. if you need to check the availability of a particular
Serial port, you just need to select Win32_SerialPort from the �Classes� combo box and you can select
all or any property that you need from the Property selection list box. The code would be automatically
generated for you in the �Generated Code� edit box. To verify if the code generated is what you need,
you can click the �Execute Code� button at the bottom of the window.

Sample Scenario:

Let us see a sample scenario. In my machine, I need to enumerate the serial ports and also need a
description of each serial port to check if I can use it. To do just that, I have to open the WMI code
Creator tool, select Win32_SerialPort from the �Classes� combo box and select �Caption� and
"DeviceID" from the Property selection list box. The code would be automatically generated in the
�Generated Code� edit box. To verify if the code generated is what I need, I can use the button
�Execute Code� at the bottom of the window.




On clicking the �Execute Code� button, the output generated on my machine is shown below.




You can select any of the available 400+ classes and the properties associated with it and even
write your own WQL statements when necessary to get the output you desire. To execute events on
a local/remote machine, you can select the �Execute a Method� tab from the code creator to browse
through the namespaces that you can use and select what you want to execute on the specified machine.

To select the code language you need, you can select one of the options from the �Code Language�
menu and in turn, the appropriate code would be generated for you.




If you want to target a remote computer / local computer / a group of computers on the network,
you can simply choose the �Target Computer� from the menu and the code would be generated
appropriately for you.




Relevant Links

   Download the WMI Code Creator
   WQL Reference Guide
   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_start_page.asp
   http://www.activexperts.com/activmonitor/windowsmanagement/wmi/samples/
   http://aspalliance.com/629
   http://en.wikipedia.org/wiki/Windows_Management_Instrumentation

Hosted by www.Geocities.ws

1