Cannot Launch Autocad from external vb.net application.

Cannot Launch Autocad from external vb.net application.

sat
Participant Participant
3,773 Views
4 Replies
Message 1 of 5

Cannot Launch Autocad from external vb.net application.

sat
Participant
Participant

I have an vb.net application that take a list of drawings and  open every one in Autocad, do some changes and print it. The application is an EXE file and works fine if Autocad is already running in the computer but fail if Autocad is not running yet. I'm working with visual studio 2015 and autocad 2018. This is the code that opens Autocad:

 

Imports System
Imports System.Runtime.InteropServices
Imports AutoCAD

Public Class cAcad

Public Sub RunAcad()
Dim ProgId As String = "AutoCAD.Application.22"
Dim AcApp As AcadApplication = Nothing

Try
   AcApp = DirectCast(Marshal.GetActiveObject(ProgId), AcadApplication)

Catch ex As Exception
  Try
    Dim AcType As Type = Type.GetTypeFromProgID(ProgId)
    AcApp = DirectCast(Activator.CreateInstance(AcType, True), AcadApplication)
  Catch ex2 As Exception
    MessageBox.Show("Cannot create object of type " & ProgId & vbNewLine & ex2.Message)
  End Try
End Try
If AcApp IsNot Nothing Then
  Try
    AcApp.Documents.Add()

  Catch ex As Exception
    MessageBox.Show("Cannot add a new document " & ProgId & vbNewLine & ex.Message)

  End Try
End If
End Sub

End Class

 

The error doesn't come in "createInstance", but it comes in "documents.add" sentence because  Acad.exe is not running. The error is :

El servidor lanzó una excepción. (Excepción de HRESULT: 0x80010105 (RPC_E_SERVERFAULT)).

 

I have tried many different ways to create the new object but always found the same problem.

Note: When createObject is running I can see Acad.exe in taskManager but when the sentence finish, acad.exe disapear.

 

0 Likes
Accepted solutions (1)
3,774 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

Hi,

 

When starting a new AutoCAD process, you should wait for AutoCAD finish intialization before calling method on the AutoCAD Application instance.

 

        Try
            While Not AcApp.GetAcadState.IsQuiescent
                Thread.Sleep(100)
            End While
        Catch ex As Exception
        End Try


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

sat
Participant
Participant
Accepted solution

Thanks Guilles,

but I have the same problem because I can't access any property of the acApp object. When I try to access acAPP.getAcadState the error comes again. it seems that the acapp object is created but is damaged.

 

Finally I have found a solution using Process.Start. This runs perfectly. Here is the code in case anybody wants to use it:

 

Public Function OpenAcad() As AcadApplication
Dim exePath As String = "C:\Program Files\Autodesk\AutoCAD 2018\acad.exe"
Dim AcApp As AcadApplication = Nothing
Dim ProgId As String = "AutoCAD.Application.22"

Try
  Dim p As Process
  p = Process.Start(exePath)

  While AcApp Is Nothing
    Try
      AcApp = DirectCast(Marshal.GetActiveObject(ProgId), AcadApplication)
    Catch ex As Exception
      AcApp = Nothing
    End Try
  End While
  AcApp.Visible = True
  Catch ex As Exception
    MessageBox.Show("Cannot get acad " & vbNewLine & ex.Message)
  End Try
  Return AcApp
End Function

 

 

0 Likes
Message 4 of 5

ActivistInvestor
Mentor
Mentor

You marked your post as a solution, but your problem doesn't seem to be solved.

 

You are starting AutoCAD via System.Process, and then trying to get the running instance. If there is already a running instance, then you may get that one, rather than the instance you started.

 

If you want to start a new instance and work with it, you should use Activator.CreateInstance(), as shown in the following C# example. It's also wise to use a timeout in case AutoCAD does something unexpected at startup, that blocks access via ActiveX, like shown in the example.

 

   public static dynamic StartAcad()
   {
      dynamic acadObject = Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application"));
      if(acadObject != null)
      {
         DateTime start = DateTime.Now;
         while(true)
         {
            try
            {
               acadObject.Visible = true;
               return acadObject;
            }
            catch
            {
               // If AutoCAD is not ready within 10 seconds, exit:
               if((DateTime.Now - start).Seconds > 10)
                  break;
               System.Threading.Thread.Sleep(100);
            }
         }
      }
      return null;
   }
0 Likes
Message 5 of 5

sat
Participant
Participant

Thanks for your post but,

I use process.start because all my tryals with activator.createInstance() fail.

Also, I call the start.process only if  I doesn't found a running instance of acad.exe. 

 

I have tried your example and it also fails in my computer. I always reach the timeout, even if I increasse it.

 

The thing is that, I don't know why, the creatInstance try to open acad.exe but it fails without giving any exeption, but the object its not null. While the sentece creatInstance  is running, I can see ACAD.exe in task manager window, but when I reach next sentence, acad.exe disappear in task manager, then acadobject.visible=true always give me an exception:

El servidor RPC no esta disponible (exception HRESULT:0x800706BA)

 

I take your idea of a timeout for my loop

 

0 Likes