Marshal.GetActiveObject(String) 方法

定义

从运行对象表 (ROT) 获取指定对象的运行实例。Obtains a running instance of the specified object from the 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)。The programmatic identifier (ProgID) of the object that was requested.

返回

Object

所请求的对象;否则为 nullThe object that was requested; otherwise null. 可将此对象转换为它支持的任何 COM 接口。You can cast this object to any COM interface that it supports.

属性

例外

找不到该对象。The object was not found.

示例

以下示例在配置了运行 Microsoft Word 实例的计算机上运行。The following example was run on a computer that was configured with a running instance of Microsoft Word. Microsoft Excel 没有运行的实例。There were no instances of Microsoft Excel running.

该示例调用了 GetActiveObject 两次。The example calls GetActiveObject twice. 第一次调用尝试检索对 Microsoft Word 实例的引用 (Word.Application) 对象的实例。The first call tries to retrieve a reference to an instance of Microsoft Word (an instance of the Word.Application object). 第二次调用尝试检索对 (对象实例) 的 Microsoft Excel 实例的引用 Excel.ApplicationThe second call tries to retrieve a reference to an instance of Microsoft Excel (an instance of an Excel.Application object).

此代码成功检索对 Microsoft Word 实例的引用。The code retrieves a reference to an instance of Microsoft Word successfully. 但是,由于 Microsoft Excel 未运行,尝试检索第二个对象时将引发 COMExceptionHowever, because Microsoft Excel is not running, the attempt to retrieve the second object raises a 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
'

注解

GetActiveObject公开 OLEAUT32.DLL 中的 COM GetActiveObject函数。 .DLL但是, 后者需要类标识符 (CLSID), 而不是此方法所需ProgID的编程标识符 ()。GetActiveObject exposes the COM GetActiveObject function from OLEAUT32.DLL; however, the latter expects a class identifier (CLSID) instead of the programmatic identifier (ProgID) expected by this method. 若要在未注册的情况下获取 COM 对象的运行实例 ProgID ,请使用平台调用来定义 Com GetActiveObject 函数。To obtain a running instance of a COM object without a registered ProgID, use platform invoke to define the COM GetActiveObject function. 有关平台调用的说明,请参阅 使用非托管 DLL 函数For a description of platform invoke, see Consuming Unmanaged DLL Functions.

ProgID 和 CLSIDProgID and CLSID

注册表的 HKEY_CLASSES_ROOT 子树中的键包含多种子项类型。Keys in the HKEY_CLASSES_ROOT subtree of the registry contain a variety of subkey types. 大多数子项均为 Progid,这会将用户友好的字符串映射到 CLSID。Most of the subkeys are ProgIDs, which map a user-friendly string to a CLSID. 应用程序通常使用这些可读取的字符串而不是数字 Clsid。Applications often use these human-readable strings instead of the numeric CLSIDs. 通常,组件具有独立于版本的 ProgID,该 ProgID 映射到系统上安装的最新版本的组件。Often, a component has a version-independent ProgID that is mapped to the latest version of the component that is installed on the system.

应用程序和组件主要使用 Progid 检索其相应的 Clsid。Applications and components primarily use ProgIDs to retrieve their corresponding CLSIDs.

适用于