Export (0) Print
Expand All

How To: Execute a Method 

.NET Framework 2.0

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.

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.

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

Community Additions

ADD
Show:
© 2014 Microsoft