共用方式為


Marshal.GetActiveObject(String) 方法

定義

從執行物件表格 (Running Object Table,ROT) 取得指定之物件的執行中執行個體。

public:
 static System::Object ^ GetActiveObject(System::String ^ progID);
public static object GetActiveObject (string progID);
[System.Security.SecurityCritical]
public static object GetActiveObject (string progID);
static member GetActiveObject : string -> obj
[<System.Security.SecurityCritical>]
static member GetActiveObject : string -> obj
Public Shared Function GetActiveObject (progID As String) As Object

參數

progID
String

要求之物件的程式設計識別項 (ProgID)。

傳回

要求的物件;否則為 null。 您可以將這個物件轉換為其支援的任何 COM 介面。

屬性

例外狀況

找不到物件。

範例

下列範例是在使用執行中 Microsoft Word 實例所設定的電腦上執行。 沒有執行 Microsoft Excel 的實例。

此範例會呼叫 GetActiveObject 兩次。 第一次呼叫會嘗試擷取 Microsoft Word 實例的參考, (對象實例 Word.Application) 。 第二個呼叫會嘗試擷取 Microsoft Excel 實例的參考, (對象實例 Excel.Application) 。

程序代碼會成功擷取 Microsoft Word 實例的參考。 不過,因為 Microsoft Excel 未執行,所以嘗試擷取第二個 COMException物件會引發 。

using System;
using System.Runtime.InteropServices;

class MainFunction
{
    static void Main()
        {
        Console.WriteLine("\nSample: C# System.Runtime.InteropServices.Marshal.GetActiveObject.cs\n"); 

        GetObj(1, "Word.Application");
        GetObj(2, "Excel.Application");
        }

    static void GetObj(int i, String progID)
    {
        Object obj = null;

        Console.WriteLine("\n" +i+") Object obj = GetActiveObject(\"" + progID + "\")");
        try
           { obj = Marshal.GetActiveObject(progID); }
        catch (Exception e)
           {
           Write2Console("\n   Failure: obj did not get initialized\n" + 
                         "   Exception = " +e.ToString().Substring(0,43), 0); 
           }
 
        if (obj != null)
           { Write2Console("\n   Success: obj = " + obj.ToString(), 1 ); }
    }

    static void Write2Console(String s, int color)
        {
        Console.ForegroundColor = color == 1? ConsoleColor.Green : ConsoleColor.Red;
        Console.WriteLine(s); 
        Console.ForegroundColor = ConsoleColor.Gray;
    }
}

/*
Expected Output:

Sample: C# System.Runtime.InteropServices.Marshal.GetActiveObject.cs

1) Object obj = GetActiveObject("Word.Application")

   Success: obj = System.__ComObject

2) Object obj = GetActiveObject("Excel.Application")

   Failure: obj did not get initialized
   Exception = System.Runtime.InteropServices.COMException
*/

Imports System.Runtime.InteropServices

Module Module1

    Sub Main()
        Console.WriteLine(vbcrlf + "Sample: VB System.Runtime.InteropServices.Marshal.GetActiveObject.vb" + vbcrlf) 
        GetObj(1, "Word.Application")
        GetObj(2, "Excel.Application")
    End Sub


    Sub GetObj(ByVal i As Integer, ByVal progID As [String])
        Dim obj As [Object] = Nothing
        
        Console.WriteLine((vbLf & i & ") Object obj = GetActiveObject(""") + progID & """)")
        Try
            obj = Marshal.GetActiveObject(progID)
        Catch e As Exception
            Write2Console((vbLf & "   Failure: obj did not get initialized" & vbLf & "   Exception = ") + e.ToString().Substring(0, 43), 0)
        End Try
        
        If obj IsNot Nothing Then
            Write2Console(vbLf & "   Success: obj = " & obj.ToString(), 1)
        End If
    End Sub

    Sub Write2Console(ByVal s As [String], ByVal color As Integer)
        Console.ForegroundColor = If(color = 1, ConsoleColor.Green, ConsoleColor.Red)
        Console.WriteLine(s)
        Console.ForegroundColor = ConsoleColor.Gray
    End Sub

End Module

'Expected Output:
'
'Sample: VB System.Runtime.InteropServices.Marshal.GetActiveObject.vb
'
'1) Object obj = GetActiveObject("Word.Application")
'
'   Success: obj = System.__ComObject
'
'2) Object obj = GetActiveObject("Excel.Application")
'
'   Failure: obj did not get initialized
'   Exception = System.Runtime.InteropServices.COMException
'

備註

如需此 API 的詳細資訊,請參閱 Marshal.GetActiveObject 的補充 API 備註

適用於