Marshal.GetActiveObject(String) 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从运行对象表 (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 备注。