Marshal.GetActiveObject Method
Obtains a running instance of the specified object from the running object table (ROT).
Assembly: mscorlib (in mscorlib.dll)
Parameters
- progID
- Type: System.String
The programmatic identifier (ProgID) of the object that was requested.
Return Value
Type: System.ObjectThe object that was requested; otherwise null. You can cast this object to any COM interface that it supports.
| Exception | Condition |
|---|---|
| COMException |
The object was not found. |
GetActiveObject 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 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 + "\")"); 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 */
-
SecurityCriticalAttribute
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.
I have an application using this method that has been running for months. Last week i was notified the method was not running anymore, throwing this error:
System.Runtime.InteropServices.COMException
(0x800401F3): Invalid class string (Exception from HRESULT: 0x800401F3
(CO_E_CLASSSTRING))
at System.Runtime.InteropServices.Marshal.CLSIDFromProgID(String progId, Guid& clsid)
at System.Runtime.InteropServices.Marshal.GetActiveObject(String progID)
at myApp.OutlookMessagePoster.DoPostMessage(MessageToPaste message, IntPtr hWnd)
I am not sure what changed (sorry!); I would say the change it was not on the source code itself but something "external" to the app. Windows updates or patches? some registry change? Policy change? security/permissions configuration? a new version of the antivirus???... i wish i could isolate it.
Any ideas? Thanks in advance!!
Marcelo
http://support.microsoft.com/kb/316126 and
http://support.microsoft.com/kb/316125/EN-US/
Note that the code line "this.Activate();" is required.
The second article provides a workaround that is essentially repeated tries to allow the registration to complete.
- 1/13/2011
- LouOttawa