The purpose of this article is to provide a very basic introduction to WMI for Delphi developers who are unfamiliar with it. It contains example code to query WMI using Delphi.
What is WMI?
WMI (Windows Management Instrumentation) is a technological invention of Microsoft, whose purpose is to take care of the different operational environments of Windows. Or more precisely, as per Wikipedia:
Windows Management Instrumentation (WMI) consists of a set of extensions to the Windows Driver Model that provides an operating system interface through which instrumented components provide information and notification. WMI is Microsoft‘s implementation of the Web-Based Enterprise Management (WBEM) and Common Information Model (CIM) standards from the Distributed Management Task Force (DMTF).
It comes preinstalled since Windows 2000, and there are downloads available for Windows NT, Windows 95, and Windows 98. It has been around for a long time. You can learn all about it from the official Microsoft site, see here. Pluralsight also has a course available here. I’m sure there are lots of videos on YouTube as well.
What is it used for?
It is essentially used to help manage a Windows PC/network environment. You can use it to query information about PCs on the network, switch off monitors, listen for events such as a network controller going offline etc. It is extremely useful.
It’s been about 15 years since I last used it but recently someone piqued my interested again, so I took that as an excuse to revisit it. I absolutely love using technology like WMI.
The best way to dive into WMI is to use Powershell. Here, I’m displaying my connected monitors:
And here, I’m enumerating my video controllers:
There’s so much information and it is extremely accessible. To get started, there are many tutorials online and plenty of books like PowerShell and WMI to take you deeper.
Back in the old days, before the likes of Powershell, VBScript was the simplest way to manipulate COM objects, whether on the command line or via ASP (Active Server Pages). Having played a little with PowerShell, my nostalgia led me back to VBScript. Here is a sample script I used to query some monitor related information about my PC:
Essentially I’m just selecting information I am interested in, iterating over each result returned, and displaying specific properties. The easiest way to execute the script is via the Console using CScript.exe which writes to the console rather than displaying a popup dialog for each message:
And here are the results of executing this simple script:
Delphi has always had great support for Microsoft’s COM technologies, so it was no surprise it was extremely simple to replicate the VBScript in Delphi. For anyone interested, the code is available here on Github. It’s a very simple console application.
The class TWmi, located in the uWmi.pas file, is responsible for connecting to WMI and executing a query. It uses a visitor-like pattern to call back a procedure on each item.
The constructor configures the WMI providers:
And the Run function does the rest of the work:
From WinExample.dpr we simply use TWmi:
And the specified callback function is invoked on each item:
There is so much more you can do with WMI, such as listening for the WIFI controller to disconnect so you can respond appropriately in your application. This is just a very basic introduction, and a chance for me to reminisce and have some fun 🙂 I hope you may find this post useful.
Here is the output of the Delphi code, matching that of the VBScript.
There is an open source project on Github here that allows you to generate Object Pascal, Oxygene, C++ and C# code to access the WMI classes, events and methods. It also includes a set of tools to explore and query the content of the WMI. Definitely worth checking out.