Obtains a running instance of the specified object from the running object table (ROT).
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Shared Function GetActiveObject ( _ progID As String _ ) As Object
- Type: System.String
The programmatic identifier (ProgID) of the object that was requested.
Return ValueType: System.Object
The object that was requested; otherwise Nothing. You can cast this object to any COM interface that it supports.
exposes the COM GetActiveObject Function function from OLEAUT32.DLL; however, the latter expects a class identifier (CLSID) instead of the programmatic identifier (ProgID) expected by this method. To obtain a running instance of a COM object without a registered ProgID, use platform invoke to define the COM GetActiveObject Function function. For a description of platform invoke, see Consuming Unmanaged DLL Functions.
ProgID and CLSID
Keys in the HKEY_CLASSES_ROOT subtree of the registry contain a variety of subkey types. Most of the subkeys are ProgIDs, which map a user-friendly string to a CLSID. Applications often use these human-readable strings instead of the numeric CLSIDs. Often, a component has a version-independent ProgID that is mapped to the latest version of the component that is installed on the system.
Applications and components primarily use ProgIDs to retrieve their corresponding CLSIDs.
The following example was run on a computer that was configured with a running instance of Microsoft Word. There were no instances of Microsoft Excel running.
The example calls twice. The first call tries to retrieve a reference to an instance of Microsoft Word (an instance of the Word.Application object). The second call tries to retrieve a reference to an instance of Microsoft Excel (an instance of an Excel.Application object).
The code retrieves a reference to an instance of Microsoft Word successfully. However, because Microsoft Excel is not running, the attempt to retrieve the second object raises a COMException.
Imports System 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 '
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.