.NET

Reply
*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 1 of 6 (816 Views)
Accepted Solution

Run function as soon as .dll loads

816 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*
Keith.Brown
Posts: 1,195
Registered: ‎03-13-2008
Message 2 of 6 (814 Views)

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*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 3 of 6 (807 Views)

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*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 4 of 6 (784 Views)

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
GrzesiekGP
Posts: 67
Registered: ‎02-03-2012
Message 5 of 6 (663 Views)

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
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 6 of 6 (631 Views)

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 Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!