GetActiveObject Method
Collapse the table of content
Expand the table of content

Marshal.GetActiveObject Method (String)


Obtains a running instance of the specified object from the running object table (ROT).

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

public static object GetActiveObject(
	string progID


Type: System.String

The programmatic identifier (ProgID) of the object that was requested.

Return Value

Type: System.Object

The object that was requested; otherwise null. You can cast this object to any COM interface that it supports.

Exception Condition

The object was not found.

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. To obtain a running instance of a COM object without a registered ProgID, use platform invoke to define the COM GetActiveObject function. For a description of platform invoke, see Consuming Unmanaged DLL Functions.

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 GetActiveObject 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.

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 + "\")");
           { 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.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


requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft