Skaranjit
Skaranjit

Reputation: 794

Exception while loading AutoCAD dll dynamically

I am working on a project where I have to figure out what version of AutoCAD is installed in the target computer. We have considered the possibility of presence of multiple version of AutoCAD and thus, according to user choice, we have to dynamically load required DLLs from that version of AutoCAD. At first, the program discovers the available versions of AutoCAD and shows it to the User. Then, after selection of a specific version, the program copies DLLs like (accoremgd.dll, acdbmgd.dll, acmgd.dll) to the program directory. But when I try to load those dll dynamically, it shows following error:

Could not load file or assembly 'accoremgd.dll' or one of its dependencies. The specified module could not be found.

StackTrace:

at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark) at System.Reflection.Assembly.LoadFrom(String assemblyFile) at DynamicDLLAdd.Form1.btnLoad_Click(Object sender, EventArgs e) in e:\AutoCadOperations\Test.AutoCadLoad_Re\DynamicDLLAdd\Form1.cs:line 140

My sub-routine which dynamically loads the file is:

try
{
    string destFile = @Path.Combine(Environment.CurrentDirectory,"accoremgd.dll"); 
    if (!File.Exists(destFile))
        return;

    Assembly a = null;
    a = Assembly.LoadFrom(destFile);

    AppDomain.CurrentDomain.Load(a.GetName());
    MessageBox.Show("LOADED");
    Type classType = a.GetType("Autodesk.AutoCAD.ApplicationService.Document");
    object obj = Activator.CreateInstance(classType);
    MethodInfo mi = classType.GetMethod("Create");
    //rest of the code here
}
catch (Exception exp)
{
    MessageBox.Show(exp.Message);
    MessageBox.Show(exp.Source);
    MessageBox.Show(exp.StackTrace);
}

I think the problem might with the dll's dependencies. What should be done? Is there any documents or articles available?

Update: The program's Target Framework is 4.0 and Platform target is Any CPU.

I ran fuselogvw.exe and I think here might be some clue to my problem. I have no idea what is going on here so, I have added a picture. It would be a great help if clarifies it. enter image description here

Upvotes: 0

Views: 1933

Answers (2)

Augusto Goncalves
Augusto Goncalves

Reputation: 8604

You should not manually load AcMgd, AcCoreMgd nor AcDbMgd on your DLL library, these references are loaded on AutoCAD process (acad.exe) by default (only from acad.exe folder) and when you NETLOAD your DLL library (i.e. your plugin), it will link automatically. When compiling your DLL library, set Copy Local = FALSE on AutoCAD references. If you leave it TRUE, AutoCAD will behave unexpectedly.

Also, you should not use AutoCAD references on external applications (i.e. a .EXE project on Visual Studio). The references are, in fact, a thin layer to access the real implementation in C++ (compiled as .ARX dynamic libraries) and will only work in process with acad.exe. If you need to call AutoCAD from an external application, you can use COM Automation or AutoCAD Console. See this reply.

That said, starting on AutoCAD 2012 you can use Autoloader mechanism. In summary, you can define a PackageContents.xml file that will define which DLL library to load according to the AutoCAD version that your customer have. That's the easiest way to NETLOAD your plugin into AUtoCAD.

If you still need to register your plugin (DLL library) manually, you can create a registry key for it, but it's a bit tricky due many AutoCAD versions of verticals (e.g. Civil 3D, Plant 3D, Map 3D, etc.). There is certain logic on the registry keys, check more here.

Check more about AutoCAD plugin development at the Developer Center.

Upvotes: 2

user2960398
user2960398

Reputation: 383

I think, its issue is dependencies dll not loaded.

Check following possibility

1) Change Platform target (AnyCPU,86,64)

2) copied dlls (accoremgd.dll, acdbmgd.dll, acmgd.dll) put to following directory

-> C:\Windows\System32
-> C:\Windows\SysWOW64  

3) Change .net framework

Upvotes: 0

Related Questions