I am a software engineer. I have been working with C++, MFC, and net technologies for 15 years. I like video games and reading books.
What Is WMI?
WMI is Windows Management Instrumentation, and it will help dot net framework to retrieve the system information. WMI is a separate world of classes dealing with accessing the system resources. The WMI classes usually used for retrieving the system information. Making changes to system information is rarely done through the WMI interfaces. In this article, we will look at how do we create WMI instances. I am going to retrieve all the Services running in the system and will display those in the List item.
Create Window Based Application (Form Design)
The first step is creating the windows application. Follow the steps given below:
- First, Create a New Visual C#, Windows Application Project named CreateInstance
- Once the form is displayed, design it as shown below:
- Listbox control. Its Name property is set as ctrlLstBox
- Button control. Its Name property is set as btnServices. Then Text property is set as Get Services.
- Windows Form. Its Text property is set as Get WMI Instances
Once the design is finished, the click event for the btnServices button is created. To create that event, in the design view double-click the button Get Services.
Double clicking the Get Services button will leave you in the below-given function. That function is actually the click event handler for the Get Services button.
We are going to write out the code for this handler function. So when a user clicks the button, the code written on the handler runs and displays the output in the ListBox control named ctrlLstBox.
Before we start our coding, we should include the required DLL reference to our CreateInsance project. To do this, click the Project->Add Reference… menu item. From the displayed dialog (It may take a while to display the dialog), we should select the System.Management item and then click OK. To select the System.Management, make sure you are in the .Net tab. This is shown in the below picture:
After adding this reference, our project has access to the functionality provided by the DotNet DLL, System.Management.dll. We can verify whether reference is successfully added to the project or not by checking the reference node from the project explorer as shown below:
Once the reference is ready we start our coding with these steps.
1) The first step is adding the above-created reference to the Top of the file (After the existing the using statement). This will enable us accessing the class kept inside the System.Management namespace. This code is shown below:
2) In the button click event handler, first, a “system.management.managementclass” is created. We passed Win32_Services to its constructor and doing so we get the Win32 Service class at runtime. From this class, we can get all the instances that belong to it. Below is the code that shows creating the ManagementClass class named service_class.
3) All the objects belonging to the Win32_Service is retrieved from the service_class instance that we created from the previous step of this coding. The call to the function GetInstances() will retrieve all the Services running in the in the system as ManagementObject Collection. This is shown in the below code:
4) Next, we get each instance of the class ManagementObject by iterating through the Service_objects_collection. The Foreach construct is used to do the iteration. Once we have the ManagementObject instance available inside the body of the Foreach loop, we are retrieving the name property of the instance and adding it to the Listbox Control. The code is shown below:
The screenshot after executing the button click handler is shown below:
The complete code in the File is shown below:
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.
© 2013 sirama