error ... unable to cast COM ... to interface type Autodesk.AutoCAD.interop.AcadApplication ...
In trying to go further ... error occurs in the cast line of the list of COM objects AcadApplication acApp = (AcadApplication)cs1 ... why? The properties of cs1 show that it is AutoCAD - what have I missed? (AutoCAD 2018 , AutoCAD.Application.22 ...
openDWGset = new List<string>();
List<object> CADsessionSet = new List<object>();
if (GetRunningObjectTable(0, out IRunningObjectTable pprot) == 0)
{
pprot.EnumRunning(out IEnumMoniker ppenumMoniker);
ppenumMoniker.Reset();
var moniker = new IMoniker[1];
while (ppenumMoniker.Next(1, moniker, IntPtr.Zero) == 0)
{
CreateBindCtx(0, out IBindCtx ppbc);
moniker[0].GetDisplayName(ppbc, null, out string ppszDisplayName);
if (ppszDisplayName.ToLower().Contains(".dwg"))
{
object ComObject;
pprot.GetObject(moniker[0], out ComObject);
if (ComObject is null)
continue;
CADsessionSet.Add(ComObject);
}
Marshal.ReleaseComObject(ppbc);
}
}
foreach(object cs1 in CADsessionSet)
{
try
{
AcadApplication acApp = (AcadApplication)cs1;
foreach (AcadDocument doc1 in acApp.Documents)
{
if (!openDWGset.Contains(doc1.Name))
openDWGset.Add(doc1.Name);
}
}
catch(System.Exception ex1) {
System.Windows.Forms.MessageBox.Show(ex1.Message);
};
}