Connection between .NET and AUTOCAD ELECTRICAL and Load dll in .NET

Connection between .NET and AUTOCAD ELECTRICAL and Load dll in .NET

soubhagya_prusty
Explorer Explorer
352 Views
3 Replies
Message 1 of 4

Connection between .NET and AUTOCAD ELECTRICAL and Load dll in .NET

soubhagya_prusty
Explorer
Explorer

I want to connect .NET with Autocad Electrical . i am using ,

AutoCAD Electrical 2025.0.2 , Product Version : 22.0.81.0 , Built on: V.154.0.0 AutoCAD 2025.1.1

Visual Studio Community 2022 - 17.12.3

my system is x64 bit

 

I am creating a console application in .NET 8.0 Runtime with C# language , Added dll accoremgd , acdbmgd, acmgd, Autodesk.AutoCAD.Interop . Also in .NET in Solution Explorer , in project references , i have made "Copy Local" property as False.

using System;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;

namespace AutoCADElecDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            AcadApplication acadApp = null;
            const string progId = "AutoCAD.Application.25"; // Adjust for your AutoCAD version

            try
            {
                // Get a running instance of AutoCAD
                acadApp = GetActiveAutoCAD(progId);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine($"Error initializing AutoCAD: {ex.Message}");
                return;
            }

            try
            {
                // Ensure AutoCAD is visible
                acadApp.Visible = true;
                Console.WriteLine("AutoCAD is now running.");

                // Register for the BeginQuit event
                Application.BeginQuit += OnBeginQuit;

                // Keep the application running to monitor AutoCAD's state
                Console.WriteLine("Press Enter to exit...");
                Console.ReadLine();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
            finally
            {
                // Unregister the event handler before exiting
                Application.BeginQuit -= OnBeginQuit;
            }
        }

        static AcadApplication GetActiveAutoCAD(string progId)
        {
            try
            {
                var comObject = Marshal.GetActiveObject(progId);
                Console.WriteLine($"Connected to AutoCAD: {progId}");
                return (AcadApplication)comObject;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine($"Error accessing AutoCAD: {ex.Message}");
                throw;
            }
        }

        static void OnBeginQuit(object sender, EventArgs e)
        {
            Console.WriteLine("AutoCAD is being closed.");
        }
    }
}

 

For above code, my application comes in break mode "Your app has entered a break state, but no code is currently executing that is supported by the selected debug engine (e.g. only native runtime code is executing)."

and i am getting below error : 
System.IO.FileNotFoundException: 'Could not load file or assembly 'accoremgd, Version=25.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.'

dll path is also correct.

i also tried same code with .NET Framework 4.7.2 and 4.8 targeting pack , but getting same error.

How to load accoremgd.dll properly so that this application can use autocad accoremgd functions properly.

 

0 Likes
Accepted solutions (1)
353 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

You cannot use AutoCAD managed assemlies (accoremgd/acdbmgd/acmgd.dll) in external EXE app. They can only used inside AutoCAD session (i.e. AutoCAD .NET plugin/class library project).

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 4

soubhagya_prusty
Explorer
Explorer

AutoCAD 2025 Developer and ObjectARX Help | Components of the AutoCAD .NET API (.NET) | Autodesk

I had used steps as mentioned in above documentation . Does above documentation will help to use dll files in external .net exe program . 

0 Likes
Message 4 of 4

norman.yuan
Mentor
Mentor
Accepted solution

If you must do an EXE app, you need to use AutoCAD COM API (i.e. you add references to Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll and the "Copy Local" for these 2 interop dlls should be True. You CANNOT reference AutoCAD .NET API assemblies in the EXE project.

 

You may want to think it harder of why you need to run an EXE to control another heavy desktop app (AutoCAD) when almost anything you want to do with the EXE can be done directly inside AutoCAD as plugin?

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes