dll loaded success, but Initialize doesn't work

dll loaded success, but Initialize doesn't work

adn019LKFW
Contributor Contributor
1,575 Views
9 Replies
Message 1 of 10

dll loaded success, but Initialize doesn't work

adn019LKFW
Contributor
Contributor

I use the netload command to load my dll, but the code in MyApp.Initialize() doesn't execute and my command doesn't work. This problem does not appear on the cad2012 version, but sometimes it occurs in the cad2016,2018,2020 version.

If I build new project, this problem does not appear.

So what's wrong with my project?

 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

[assembly: ExtensionApplication(typeof(MyNamespace.MyApp))]
namespace MyNamespace{
public class MyApp : IExtensionApplication
{
    public void Initialize()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        ed.WriteMessage("MyDll load success");
    }

    //...other code
}
}

 

 

0 Likes
1,576 Views
9 Replies
Replies (9)
Message 2 of 10

_gile
Consultant
Consultant

Hi,

A project referencing AutoCAD 2012 libraries (AcDbMgd.ll and AcMgd.dll) won't work on later versions. You have to build a new project referencing AcCoreMgd.dill, AcDbMgd.ll and AcMgd.dll from AutoCAD 2013 or later libraries.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 10

adn019LKFW
Contributor
Contributor

Thank you for your reply,

I'm using different project for each cad version, and both correctly reference the corresponding version of the dlls.

So it doesn't look like the problem is caused by this reason.

0 Likes
Message 4 of 10

Alexander.Rivilis
Mentor
Mentor

You have to compare Application.DocumentManager.MdiActiveDocument and null before call doc.Editor.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 5 of 10

adn019LKFW
Contributor
Contributor

Thank you for your reply,

I'm sure Application.DocumentManager.MdiActiveDocument is not null.

I try to attach to acad.exe in debug mode,  Initialize() is not invoked.

 

0 Likes
Message 6 of 10

_gile
Consultant
Consultant

In addition to @Alexander.Rivilis said, you should also handle the Apllication.Idle event.

        public void Initialize()
        {
            Application.Idle += OnIdle;
        }

        private void OnIdle(object sender, EventArgs e)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            if (doc != null)
            {
                Application.Idle -= OnIdle;
                doc.Editor.WriteMessage("\nMyDll load success.\n");
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 10

adn019LKFW
Contributor
Contributor

I rebuild the project a few times without any modification and it works. It's so weird.

0 Likes
Message 8 of 10

CadWCCNL
Enthusiast
Enthusiast

@Alexander.Rivilis wrote:

You have to compare Application.DocumentManager.MdiActiveDocument and null before call doc.Editor.


I remember seeing something like this in Helper.cs.

..\Autodesk\ObjectARX_for_AutoCAD_2023_Win_64bit_dlm\samples\dotNet\EventsWatcher
try
{
	// Application.DocumentManager.Count should be used to check zero doc status
	// MdiActiveDocument returns non null sometimes even if in zero doc status!
	if(	Application.DocumentManager.MdiActiveDocument != null
		&& Application.DocumentManager.Count != 0)
		Helper.CmdLineMessage(str);
	else
		Helper.InfoMessageBox(str);

	Helper.StreamToRichTextControl(str);
}
catch (System.Exception ex)
{
	Helper.Message(ex);
}
    // Application.DocumentManager.Count should be used to check zero doc status
    // MdiActiveDocument returns non null sometimes even if in zero doc status!
 
Possible why it was working and then not working no? Should possible consider adding to avoid the error in the future.
0 Likes
Message 9 of 10

adn019LKFW
Contributor
Contributor

My problem is that Initialize() is not invoked after netload, and commands in this project are not registered, Editor.WriteMessage() is just an example added here.

0 Likes
Message 10 of 10

varshaauti27
Advocate
Advocate

@adn019LKFW 

 

Remove 

[assembly: ExtensionApplication(typeof(MyNamespace.MyApp))]

It should work as expected!

0 Likes