我正在尝试通过其与 C# 外部应用程序的 COM 接口获取有关 AutoCAD 2007 的所有运行实例的信息(列出已打开文档的全名)。这是一个代码:
…
using AutoCAD = Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices.ComTypes;
…
[DllImport("ole32.dll")]
extern static IntPtr CreateBindCtx(IntPtr reserved, out IBindCtx ppbc);
…
static void ListACADDocs()
{
IntPtr HRESULT;
IBindCtx objCtx = null;
HRESULT = CreateBindCtx(new IntPtr(0), out objCtx);
IRunningObjectTable objRot = null;
objCtx.GetRunningObjectTable(out objRot);
IEnumMoniker RunningMonikers = null;
objRot.EnumRunning(out RunningMonikers);
RunningMonikers.Reset();
IntPtr pointerFetchedMonikers = IntPtr.Zero;
IMoniker[] monikerContainer = new IMoniker[1];
String strDisplayName;
Object COMObject = null;
List<Object> colACADInstances = new List<Object>();
while (RunningMonikers.Next(1, monikerContainer, pointerFetchedMonikers) == 0)
{
monikerContainer[0].GetDisplayName(objCtx, null, out strDisplayName);
objRot.GetObject(monikerContainer[0], out COMObject);
//AutoCAD.Application.17 class ID: {28B7AA99-C0F9-4C47-995E-8A8D729603A1}
if (strDisplayName == "!{28B7AA99-C0F9-4C47-995E-8A8D729603A1}")
colACADInstances.Add(COMObject);
}
foreach (Object obj in colACADInstances)
foreach(AutoCAD.AcadDocument doc in
((AutoCAD.AcadApplication)obj).Documents)
Console.WriteLine(doc.FullName);
}
结果,它找到了对应于所有 ACAD 运行实例的正确数量的名字对象,但仅从“最早的”实例中检索文档名称。看起来所有名字对象都引用单个 AutoCAD.AcadApplication 对象。有没有办法为每个实例做到这一点?