ManagementObject.InvokeMethod 方法

定義

在物件上叫用方法。Invokes a method on the object.

多載

InvokeMethod(String, Object[])

在物件上叫用方法。Invokes a method on the object.

InvokeMethod(ManagementOperationObserver, String, Object[])

以非同步方式,在物件上叫用方法。Invokes a method on the object, asynchronously.

InvokeMethod(String, ManagementBaseObject, InvokeMethodOptions)

在 WMI 物件上叫用方法。Invokes a method on the WMI object. 輸入和輸出參數會表示做為 ManagementBaseObject 物件。The input and output parameters are represented as ManagementBaseObject objects.

InvokeMethod(ManagementOperationObserver, String, ManagementBaseObject, InvokeMethodOptions)

以非同步方式,在物件上叫用方法。Invokes a method on the object, asynchronously.

InvokeMethod(String, Object[])

在物件上叫用方法。Invokes a method on the object.

public:
 System::Object ^ InvokeMethod(System::String ^ methodName, cli::array <System::Object ^> ^ args);
public object InvokeMethod (string methodName, object[] args);
member this.InvokeMethod : string * obj[] -> obj
Public Function InvokeMethod (methodName As String, args As Object()) As Object

參數

methodName
String

要執行的方法名稱。The name of the method to execute.

args
Object[]

含有參數值的陣列。An array containing parameter values.

傳回

Object

方法傳回的物件值。The object value returned by the method.

範例

下列範例會叫用 Win32_Process:: Create 方法來啟動 Notepad.exe 的新進程。The following example invokes the Win32_Process::Create method to start a new process of Notepad.exe.

using System;
using System.Management;

// This sample demonstrates invoking
// a WMI method using an array of arguments.
public class InvokeMethod
{
    public static void Main()
    {

        // Get the object on which the
        // method will be invoked
        ManagementClass processClass =
            new ManagementClass("Win32_Process");

        // 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]);
    }
}
Imports System.Management

' This sample demonstrates invoking a WMI method
' using an array of arguments.
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")

        ' Create an array containing all arguments 
        ' for the method
        Dim methodArgs() As Object = _
            {"notepad.exe", Nothing, Nothing, 0}

        ' Execute the method
        Dim result As Object = _
            processClass.InvokeMethod("Create", methodArgs)

        ' Display results
        Console.WriteLine( _
            "Creation of process returned: {0}", result)
        Console.WriteLine( _
            "Process id: {0}", methodArgs(3))
        Return 0
    End Function
End Class

備註

如果是靜態方法,則執行應該仍會成功。If the method is static, the execution should still succeed.

.NET Framework 安全性.NET Framework Security

完全信任立即呼叫者。Full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code. 如需詳細資訊,請參閱 使用來自部分信任程式碼的程式庫For more information, see Using Libraries from Partially Trusted Code.

適用於

InvokeMethod(ManagementOperationObserver, String, Object[])

以非同步方式,在物件上叫用方法。Invokes a method on the object, asynchronously.

public:
 void InvokeMethod(System::Management::ManagementOperationObserver ^ watcher, System::String ^ methodName, cli::array <System::Object ^> ^ args);
public void InvokeMethod (System.Management.ManagementOperationObserver watcher, string methodName, object[] args);
member this.InvokeMethod : System.Management.ManagementOperationObserver * string * obj[] -> unit
Public Sub InvokeMethod (watcher As ManagementOperationObserver, methodName As String, args As Object())

參數

watcher
ManagementOperationObserver

要接收作業結果的物件。The object to receive the results of the operation.

methodName
String

要執行的方法名稱。The name of the method to execute.

args
Object[]

含有參數值的陣列。An array containing parameter values.

備註

如果是靜態方法,則執行應該仍會成功。If the method is static, the execution should still succeed.

.NET Framework 安全性.NET Framework Security

完全信任立即呼叫者。Full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code. 如需詳細資訊,請參閱 使用來自部分信任程式碼的程式庫For more information, see Using Libraries from Partially Trusted Code.

適用於

InvokeMethod(String, ManagementBaseObject, InvokeMethodOptions)

在 WMI 物件上叫用方法。Invokes a method on the WMI object. 輸入和輸出參數會表示做為 ManagementBaseObject 物件。The input and output parameters are represented as ManagementBaseObject objects.

public:
 System::Management::ManagementBaseObject ^ InvokeMethod(System::String ^ methodName, System::Management::ManagementBaseObject ^ inParameters, System::Management::InvokeMethodOptions ^ options);
public System.Management.ManagementBaseObject InvokeMethod (string methodName, System.Management.ManagementBaseObject inParameters, System.Management.InvokeMethodOptions options);
member this.InvokeMethod : string * System.Management.ManagementBaseObject * System.Management.InvokeMethodOptions -> System.Management.ManagementBaseObject
Public Function InvokeMethod (methodName As String, inParameters As ManagementBaseObject, options As InvokeMethodOptions) As ManagementBaseObject

參數

methodName
String

要執行的方法名稱。The name of the method to execute.

inParameters
ManagementBaseObject

ManagementBaseObject,保存方法的輸入參數。A ManagementBaseObject holding the input parameters to the method.

options
InvokeMethodOptions

InvokeMethodOptions,含有執行方法的其他選項。An InvokeMethodOptions containing additional options for the execution of the method.

傳回

ManagementBaseObject

ManagementBaseObject,含有執行方法的輸出參數和傳回值。A ManagementBaseObject containing the output parameters and return value of the executed method.

範例

下列範例會叫用 Win32_Process:: Create 方法來啟動 Calc.exe 的新進程。The following example invokes the Win32_Process::Create method to start a new process of Calc.exe.

using System;
using System.Management;

// This sample demonstrates invoking
// a WMI method using parameter objects
public class InvokeMethod
{
    public static void Main()
    {

        // Get the object on which the method will be invoked
        ManagementClass processClass =
            new ManagementClass("Win32_Process");

        // 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"]);
    }
}
Imports System.Management

' This sample demonstrates invoking
' a WMI method using parameter objects
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")

        ' Get an input parameters object for this method
        Dim inParams As ManagementBaseObject = _
            processClass.GetMethodParameters("Create")

        ' Fill in input parameter values
        inParams("CommandLine") = "calc.exe"

        ' Execute the method
        Dim outParams As ManagementBaseObject = _
            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"))

        Return 0
    End Function
End Class

備註

.NET Framework 安全性.NET Framework Security

完全信任立即呼叫者。Full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code. 如需詳細資訊,請參閱 使用來自部分信任程式碼的程式庫For more information, see Using Libraries from Partially Trusted Code.

適用於

InvokeMethod(ManagementOperationObserver, String, ManagementBaseObject, InvokeMethodOptions)

以非同步方式,在物件上叫用方法。Invokes a method on the object, asynchronously.

public:
 void InvokeMethod(System::Management::ManagementOperationObserver ^ watcher, System::String ^ methodName, System::Management::ManagementBaseObject ^ inParameters, System::Management::InvokeMethodOptions ^ options);
public void InvokeMethod (System.Management.ManagementOperationObserver watcher, string methodName, System.Management.ManagementBaseObject inParameters, System.Management.InvokeMethodOptions options);
member this.InvokeMethod : System.Management.ManagementOperationObserver * string * System.Management.ManagementBaseObject * System.Management.InvokeMethodOptions -> unit
Public Sub InvokeMethod (watcher As ManagementOperationObserver, methodName As String, inParameters As ManagementBaseObject, options As InvokeMethodOptions)

參數

watcher
ManagementOperationObserver

ManagementOperationObserver,用來處理非同步 (Asynchronous) 執行的進度和結果。A ManagementOperationObserver used to handle the asynchronous execution's progress and results.

methodName
String

要執行的方法名稱。The name of the method to be executed.

inParameters
ManagementBaseObject

ManagementBaseObject,含有方法的輸入參數。A ManagementBaseObject containing the input parameters for the method.

options
InvokeMethodOptions

InvokeMethodOptions,含有用來執行方法的其他選項。An InvokeMethodOptions containing additional options used to execute the method.

備註

方法會叫用指定的方法執行,然後傳回。The method invokes the specified method execution and then returns. 進度和結果是透過中的附隨報告 ManagementOperationObserverProgress and results are reported through events on the ManagementOperationObserver.

.NET Framework 安全性.NET Framework Security

完全信任立即呼叫者。Full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code. 如需詳細資訊,請參閱 使用來自部分信任程式碼的程式庫For more information, see Using Libraries from Partially Trusted Code.

適用於