WMI Programming with Visual Basic.NET: Tips and Tricks (Page 1 of 5 )
In my previous article, part four of this series, we looked into managing OS using WMI methods. In this article, we will look into some tips and tricks for using WMI.
You can download the zip of entire Visual Studio.NET solution (developed for this article)
here.
The first part of my series gives you the details for creating a Visual Studio.NET solution for WMI from scratch. It will not be repeated anymore in any of the future articles in this series.
Can we start another application (process) through WMI?
In all of my examples in the previous part, we worked with windows services (or system services). Now I will start talking about processes. A process is something like a thread which starts execution in its only memory area (application domain). In general, every application starts in the form of a process. Almost all processes can be viewed using the task manager. Some processes may be system processes which run in the background (without having a user interface). Every process gets identified by a process id internally. We can get the full list of processes by using WMI class “Win32_process”. You can refer to part three to get an entire example of listing processes.
Now, the question is, can we start our own process (or even an application) using WMI? I hope anyone who works with .NET would definitely know how to do it using the native .NET framework. But now, I would like to do the same thing using WMI. The following program fragment demonstrates this:
' Get the object on which the method will be invoked
DimprocessClassAsNewManagementClass("Win32_Process")
' Get an input parameters object for this method
DiminParamsAsManagementBaseObject = processClass.GetMethodParameters("Create")
' Fill in input parameter values
inParams("CommandLine") = "calc.exe"
' Execute the method
DimoutParamsAsManagementBaseObject = processClass.InvokeMethod("Create", inParams,Nothing)
DimobjAsObject= outParams("ReturnValue")
Ifobj.ToString <> "0"Then
MessageBox.Show("Could not Start, Returned code:" & obj.ToString)
EndIf
A brief explanation of above program is included below.
We create a new process by using WMI class “Win32_Process”. There exists a method “create” within the class “Win32_Process”. That method creates a process at the OS through WMI and not our application. We provide parameters to that object using "inParams" (such as which application to execute as part of the process).
We execute the “create” method using the “InvokeMethod” of the “ManagementClass” object (currently the object points to a new process). The result of the “InvokeMethod” is returned into “outParams”. If the result is zero, it has successfully managed to invoke an external process. A non-zero result is a failure which would be shown through a message box. A detailed explanation of every return code is provided in the WMI SDK documentation.
You can understand the above program very easily, if you go through my previous article (part four). All information about calling a WMI method (including the “InvokeMethod” member) was discussed in detail in that article.
Next: Retrieving all properties of a WMI class without knowing their names >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee