王华康 发表于 2009-7-31 15:15:00

(AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17")出现异常

<p>运行时出现错误,是第一句的,请问高手如何解决</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcadApplication app = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17");<br/>&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcadMenuBar menuBar = app.MenuBar;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcadMenuGroup menuGroup = app.MenuGroups.Item(0);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcadPopupMenus menus = menuGroup.Menus;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcadPopupMenu mymenu = menus.Add("我的菜单");</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mymenu.AddMenuItem(0,"第一个菜单", "hello");<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mymenu.AddSeparator(1);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mymenu.AddMenuItem(2, "2", "hello");<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcadPopupMenu ext = mymenu.AddSubMenu(3, "Ext");<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ext.AddMenuItem(0, "Hello", "hello");<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ext.AddSeparator(1);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ext.AddMenuItem(2, "Hello2", "hello");<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mymenu.InsertInMenuBar(menuBar.Count - 2);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.Application.Update();</p><p>另请问上面加载菜单至主菜单的方法正确吗?</p><p></p><p>错误信息如下:</p><p>应用程序不支持实时(JIT)调试。<br/>有关详细信息,请参见此消息的结尾。</p><p>************** 异常文本 **************<br/>System.Reflection.TargetInvocationException: 调用的目标发生了异常。 ---&gt; System.InvalidCastException: 无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“Autodesk.AutoCAD.Interop.AcadApplication”。此操作失败的原因是对 IID 为“{93BC4E71-AFE7-4AA7-BC07-F80ACDB672D5}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))。<br/>&nbsp;&nbsp; 在 ClassLibrary.Class.AddMenuCom() 位置 E:\CAD2007 Lab\CAD_lab2\CAD_lab2\Class.cs:行号 160<br/>&nbsp;&nbsp; --- 内部异常堆栈跟踪的结尾 ---<br/>&nbsp;&nbsp; 在 System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct&amp; sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)<br/>&nbsp;&nbsp; 在 System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)<br/>&nbsp;&nbsp; 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)<br/>&nbsp;&nbsp; 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)<br/>&nbsp;&nbsp; 在 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)<br/>&nbsp;&nbsp; 在 AcMgCommandClass.InvokeWorker(AcMgCommandClass* , MethodInfo mi, Object commandObject, Boolean bLispFunction)<br/>&nbsp;&nbsp; 在 AcMgCommandClass.Invoke(AcMgCommandClass* , gcroot&lt;System::Reflection::MethodInfo ^&gt;* mi, Boolean bLispFunction)<br/>&nbsp;&nbsp; 在 Autodesk.AutoCAD.Runtime.TargetInvocationSEHExceptionFilter.InvokeWorker()<br/>&nbsp;&nbsp; 在 Autodesk.AutoCAD.Runtime.ExceptionFilter.Invoke()</p><p><br/>************** 已加载的程序集 **************<br/>mscorlib<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 2.0.0.0<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 2.0.50727.42 (RTM.050727-4200)<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll">file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll</a><br/>----------------------------------------<br/>acdbmgd<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 17.0.54.0<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 17.0.54.0<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///D:/Program%20Files/AcdbMgd.DLL">file:///D:/Program%20Files/AcdbMgd.DLL</a><br/>----------------------------------------<br/>msvcm80<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 8.0.50727.762<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 8.00.50727.762<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///C:/WINDOWS/WinSxS/x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_6b128700/msvcm80.dll">file:///C:/WINDOWS/WinSxS/x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_6b128700/msvcm80.dll</a><br/>----------------------------------------<br/>System<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 2.0.0.0<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 2.0.50727.42 (RTM.050727-4200)<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll">file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll</a><br/>----------------------------------------<br/>System.Xml<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 2.0.0.0<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 2.0.50727.42 (RTM.050727-4200)<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll">file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll</a><br/>----------------------------------------<br/>System.Drawing<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 2.0.0.0<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 2.0.50727.42 (RTM.050727-4200)<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll">file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll</a><br/>----------------------------------------<br/>System.Windows.Forms<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 2.0.0.0<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 2.0.50727.42 (RTM.050727-4200)<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll">file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll</a><br/>----------------------------------------<br/>acmgd<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 17.0.54.0<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 17.0.54.0<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///D:/Program%20Files/acmgd.DLL">file:///D:/Program%20Files/acmgd.DLL</a><br/>----------------------------------------<br/>AcDxUi<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 17.0.54.0<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 17.0.54.0<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///D:/Program%20Files/AcDxUi.DLL">file:///D:/Program%20Files/AcDxUi.DLL</a><br/>----------------------------------------<br/>AcDx<br/>&nbsp;&nbsp;&nbsp; 程序集版本: 17.0.54.0<br/>&nbsp;&nbsp;&nbsp; Win32 版本: 17.0.54.0<br/>&nbsp;&nbsp;&nbsp; 基本代码: <a href="file:///D:/Program%20Files/AcDx.DLL">file:///D:/Program%20Files/AcDx.DLL</a><br/>----------------------------------------</p>

王华康 发表于 2009-8-2 09:52:00

检索出clsid为6AB55F46-2523-4701-A912-B226F46252BA,请部如何加入?

wj_job 发表于 2009-7-31 16:10:00

你用CLSID去查找CAD接口然后再去操作。

menxin 发表于 2013-8-15 18:24:38

var app =Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as AcadApplication;

zfbj 发表于 2013-8-29 14:08:28

如果是VC中使用,我的代码是这么写的;
IAcadApplication acadApp;
                                        CLSID clsid;
                                        LPUNKNOWN pUnk = NULL;
                                        LPDISPATCH pDispatch = NULL;
                                       
                                        HRESULT hr = ::CLSIDFromProgID(L"AutoCAD.Application", &clsid);                       
                                        if (SUCCEEDED(hr))
                                        {
                                                if(::GetActiveObject(clsid, NULL, &pUnk) == S_OK)                // 如果有AutoCAD正在运行就获得它的OLE指针
                                                {
                                                        pUnk->QueryInterface(IID_IDispatch, (LPVOID*)&pDispatch);
                                                        acadApp.AttachDispatch(pDispatch);
                                                        pUnk->Release();
页: [1]
查看完整版本: (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17"