No definition found for GetActiveObject from System.Runtime.InteropServices.Marshal C#

We need to pull the GetActiveObject(String ProgID) function from the source code GitHub.Microsoft

Create your own class, for example - Marshal2

And use as before

Marshal2.GetActiveObject(progID);

Source code

public static class Marshal2
{
    internal const String OLEAUT32 = "oleaut32.dll";
    internal const String OLE32 = "ole32.dll";

    [System.Security.SecurityCritical]  // auto-generated_required
    public static Object GetActiveObject(String progID)
    {
        Object obj = null;
        Guid clsid;

        // Call CLSIDFromProgIDEx first then fall back on CLSIDFromProgID if
        // CLSIDFromProgIDEx doesn't exist.
        try
        {
            CLSIDFromProgIDEx(progID, out clsid);
        }
        //            catch
        catch (Exception)
        {
            CLSIDFromProgID(progID, out clsid);
        }

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

    //[DllImport(Microsoft.Win32.Win32Native.OLE32, PreserveSig = false)]
    [DllImport(OLE32, PreserveSig = false)]
    [ResourceExposure(ResourceScope.None)]
    [SuppressUnmanagedCodeSecurity]
    [System.Security.SecurityCritical]  // auto-generated
    private static extern void CLSIDFromProgIDEx([MarshalAs(UnmanagedType.LPWStr)] String progId, out Guid clsid);

    //[DllImport(Microsoft.Win32.Win32Native.OLE32, PreserveSig = false)]
    [DllImport(OLE32, PreserveSig = false)]
    [ResourceExposure(ResourceScope.None)]
    [SuppressUnmanagedCodeSecurity]
    [System.Security.SecurityCritical]  // auto-generated
    private static extern void CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] String progId, out Guid clsid);

    //[DllImport(Microsoft.Win32.Win32Native.OLEAUT32, PreserveSig = false)]
    [DllImport(OLEAUT32, PreserveSig = false)]
    [ResourceExposure(ResourceScope.None)]
    [SuppressUnmanagedCodeSecurity]
    [System.Security.SecurityCritical]  // auto-generated
    private static extern void GetActiveObject(ref Guid rclsid, IntPtr reserved, [MarshalAs(UnmanagedType.Interface)] out Object ppunk);

}

This is available only .Net Framework but not for .Net Core . Please check the .Net project type .

https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.getactiveobject?view=netframework-4.8&viewFallbackFrom=netcore-2.2


Comments

  1. Yadiel

    • 2020/3/9

    InteropServices.Marshal C# · c# excel interop. I'm trying to connect to a running Excel instance, but when I try to use 

  2. Esteban

    • 2016/1/6

    Is there a substitue for System.Runtime.InteropServices.Marshal.GetActiveObject() in .NET Core? 0 oleaut32.dll returns negative hresult in debugger, but the correct result in a different app and even in my built app

  3. Chaim

    • 2021/7/19

    No definition found for GetActiveObject from System.Runtime.InteropServices.Marshal C#. I'm trying to connect to a running Excel instance, but when I try to 

  4. Tru

    • 2019/1/15

    GetActiveObject exposes the COM GetActiveObject 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.

  5. Kody

    • 2021/2/14

    No other .NET language has a comparable feature. The other .NET languages must rely on Marshal.GetActiveObject(). Imports System.Runtime.InteropServices.

  6. Damari

    • 2015/8/6

    Imports System.Text Imports System.Runtime.InteropServices Imports System.Security.Permissions Public Structure Point Public x, y As Int32 End Structure Public NotInheritable Class App <SecurityPermission(SecurityAction.LinkDemand, Unrestricted:=True)> _ Shared Sub Main() ' Demonstrate the use of public static fields of the Marshal class.

  7. Dante

    • 2020/8/19

    Structs are a fundamental data type in C# and most other modern programming languages. MissingMethodException: Method not found: 'Void System. XLS, .

  8. Cash

    • 2016/6/25

    Here are the examples of the csharp api class System.Runtime.InteropServices.Marshal.GetActiveObject(string) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

  9. Murphy

    • 2021/6/15

    It fails to go through code line: thisapplication = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application"). Using the exact code, I re-compile the addin,and unregister then register it again, and exhaust any other method I can think of, but the problem consists!

  10. Callan

    • 2018/12/19

    The message I receive is this (edited for confidentiality): "System.Runtime.InteropServices.COMException (0x800401E3): Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))\r at System.Runtime.InteropServices.Marshal.GetActiveObject(Guid& rclsid, IntPtr reserved, Object& ppunk)\r at System.Runtime.InteropServices

  11. Gianni

    • 2015/3/17

    The instance that is first registered in the ROT is typically the instance that is returned by GetActiveObject. If you want to get an Automation Reference to a specific running instance of Word, Excel, or Microsoft Access, use BindToMoniker with the name of the file that is opened in that instance. For a Multiuse (Single Instance) server like

Comments are closed.

Recent Posts