Marshal.GetActiveObject(String) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
The programmatic identifier (ProgID) of the object that was requested.
The object that was requested; otherwise null
. You can cast this object to any COM interface that it supports.
- Attributes
The object was not found.
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
*/
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
'
For more information about this API, see Supplemental API remarks for Marshal.GetActiveObject.
Product | Versions |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: