Search code examples
c#.net.net-coremarshalling

Question about the implementation of "Marshal.GetActiveObject(System.String)" method


I'm trying to connect to a running Solid Edge instance, but I am getting the following error:

System.MissingMethodException: 'Method not found: 'System.Object System.Runtime.InteropServices.Marshal.GetActiveObject(System.String)'

I've been searching about this error and I found that:

I want to make another solution, more accurate for my project. The idea is to make a DLL with .NET Framework 4.8.1 and then use it in another projects that are programmed with .NET Core 7.

I wrote this code snippet for the DLL:

using System;
using System.Runtime.InteropServices;
 
namespace SolidEgde.OpenSDK
{
    public static class Communication
    {
        /// <summary>
        /// Connects to or starts a new instance of Solid Edge.
        /// </summary>
        /// <param name="startIfNotRunning"></param>
        /// <returns>
        /// An object of type SolidEdgeFramework.Application.
        /// </returns>
        public static SolidEdgeFramework.Application Connect(bool startIfNotRunning)
        {
            SolidEdgeFramework.Application application = null;
 
            try
            {
                // Attempt to connect to a running instance of Solid Edge.
                application = (SolidEdgeFramework.Application)Marshal.GetActiveObject(PROGID.SolidEdge_Application);
            }
            catch { throw; }
 
            if (application != null)
                application.Visible = true;
 
            return application;
        }
    }
    public static class PROGID
    {
        public const string SolidEdge_Application = "SolidEdge.Application"; // HKEY_CLASSES_ROOT\SolidEdge.Application
    }
}

And then I call It from a test project, like this:

namespace Testeo
{
    class Program
    {
        static void Main(string[] args)
        {
            SolidEdgeFramework.Application application = null;
 
            application = SolidEgde.OpenSDK.Communication.Connect(true);
        }
    }
}

But I get the error mentioned at the beginning.

PS: in my project I am trying to connect to an instance of the Solid Edge CAD program.


Solution

  • Based on the comment of @Hans Passant, I search more info about legacy dll .NET Framework components and .NET Core integration and the answer is:

    No, It is not possible to have both on a project in a safe way.

    Why?

    • They target a different set of assemblies (mscorlib vs. System.Runtime) which causes incompatibilities since all usages of types are prefixed with the assembly the type is from.
    • .NET Core can reference .NET Framework assemblies. This allows to reference the assembly and compile successfully. BUT this doesn't guarantee a successful execution on runtime, since .NET Core doesn't provide all the APIs from .NET Framework.

    References to this problem: