.NET

Reply
Contributor
bababarghi
Posts: 19
Registered: ‎05-01-2013
Message 1 of 11 (273 Views)

Assembly Load problem

273 Views, 10 Replies
05-12-2013 09:54 PM

Hi all,

 

I am writing an EXE .net application and I need to, somehow, use NETLOAD and load a dll file. As far as I understood from here, following line would be the key for me.

 

Assembly.LoadFrom( <...full path to my DLL file...> )

I run the code and I get no error - AutoCAD has been set to be Visible since I am still debuging my code.

Meanwhile the subroutine's meta data is not recognised at all - as if the DLL file has not been loaded in AutoCAD.

 

Any idea what that might be for?

 

 

Thanks

Mentor
mzakiralam
Posts: 225
Registered: ‎11-09-2012
Message 2 of 11 (249 Views)

Re: Assembly Load problem

05-13-2013 03:51 AM in reply to: bababarghi
Hi,
Did you try to load your DLL manually via NETLOAD command? If not I would recommend you to do so. If manually DLL loading is working then you can try for automatic loading. and to understand what have you done put your code also? otherwise people cannot help you.
*Expert Elite*
norman.yuan
Posts: 946
Registered: ‎04-27-2009
Message 3 of 11 (245 Views)

Re: Assembly Load problem

05-13-2013 06:32 AM in reply to: bababarghi

Yes, code Assembly.LoadFrom/LoadFile() is the same as NETLOAD command in AutoCAD, if the code code is called inside AutoCAD process.

 

However, since you are doing EXE, which is a separate process from AutoCAD, even you are automating AutoCAD (via COM API), if you call Assemly.Loadxxx() in your EXE project, the DLL is loaded into your EXE process, not into AutoCAD process. Since the code in the DLL is for AutoCAD and likely has references to acdbmgd/acmgd/accore.dll, which cannot be used outside AutoCAD process, that is why you get error.

 

If you automate AutoCAD froom EXE via COM, you'd better to make sure the .NET DLL is somehow automatically loaded into AutoCAD on its startup. Or, you want to make things more complicated (and often unnecessarily) by expose your functionality in the .NET DLL to COM, so that you can access it from outside AutoCAD via COM API.

Contributor
bababarghi
Posts: 19
Registered: ‎05-01-2013
Message 4 of 11 (236 Views)

Re: Assembly Load problem

05-13-2013 02:22 PM in reply to: norman.yuan

Thanks for the clear response. 

 

Now, I have another question: Is there any possibility to set a AutoCAD variable via COM API? If the answer is Yes, then I can set FILEDIA value to 0 and then I could pass my dll path to AutoCAD using following:

 

app.ActiveDocument.SendCommand("NETLOAD" & vbCr)
app.ActiveDocument.SendCommand("...<DLL path>..." & vbCr)

 

 

Can't I?

*Expert Elite*
norman.yuan
Posts: 946
Registered: ‎04-27-2009
Message 5 of 11 (229 Views)

Re: Assembly Load problem

05-13-2013 02:38 PM in reply to: bababarghi

Yes, you can set AutoCAD's system variable via AcadDocument:

 

acadApp.AcitveDocument.SetVariable "FILEDIA", 0

 

But, if you use SemdCommand to execute "NETLOAD", and you obviously want to execute some commands defined in your .NET DLL after the DLL is loaded.

 

However, your EXE app's execution of SendCommand may not wait the NETLOAD being completed (especially, if your DLL implementing IExtensionApplication, which do qite somework) and just jump to next line of code (after SemdCommand("NETLOAD"....). That is, you'd better only call SendCommand() from your external app only once as LAST action it ask AutoCAD to do.

 

So, if you have to automate AutoCAD this way, you'd better, (as I suggested previously) consider some autoloading .NET DLL approach, or loading them on demaond...

Contributor
bababarghi
Posts: 19
Registered: ‎05-01-2013
Message 6 of 11 (227 Views)

Re: Assembly Load problem

05-13-2013 02:47 PM in reply to: norman.yuan

Actually I was editing my previous post while you sent your response.

 

The main problem with the command you mentioned i.e.

