October 2014

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  










« No muttering at the back! Reducing the background noise when sending commands to AutoCAD | Main | Announcing two new Autodesk developer blogs »

September 08, 2008

Preventing a .NET module from being loaded by AutoCAD

This is an interesting one that came up recently during an internal discussion:

During my module's Initialize() function, I want to decide that the module should not actually be loaded. How can I accomplish that?

The answer is surprisingly simple: if you throw an exception during the function, AutoCAD's NETLOAD mechanism will stop loading the application.

For an example, see this C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;


namespace PreventLoad

{

  public class Commands

  : IExtensionApplication

  {

    public void Initialize()

    {

      // This will prevent the application from loading


      throw new Exception();

    }


    public void Terminate(){}


    [CommandMethod("TEST")]

    static public void TestCommand()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;


      doc.Editor.WriteMessage(

        "\nThis should not get called."

      );

    }

  }

}

Here's what happens when we attempt to load the application and then run the TEST command:

Command: FILEDIA

Enter new value for FILEDIA <1>: 0

Command: NETLOAD

Assembly file name: c:\MyApplication.dll

Command: TEST

Unknown command "TEST".  Press F1 for help.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200e554f9e59c8834

Listed below are links to weblogs that reference Preventing a .NET module from being loaded by AutoCAD:

blog comments powered by Disqus

Feed/Share

10 Random Posts