Activator.CreateInstance() problem??

Activator.CreateInstance() problem??

integtech
Enthusiast Enthusiast
893 Views
7 Replies
Message 1 of 8

Activator.CreateInstance() problem??

integtech
Enthusiast
Enthusiast

Hi Dears.

 

I used the Activator.CreateInstance() to run autocad in my program.

It worked well in cad 2015, 2018, 2021.

 

However, a problem occurred after installing civil3d 2023.

try
{
    dynamic acAppComObj = Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application.24"));
}
catch (System.Exception ex)
{
    System.Windows.Forms.MessageBox.Show($"Instance of 'AutoCAD.Application' could not be created.\r\nProgId : {sProgId} \r\n{ex.Message}");
    return -1;
}

This code always run civil 3d.

I think the procid of 2023 is "AutoCAD.Application.24.2".

 

The results after running CAD 2021 are the same, civil3d 2023 launched.

 

How can I launch the Autocad 2021?

 

0 Likes
894 Views
7 Replies
Replies (7)
Message 2 of 8

DeltaSnollyGoster
Contributor
Contributor

Hello, when you create instance, he run last launched (acad or civil3d) app.
You may change this by editing registry key.

Set value to "ACAD"

HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R24.0\LastLaunchedProduct

Or simply run acad.exe with attribute "/product ACAD" 

0 Likes
Message 3 of 8

integtech
Enthusiast
Enthusiast

Thanks for your reply.

 

My source launched the cad 2021, but as a result, cad 2023 was launched.

After repeating the test, I confirmed cad 2021 sometimes runs.

(I have no idea, Why?)

 

Cad 2021 and 2023 are installed.

Then I want to launch the cad 2021 or 2023 that the user selected.

After launching cad, I want to load my application.(.net dll)

How can I do this?

 

If I use Process.Start() with acad.exe, can I load my application as cad running?

How can I get acad.exe location? 

Is it fixed at C:\Program Files\Autodesk\AutoCAD 20xx\acad.exe?

 

0 Likes
Message 4 of 8

DeltaSnollyGoster
Contributor
Contributor

I get list installed acad apps from registry and pick which one is needed.

 

String[] appIds = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Classes", false).GetSubKeyNames().Where(n =>
n.StartsWith("AutoCAD.Application")).ToArray();

String strId = appIds[0];
System.Type type = System.Type.GetTypeFromProgID(strId);
AcadApplication acad = System.Activator.CreateInstance(type) as AcadApplication;

 

Load .net dll in acad, you can use:

 

adoc.SendCommand(" (COMMAND \"_NETLOAD\" \"PATH TO DLL\") ");

 

You can load your dll in acad on start up through:

  • Script file .scr (Add "/b "PATH TO SCR")
  • Entry in the registry
  • From file PackageContents.xml

That's what I remembered.
I also recommend paying attention to AcCoreConsole.
For application folder of acad look at "HKLM\SOFTWARE\Autodesk\AutoCAD\RXX.X\ACAD-E001:409" key: "AcadLocation" in registry.

 

 

 

0 Likes
Message 5 of 8

integtech
Enthusiast
Enthusiast

Your suggestion is similar to my existing code.

This is my code.

Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application.24"));

This is launched cad 2023, sometimes 2021.

 

Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application.24.2"));

This is launched cad 2023 only.

 

I'm curious as to why, but first I want a solution.

I want a sample code that can always run 2021 in an environment that both 2021 and 2023 are installed.

 

Thank you.

0 Likes
Message 6 of 8

DeltaSnollyGoster
Contributor
Contributor

In windows reg find "\HKEY_CLASSES_ROOT\AutoCAD.Application.24\CLSID" key, copy value end try find

on path:"\HKEY_CLASSES_ROOT\CLSID\"Your copied clsid value"\LocalServer32"
There must be path to your Autocad 2021.

0 Likes
Message 7 of 8

integtech
Enthusiast
Enthusiast

I searched my registry.

The result is strange for me. 

 

2021_1.png2021_2.png2023_1.png2023_2.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

AutoCAD.Application.24 and AutoCAD.Application.24.2 CLSID are different but LocalServer32 are same.

C:\Program Files\Autodesk\AutoCAD 2023\acad.exe /Automation

Why is there 2023 in the AutoCAD.Application.24 path?

I test on some PCs in the office, and they all came out with the same results.

 

Is this an AutoCAD bug?

I think there is some problem with running Autocad using information in the registry.

0 Likes
Message 8 of 8

DeltaSnollyGoster
Contributor
Contributor

Maybe someone who knows will explain, but they should be different.

There may be an incorrect installation here.
Unfortunately, I don't have acad 2021 and 2023 installed, so I can't check the correctness.
I have acad 2022 but in the registry it is signed as AutoCAD.Application.24 and should 24.1.

I don't know if it will help to change the path to the correct one, since everything is tied up there and there are many places where you need to change.
We need someone from the developers or experts to figure out how to fix this and what it's related to

0 Likes