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). System.Runtime.InteropServices.Marshal class does not exhibit GetActiveObject() anymore. Therefore, P/Invoke is required to call the underlying WinAPI's function. 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;
}
}
}
$src = get-content -raw PInvoke.cs
add-type -typeDefinition $src
$acc = [TQ84.COM]::getActiveObject('Access.Application')