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

Marshal.GetActiveObject Method

Updated: October 2009

Obtains a running instance of the specified object from the Running Object Table (ROT).

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

[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static Object GetActiveObject(
	string progID


Type: System.String

The ProgID of the object being requested.

Return Value

Type: System.Object
The object requested. You can cast this object to any COM interface that it supports.


The object was not found.

Marshal.GetActiveObject exposes the GetActiveObject COM API method 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 GetActiveObject COM method. For a description of platform invoke, see Consuming Unmanaged DLL Functions. For additional information about the GetActiveObject COM method, see the MSDN Library.


This method uses SecurityAction.LinkDemand to prevent it from being called from untrusted code; only the immediate caller is required to have SecurityPermissionAttribute.UnmanagedCode permission. If your code can be called from partially trusted code, do not pass user input to Marshal class methods without validation. For important limitations on using the LinkDemand member, see Demand vs. LinkDemand.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0




October 2009

Made the missing example appear.

Content bug fix.

July 2009

Enhanced Remarks section.

Customer feedback.

Community Additions

© 2016 Microsoft