vb.net 无法创建 ActiveX 组件。在 autocad 2011 中使用 GetObject 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19221672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:23:03  来源:igfitidea点击:

Cannot create ActiveX component.Error while using GetObject in autocad 2011

vb.netautocad

提问by Preeti

I am using below code to use autocad object.

我正在使用下面的代码来使用 autocad 对象。

Dim acadapp As AcadApplication

acadapp = GetObject(, "AutoCAD.Application")

'''and using below code to create object -------------

'''并使用下面的代码创建对象 -------------

acadapp = CreateObject("AutoCAD.Application")

Getting error "Cannot create ActiveX component". I tried using 18,19 and various combination as below :

出现错误“无法创建 ActiveX 组件”。我尝试使用 18,19 和各种组合如下:

acadapp = GetObject(, "AutoCAD.Application.18")

But nothing work. Please help.

但没有任何作用。请帮忙。

@Locke : Thanks for reply.I tried your soltion as below :

@Locke:感谢您的回复。我尝试了您的解决方案,如下所示:

Dim acadType As Type

Try
   acadapp =   
   System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application.18.1")
   ''Above code din't worked so tried below code also
    ' acadapp = DirectCast(Marshal.GetActiveObject("AutoCAD.Application.18.1"), 
    'AcadApplication)

Catch ex As Exception           
  acadType = Type.GetTypeFromProgID("AutoCAD.Application")
  acadapp = DirectCast(Activator.CreateInstance(acadType, True), AcadApplication)           
End Try

Showing Exception :

显示异常:

Unable to cast COM object of type 'System.__ComObject' to interface type 'AutoCAD.AcadApplication'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{8E75D910-3D21-11D2-85C4-080009A0C626}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

无法将“System.__ComObject”类型的 COM 对象转换为“AutoCAD.AcadApplication”类型的接口。此操作失败,因为 IID 为“{8E75D910-3D21-11D2-85C4-080009A0C626}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE)) .

回答by Parrish Husband

Here's what I typically use when dealing with AutoCAD interop. It checks for a running instance, and creates a new one if necessary:

这是我在处理 AutoCAD 互操作时通常使用的内容。它检查正在运行的实例,并在必要时创建一个新实例:

private static AcadApplication GetAcadApp(string progId)
{
    // Create the return application
    AcadApplication returnApp = null;

    try
    {
        // Try getting a running instance
        returnApp = (AcadApplication)Marshal.GetActiveObject(progId);
    }
    catch (COMException)
    {
        try
        {
            // Try creating a new instance
            Type acadType = Type.GetTypeFromProgID(progId);
            returnApp = (AcadApplication)Activator.CreateInstance(acadType, true);
        }
        catch (COMException)
        {
            // Report failure
            MessageBox.Show(string.Format("Cannot create object of type \"{0}\"", progId));
        }
    }

    // Return the application
    return returnApp;
}

An AcadApplication COM object can be set like this:

AcadApplication COM 对象可以这样设置:

// Get/create an AutoCAD instance
var acadApp = getAcadApp("AutoCAD.Application.18");

Regardless of C# or VB.NET, using Marshal.GetActiveObject and Activator.CreateInstance are probably the better ways to approach this.

不管是 C# 还是 VB.NET,使用 Marshal.GetActiveObject 和 Activator.CreateInstance 可能是解决这个问题的更好方法。

回答by Owen Wengerd

According to the exception, the problem is not the GetActiveObject() call, but that the returned object doesn't support the interface you're looking for. Most likely reason is that your code references a different version of AcadApplication than the one you're getting back from GetActiveObject(). Change your project to reference the COM library version for the returned AutoCAD instance, and it should work fine.

根据异常,问题不在于 GetActiveObject() 调用,而是返回的对象不支持您正在寻找的接口。最可能的原因是您的代码引用的 AcadApplication 版本与您从 GetActiveObject() 返回的版本不同。更改您的项目以引用返回的 AutoCAD 实例的 COM 库版本,它应该可以正常工作。