How to get running/existing BricscadApp.AcadApplication instance C#

Hi all,
I'm brand new in Bricscad world.
I'd like to implement an external .NET client integration for this CAD.

I've an application console which refers a class library that provides the following class method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


//using AcRx = Teigha.Runtime;
//using AcDb = Teigha.DatabaseServices;
//using AcAp = Bricscad.ApplicationServices;
//using AcEd = Bricscad.EditorInput;
//using AcGe = Teigha.Geometry;
//using AcWn = Bricscad.Windows;
// com
using BricscadDb;
using BricscadApp;
using System.Runtime.InteropServices;
using Bricscad.ApplicationServices;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
using System.Threading;


public class BricscadProvider : IDisposable
  {
    private string ProgID = "BricscadApp.AcadApplication.21.0"; //"BricscadApp.AcadApplication";
    BricscadApp.AcadApplication BrApp = null;
    BricscadApp.AcadDocument BrDoc;

    public BricscadProvider()
    {
      BrApp = GetRunningApp();
      if (BrApp == null)
        BrApp = StartApp();
    }

    internal AcadApplication GetRunningApp()
    {
       try
       {
               var app = (AcadApplication)Marshal.GetActiveObject(ProgID);
               return app;
       }
       catch (System.Runtime.InteropServices.COMException exc)
       {
          int codeError = exc.ErrorCode;
          return null;
       }
    }
... // rest of class code
} // end of class

My problem is that, even though I have a running Bricscad instance (in TaskManager/Details I can clearly see it!!!), GetRunningApp() method throws a COMException with -2147221021 error code value and returns null value.

I'm reading several questions on this forum and the 'CsBrxMgd' project's code [this is the sample project provided by Bricscad within its software installation directory] and code examples use two call options:
1. (AcadApplication)Marshal.GetActiveObject(ProgID);
2. AcAp.Application.AcadApplication as AcadApplication;

Unfortunately, both throw the COMException.

How to retrieve the running instance? Any hint?
T&R,
LemonXXX

Comments

  • Hi all!
    As the issue I pointed out with my previous post had no sense, I tried the same code on another machine... and it worked!
    So I've uninstalled Bricscad on the 1st machine, re-installed it and... now the
    (AcadApplication)Marshal.GetActiveObject(ProgID);
    works like a charm!

  • I always wonder if people really need the app to be external. If you have to have cad running, putting it inside saves all that trouble.
    Still, curious what the app does, always lookin for ideas.

  • LemonXXX
    edited February 2021

    Hi @James Maeding !

    Question #2
    My app has to programmatically read and/or write metadata from and/or into some Bricscad files, save them, save them as, export into PDF version etc.

    Point #1
    I believe that your statement depends on your final goal... in some situations it's true, but it couldn't be the answer in other contexts.
    Having an external tool (that is not an integrated one inside your CAD) allows you to have a tool that a) potentially could work with several Authoring Tools; b) if crashes, doesn't risk to ruin/affect the Authoring Tool it is attached at.