Application.SetSystemVariable("FILEDIA", 0)

is that I have to add following Refs to my project tobe able to benefit this code.

  • acmgd.dll
  • acdbmgd.dll
  • accoremgd.dll

Which is basically impossible to import them in an EXE application (as we all know).

 

Contributor
bababarghi
Posts: 19
Registered: ‎05-01-2013
Message 7 of 11 (225 Views)

Re: Assembly Load problem

05-13-2013 02:51 PM in reply to: bababarghi

I appologise. I did not read your response thoroughly enough. 

 

I though you meant 

Application.SetSystemVariable

 

 

 

My bad :smileyfrustrated:

Contributor
bababarghi
Posts: 19
Registered: ‎05-01-2013
Message 8 of 11 (221 Views)

Re: Assembly Load problem

05-13-2013 03:38 PM in reply to: bababarghi

Although even with acadApp.AcitveDocument.SetVariable , the following exception is thrown:

 

Public memeber 'ActiveDocument' on type 'IAcadApplication' not found :smileymad:

*Expert Elite*
norman.yuan
Posts: 946
Registered: ‎04-27-2009
Message 9 of 11 (200 Views)

Re: Assembly Load problem

05-14-2013 06:30 AM in reply to: bababarghi

Depending on how AutoCAD is started, there may or may not be an ActiveDocument. It looks like you launch AutoCAD from your EXE app. Iin this case, unless you expliciltly open a new or existing drawing, the AutoCAd session will not have an ActiveDocument.

 

So, before you can use AcadDocument.SetVariable() method, or any methods of the AcadDocument class for that matter, you need to obtain an instance of AcadDocument, be it ActiveDocument or not. Without seeing more of your code as what it exactly does, it is difficult to say more.

 

Contributor
bababarghi
Posts: 19
Registered: ‎05-01-2013
Message 10 of 11 (184 Views)

Re: Assembly Load problem

05-14-2013 02:09 PM in reply to: norman.yuan

Thank you again Norman but I am afraid that's not the case. I put my code here to have a look:

 

 

 

 

Public Function InAutoCAD(FilePath As String) As Boolean
        Dim GivenTaskStatus As Boolean = False
        Const progID As String = "AutoCAD.Application.19"
        'Const DllPath As String = "C:\DLL file.dll"
        Dim acApp As AcadApplication = Nothing
        Try
            acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
        Catch
            Try
                Dim acType As Type = Type.GetTypeFromProgID(progID)
                acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)
            Catch
                GivenTaskStatus = False
                MessageBox.Show("Cannot create object of type """ + progID + """")
            End Try
        End Try
        If acApp IsNot Nothing Then
            Try
                acApp.Visible = True
                acApp.WindowState = AcWindowState.acMax
                Dim doc As AcadDocument = acApp.Documents.Open(FilePath)
                doc.Activate()

                ''╔════════ Loading DLL file ═══════╗
                'acApp.AcitveDocument.SetVariable("FILEDIA", 0)
                'Try
                '    acApp.ActiveDocument.SendCommand("NETLOAD" & vbCr & DllPath & vbCr)
                'Catch ex As System.Exception
                '    MsgBox(vbLf & "Cannot load {0}: {1}", DllPath, ex.Message)
                'End Try
                ''╚════════ Loading DLL file ═══════╝

                'acApp.ActiveDocument.SendCommand("MYCOMMAND" + vbCr)

                'saving document
                doc.Close(True)

                GivenTaskStatus = True
            Catch ex As Exception
                GivenTaskStatus = False
                MessageBox.Show(Me, "Problem executing component: " + ex.Message)
            End Try
        End If
        Return GivenTaskStatus
    End Function

 

 

Having said that, I am not happy with AutoCAD launching part of the code as well. It looks bogus to me. It gives me following error at my very first run of the code (cold start).

 

6a00d83452464869e20120a8d6eb55970b.png

 

But after re-running the code couple of times, that error will not pup up again and AutoCAD is simply launched. Any idea what that might be for?

I found this by Google but unfortunately that article is too technical for me to comprehend :smileysad:

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community