Search notes:

System.Runtime.InteropServices.Marshal - GetActiveObject()

The method GetActiveObject of the System.Runtime.InteropServices class exposes the COM function GetActiveObject from oleaut32.dll (the latter expects a class identifier (CLSID) instead of ProgID).

Missing GetActiveObject method in .NET Core

In .NET Core , the System.Runtime.InteropServices.Marshal class does not exhibit GetActiveObject() anymore. Therefore, P/Invoke is required to call the underlying WinAPI's function.

PInvoke.cs

This is the C# source file for the P/Invocation for GetActiveObject():
using System;
using System.Runtime.InteropServices;

namespace TQ84 {

   public class COM {

     [DllImport("oleaut32.dll", PreserveSig=false)]
      static extern void GetActiveObject(
                                            ref Guid   rclsid,
                                                IntPtr pvReserved,
        [MarshalAs(UnmanagedType.IUnknown)] out Object ppunk
      );

     [DllImport("ole32.dll")]
      static extern int CLSIDFromProgID(
         [MarshalAs(UnmanagedType.LPWStr)]      string lpszProgID,
                                            out Guid   pclsid
      );

      public static object getActiveObject(string progId) {
         Guid clsid;
         CLSIDFromProgID(progId, out clsid);

         object obj;
         GetActiveObject(ref clsid, IntPtr.Zero, out obj);

         return obj;
      }
   }
}

Using PInvoke.cs in PowerShell

In PowerShell, this source might be used like so:
$src = get-content -raw PInvoke.cs

add-type -typeDefinition $src

$acc = [TQ84.COM]::getActiveObject('Access.Application')

See also

The PowerShell module COM

Index