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.