I have a standalone VB.NET program that we use to mangage our AutoCAD Electrical databases and insert components (blocks) into AutoCAD. It has worked fine until we started deploying 64 bit computers. Now, it won't connect to AutoCad (again the 64 bit vesions don't work, 32 bit works fine).
Below are snippets from my VB.NET app. Do I need to connect differently for 64 bit machines? (I aplogize for not displaying the exception message when I try to connect to AutoCAD. The 64 bit computer that is having this problem is in a remote office and the engineer is not in today to let me log in.)
--------------------------
Imports System
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports System.Runtime.InteropServices.Marshal
Imports Autodesk.AutoCAD.Interop.AcadDocumentClass
Public acadapp As Autodesk.AutoCAD.Interop.AcadApplication
Public acadDoc As Autodesk.AutoCAD.Interop.AcadDocument
acadapp = System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application.18.2")
The COM API is platform (and version) dependent.
You have to build a 64 bits version referencing the 64 bits interop librairies.
One way to use the same code in the two projects:
- start a new project in the same solution ;
- reference the 64 bits Interop libraries ;
- right click the new project and choose "Add" > "Existing elements..." ;
- in the dialog, select the 32 bits project vb files, expand the "Add" button menu and choose "Add as link".
You code itself looks OK, as long as there is an running AutoCAD session (of course you need a Try...Catch...block wrapping it in case there isn't a running AutoCAD session, right?).
However, looking at the picture you attached, isn't the issue quite obvious?
Firstly, your code is trying to get Acad 18.2, which is ACAD2012, while the referenced interop assembly is froom Acad2011. (this may not be the reason your code not working, though, from my test, but worth pointing out)
Secondly, and the critically, it seems the referenced interop assembly is for 32-bit AutoCAD 2011, which can only be used to automate 32-bit AutoCAD. This would be exactly the reason why your app does not work with 64-bit AutoCAD.
Norman,
Thanks for the help and the problem was obvious when you pointed it out.
Yes, interop was from the wrong version of AutoCAD and not for 64 bit. So, now that I have that fixed, I am a little further along but not out of the woods. And yes, my orignal code has wrappers but I just placed snippets to keep things simple for troubleshooting here.
The problem I have now is at "acadDoc = acadapp.ActiveDocument" (See attached file). It appears that my application now does recognizes that AutoCAD 2012 is open, but I cannot get to the active document. Should I try to access the active document differently than my example?
Keith
Imports System
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports System.Runtime.InteropServices.Marshal
Imports Autodesk.AutoCAD.Interop.AcadDocumentClass
Public Class Form1
Public acadapp As Autodesk.AutoCAD.Interop.AcadApplication
Public acadDoc As Autodesk.AutoCAD.Interop.AcadDocument
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
acadapp = System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application.18.2")
AppActivate(acadapp.Caption)
acadDoc = acadapp.ActiveDocument
End Sub
End Class
I have not problem running following code with my Acad2012 x64, if an Acad2012 session is running and at least a drawing exists (even it is an new, not saved "Drawing1.dwg"). It is an form with one button on it:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { AcadApplication app = System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application.18.2") as AcadApplication; if (app != null) { MessageBox.Show("Succeeded"); AcadDocument doc = app.ActiveDocument; MessageBox.Show(doc.Name); } else { MessageBox.Show("???"); } } catch { MessageBox.Show("Failed"); } } }
Running the code will pops up 2 messagebox "Succeeded" and "Drawing1.dwg".
BTW, the WinForm project targets .NET4.0 client profile