using System;
using System.Reflection;
using System.Collections;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestROT();
}
[DllImport("ole32.dll")]
static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);
[DllImport("ole32.dll")]
public static extern void GetRunningObjectTable(
int reserved,
out IRunningObjectTable prot);
//Requires Using System.Runtime.InteropServices.ComTypes
//Get all running AutoCAD instance by querying ROT
private static List<object> GetAcadInstances(string[] progIds)
{
List<string> clsIds = new List<string>();
//get the autocad clsid
foreach (string progId in progIds)
{
Type type = Type.GetTypeFromProgID(progId);
if (type != null)
clsIds.Add(type.GUID.ToString().ToUpper());
}
//Get Running Object Table ...
IRunningObjectTable Rot = null;
GetRunningObjectTable(0, out Rot);
if (Rot == null)
return null;
//get enumerator for ROT entries
IEnumMoniker monikerEnumerator = null;
Rot.EnumRunning(out monikerEnumerator);
if (monikerEnumerator == null)
return null;
monikerEnumerator.Reset();
List<object> instances = new List<object>();
IntPtr pNumFetched = new IntPtr();
IMoniker[] monikers = new IMoniker[1];
//Go through all entries and identifies ACAD instances
while (monikerEnumerator.Next(1, monikers, pNumFetched) == 0)
{
IBindCtx bindCtx;
CreateBindCtx(0, out bindCtx);
if (bindCtx == null)
continue;
string displayName;
monikers[0].GetDisplayName(bindCtx, null, out displayName);
foreach (string clsId in clsIds)
{
if (displayName.ToUpper().IndexOf(clsId) > 0)
{
object ComObject;
Rot.GetObject(monikers[0], out ComObject);
if (ComObject == null)
continue;
if (instances.IndexOf(ComObject) == -1)
instances.Add(ComObject);
break;
}
}
}
return instances;
}
private static void TestROT()
{
string[] progIds =
{
"AutoCAD.Application.17",
"AutoCAD.Application.17.1",
"AutoCAD.Application.17.2",
"AutoCAD.Application.18",
"AutoCAD.Application.18.1",
"AutoCAD.Application.18.2",
"AutoCAD.Application.19",
"AutoCAD.Application.19.1",
"AutoCAD.Application.19.2",
"AutoCAD.Application.20",
"AutoCAD.Application.20.1",
"AutoCAD.Application.20.2",
"AutoCAD.Application.21",
"AutoCAD.Application.21.1",
"AutoCAD.Application.21.2"
};
List<object> instances = GetAcadInstances(progIds);
foreach (object acadObj in instances)
{
try
{
//Late bind property "AcadApplication.Preferences.Profiles.ActiveProfile"
object Preferences = acadObj.GetType().InvokeMember("Preferences",
System.Reflection.BindingFlags.GetProperty,
null, acadObj,
null, null, null, null);
object Profiles = Preferences.GetType().InvokeMember("Profiles",
System.Reflection.BindingFlags.GetProperty,
null, Preferences,
null, null, null, null);
string ActiveProfile = Profiles.GetType().InvokeMember("ActiveProfile",
System.Reflection.BindingFlags.GetProperty,
null, Profiles,
null, null, null, null) as string;
Console.WriteLine(ActiveProfile);
Console.ReadKey();
}
catch
{
continue;
}
}
}
}
}