.NET

Reply
*Expert Elite*
1,286 Posts
37 Kudos
Registered: ‎06-21-2004
Post 1 of 6
Accepted Solution

Run function as soon as .dll loads

853 Views, 5 Replies
03-29-2012 06:29 AM

Is there a way to run some code as soon as a .dll file is loaded without having to using lisp (command "Function_Name")?

 

Thank You.

---------------------------



(defun botsbuildbots() (botsbuildbots))

I am a beginning dotnet autocad programmer and use visual basic but I use this to run code when my dll is loaded.  I just grabbed it from the autodesk visual studio wizard.

 

 Implements IExtensionApplication
Public Sub Initialize() Implements IExtensionApplication.Initialize
' Add one time initialization here
' One common scenario is to setup a callback function here that
' unmanaged code can call.
' To do this:
' 1. Export a function from unmanaged code that takes a function
' pointer and stores the passed in value in a global variable.
' 2. Call this exported function in this function passing delegate.
' 3. When unmanaged code needs the services of this managed module
' you simply call acrxLoadApp() and by the time acrxLoadApp
' returns global function pointer is initialized to point to
' the C# delegate.
' For more info see:
' http:'msdn2.microsoft.com/en-US/library/5zwkzwf4(VS.80).aspx
' http:'msdn2.microsoft.com/en-us/library/44ey4b32(VS.80).aspx
' http:'msdn2.microsoft.com/en-US/library/7esfatk4.aspx
' as well as some of the existing AutoCAD managed apps.
' Initialize your plug-in application here
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage("Plugin Loaded successfully...")
End Sub
Public Sub Terminate() Implements IExtensionApplication.Terminate
' Do plug-in application clean up here
End Sub

 

 

*Expert Elite*
1,277 Posts
109 Kudos
Registered: ‎03-13-2008
Post 2 of 6

Re: Run function as soon as .dll loads

03-29-2012 06:40 AM in reply to: arcticad

I am a beginning dotnet autocad programmer and use visual basic but I use this to run code when my dll is loaded.  I just grabbed it from the autodesk visual studio wizard.

 

        Implements IExtensionApplication

        Public Sub Initialize() Implements IExtensionApplication.Initialize
            ' Add one time initialization here
            ' One common scenario is to setup a callback function here that 
            ' unmanaged code can call. 
            ' To do this:
            ' 1. Export a function from unmanaged code that takes a function
            '    pointer and stores the passed in value in a global variable.
            ' 2. Call this exported function in this function passing delegate.
            ' 3. When unmanaged code needs the services of this managed module
            '    you simply call acrxLoadApp() and by the time acrxLoadApp 
            '    returns  global function pointer is initialized to point to
            '    the C# delegate.
            ' For more info see: 
            ' http:'msdn2.microsoft.com/en-US/library/5zwkzwf4(VS.80).aspx
            ' http:'msdn2.microsoft.com/en-us/library/44ey4b32(VS.80).aspx
            ' http:'msdn2.microsoft.com/en-US/library/7esfatk4.aspx
            ' as well as some of the existing AutoCAD managed apps.

            ' Initialize your plug-in application here
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

            ed.WriteMessage("Plugin Loaded successfully...")

        End Sub

        Public Sub Terminate() Implements IExtensionApplication.Terminate
            ' Do plug-in application clean up here
        End Sub

 

 


Keith Brown AutoCAD MEP BLOG | RSS Feed
AutoCAD MEP 2014 | Revit 2014 | EastCoast CAD/CAM V6.1 | Visual Studio 2013
────────────────────────────────────
⁞|⁞ Please use Mark Solutions!.Accept as Solution and Give Kudos!Give Kudos as appropriate. Thank you!
*Expert Elite*
1,286 Posts
37 Kudos
Registered: ‎06-21-2004
Post 3 of 6

Re: Run function as soon as .dll loads

03-29-2012 06:49 AM in reply to: Keith.Brown

Awesome, Works great.

Thank You.

---------------------------



(defun botsbuildbots() (botsbuildbots))
*Expert Elite*
753 Posts
54 Kudos
Registered: ‎02-13-2008
Post 4 of 6

Re: Run function as soon as .dll loads

03-29-2012 12:57 PM in reply to: arcticad

I would have made the same suggestion, but with one caveat.  You need to be careful exactly what you try to do in the initialize routine, as AutoCAD my not be ready for certain things at that time, especially if you are demand loading on AutoCAD startup.

Dave O.                                                                  Sig-Logos32.png
Valued Contributor
70 Posts
1 Kudo
Registered: ‎02-03-2012
Post 5 of 6

Re: Run function as soon as .dll loads

11-04-2012 10:47 AM in reply to: arcticad

Hello,

 

Hope someone can help me.

I want to check the license key (from user settings) in Initialize function, but when I set true/false for the global variable in this method, in commands I'm always getting false value. Why?

 

        private bool isLicensed;

        public bool Licensed
        {
            get { return isLicensed; }
            set { isLicensed = value; }
        }

void IExtensionApplication.Initialize()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("Sprawdzanie licencji ... ");
            if (licenseKey == userKey)
            {
                Licensed = true;
            }
            else
            {
                GPLicense gpl = new GPLicense();
                gpl.Show();
                Licensed = gpl.isValidate;
            }
        }

 private bool CheckLicense()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            if (!Licensed)
            {
                ed.WriteMessage("Brak licencji!");
                GPLicense gpl = new GPLicense();
                gpl.Show();
                Licensed = gpl.isValidate;
                return Licensed;
            }
            else
                return true;
        }

        [CommandMethod("GC")]
        public void CleanLayer()
        {
            if (!CheckLicense())
                return;
            GC.CleanLayer();
        }

 

Valued Mentor
370 Posts
175 Kudos
Registered: ‎05-06-2012
Post 6 of 6

Re: Run function as soon as .dll loads

11-05-2012 07:34 PM in reply to: GrzesiekGP

Your command method and the properties of your class are instance members.

 

AutoCAD will create a seperate instance of your class for each document you use your command in.

 

Unless your data is document-specific, you should not be using non-static command methods or instance properties, you should either store that data in a separate class for which there is only one instance, without any non-static command methods (e.g., a singleton), or you should make everything static (the command methods and the properties they access).

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!