*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 11 of 14 (324 Views)

Re: Connecting to AutoCAD

03-03-2012 02:44 PM in reply to: cvaught

Both ways is woking for me with A2009 Win7

Try again


   ''1st way   
  Private Function GetAcadVer() As String
        Dim acadver As Object
            acadver = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\AutoCAD.Application\CurVer", _
                                                  String.Empty, String.Empty)

            If TypeOf acadver Is String Then
                'MessageBox.Show("String acadver: " & acadver.ToString())

                Return acadver.ToString()
                Return Nothing
            End If

            Return Nothing
        End Try
    End Function
    Public Sub connectAutoCad()

        Dim acApp As Object = Nothing
        Dim progID As String = GetAcadVer()

            'try to get a current instance of AutoCAD
            acApp = Marshal.GetActiveObject(progID)

            'try to create a new instance of AutoCAD
            Dim acType As Type = Type.GetTypeFromProgID(progID)
            acApp = Activator.CreateInstance(acType, True)
        End Try
        acApp.GetType().InvokeMember("visible", Reflection.BindingFlags.SetProperty + Reflection.BindingFlags.IgnoreCase, Nothing, acApp, New Object() {True})
        acApp.GetType().InvokeMember("eval", Reflection.BindingFlags.InvokeMethod + Reflection.BindingFlags.IgnoreCase, Nothing, acApp, New Object() {"MsgBox(" + """Hello Discussion group""" + ")"})
        acApp.GetType().InvokeMember("quit", Reflection.BindingFlags.InvokeMethod + Reflection.BindingFlags.IgnoreCase, Nothing, acApp, Nothing)

        acApp = Nothing
    End Sub

''2nd way
   Private Sub GetAcadApp(ByVal AcadString As String)
        Dim AcadApp As Object = Nothing
            AcadApp = GetObject(, "AutoCAD.Application." & AcadString)


            AcadApp = CreateObject("AutoCAD.Application." & AcadString)

            AcadApp.GetType().InvokeMember("Visible", Reflection.BindingFlags.SetProperty + Reflection.BindingFlags.IgnoreCase, Nothing, AcadApp, New Object() {True})
            MessageBox.Show(AcadApp.GetType().InvokeMember("Name", Reflection.BindingFlags.GetProperty + Reflection.BindingFlags.IgnoreCase, Nothing, AcadApp, Nothing).ToString)

            AcadApp.GetType().InvokeMember("Quit", Reflection.BindingFlags.InvokeMethod + Reflection.BindingFlags.IgnoreCase, Nothing, AcadApp, Nothing)
            AcadApp = Nothing
        End Try

    End Sub
''on button click call
'' GetAcadApp("17")





Posts: 18
Registered: ‎01-25-2012
Message 12 of 14 (318 Views)

Re: Connecting to AutoCAD

03-03-2012 07:56 PM in reply to: Hallex

Still doesn't work.  Just to confirm, did the code you use work with just one version of AutoCAD or have you been able to use it to connect to different versions on different machines using the same executable?  

*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 13 of 14 (312 Views)

Re: Connecting to AutoCAD

03-04-2012 03:45 AM in reply to: cvaught

I'm working nowhere, sorry

just tested this code on my home machine - A2009(eng) release


Active Member
Posts: 8
Registered: ‎05-10-2010
Message 14 of 14 (208 Views)

Re: Connecting to AutoCAD

10-07-2012 10:24 PM in reply to: cvaught

cvaught wrote:

Same problem still exists.  It starts acad.exe but cannot connect to it.  Any other opitons?  This seems like it should be pretty straight forward.  How else can a stand along application support multiple versions of AutoCAD?


There is no way that the same exe can support multiple versions of AutoCAD.

Different version or OS (X64 ,X86) of AutoCAD has different library.

The best way is to recomplie another project (the same code but has different reference librayies).

Huilung Chen


Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.