Management objects (representing WMI classes) can have methods that can be invoked through management client applications. For example, a disk object (representing the Win32_LogicalDisk class) might expose a "format" method, or a service object (representing the Win32_Service class) can have "start" and "stop" methods.
Example
The following code is an example of how to invoke a static WMI method. The example invokes the Create method of the Win32_Process class using parameter objects to launch Calc.exe. For more information, see "Create Method of the Win32_Process Class" in the Windows Management Instrumentation documentation in the MSDN Library at http://msdn.microsoft.com/library. The example also shows how to invoke the Create method of the Win32_Process class using an array of arguments to launch Notepad.exe. In this particular case, the method is static and is invoked on the class itself; however, methods will typically be invoked on instances.
Imports System
Imports System.Management
Public Class InvokeMethod
Public Overloads Shared Function _
Main(ByVal args() As String) As Integer
'Get the object on which the method will be invoked
Dim processClass As New ManagementClass("Win32_Process")
' Option 1: Invocation using parameter objects
'================================================
'Get an input parameters object for this method
Dim inParams As ManagementBaseObject
inParams = processClass.GetMethodParameters("Create")
'Fill in input parameter values
inParams("CommandLine") = "calc.exe"
'Execute the method
Dim outParams As ManagementBaseObject
outParams = processClass.InvokeMethod("Create", _
inParams, Nothing)
'Display results
'Note: The return code of the method is provided
'in the "returnValue" property of the outParams object
Console.WriteLine("Creation of calculator " & _
"process returned: {0}", outParams("returnValue"))
Console.WriteLine("Process ID: {0}", _
outParams("processId"))
' Option 2: Invocation using args array
'=======================================
'Create an array containing
' all arguments for the method
Dim methodArgs(4) As Object
methodArgs(0) = "notepad.exe"
methodArgs(1) = Nothing
methodArgs(2) = Nothing
methodArgs(3) = 0
'Execute the method
Dim result As Object
result = processClass.InvokeMethod( _
"Create", methodArgs)
'Display results
Console.WriteLine("Creation of process " & _
"returned: {0}", result)
Console.WriteLine("Process id: {0}", _
methodArgs(3))
End Function
End Class
using System;
using System.Management;
public class InvokeMethod
{
public static void Main()
{
//Get the object on which the method will be invoked
ManagementClass processClass =
new ManagementClass("Win32_Process");
// Option 1: Invocation using parameter objects
//================================================
//Get an input parameters object for this method
ManagementBaseObject inParams =
processClass.GetMethodParameters("Create");
//Fill in input parameter values
inParams["CommandLine"] = "calc.exe";
//Execute the method
ManagementBaseObject outParams =
processClass.InvokeMethod("Create", inParams, null);
//Display results
//Note: The return code of the method is provided
// in the "returnValue" property of the outParams object
Console.WriteLine("Creation of calculator " +
"process returned: " + outParams["returnValue"]);
Console.WriteLine("Process ID: " + outParams["processId"]);
// Option 2: Invocation using args array
//=======================================
//Create an array containing all arguments for the method
object[] methodArgs = {"notepad.exe", null, null, 0};
//Execute the method
object result = processClass.InvokeMethod ("Create", methodArgs);
//Display results
Console.WriteLine ("Creation of process returned: " + result);
Console.WriteLine ("Process id: " + methodArgs[3]);
}
}
The following code is an example of how to invoke a WMI method on an instance of a WMI class. The example invokes the StartService method of the Win32_Service class on the instance of the class where the Name property equals 'PlugPlay' (the Plug and Play service). For more information, see "StartService Method of the Win32_Service Class" in the Windows Management Instrumentation documentation in the MSDN Library at http://msdn.microsoft.com/library.
Imports System
Imports System.Management
Imports System.Windows.Forms
Namespace WMISample
Public Class CallWMIMethod
Public Overloads Shared Function Main() As Integer
Try
Dim classInstance As New ManagementObject( _
"root\CIMV2", _
"Win32_Service.Name='PlugPlay'", _
Nothing)
' no method [in] parameters to define
' Execute the method and obtain the return values.
Dim outParams As ManagementBaseObject = _
classInstance.InvokeMethod( _
"StartService", Nothing, Nothing)
' List outParams
Console.WriteLine("Out parameters:")
Console.WriteLine( _
"ReturnValue: {0}", outParams("ReturnValue"))
Catch err As ManagementException
MessageBox.Show("An error occured while trying" & _
" to execute the WMI method: " & err.Message)
End Try
End Function
End Class
End Namespace
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class CallWMIMethod
{
public static void Main()
{
try
{
ManagementObject classInstance =
new ManagementObject("root\\CIMV2",
"Win32_Service.Name='PlugPlay'",
null);
// no method [in] parameters to define
// Execute the method and obtain the return values.
ManagementBaseObject outParams =
classInstance.InvokeMethod(
"StartService", null, null);
// List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " +
outParams["ReturnValue"]);
}
catch(ManagementException err)
{
MessageBox.Show(
"An error occured while trying to execute" +
" the WMI method: " + err.Message);
}
}
}
}
See Also