代码之家  ›  专栏  ›  技术社区  ›  dretzlaff17

在VB6中引用.NET程序集将不起作用

  •  9
  • dretzlaff17  · 技术社区  · 15 年前

    public class MyClass
    
        [ComVisible(true)]
        public string GetResponse()
        {
            return "Testing Response"
        }
    
    }
    

    我构建程序集并将文件复制到一个文件夹中。测试中断.dll

    然后我运行一个批处理文件来注册汇编工具,以便为COM注册对象。

    cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\
    regasm "c:\Program Files\TestApp\TestInterop.dll" /tlb:TestInterop.tlb
    

    我打开一个新的VB6应用程序并引用TestInterop.dll

    Dim obj as TestInterop.MyClass
    Set obj = new TestInterop.MyClass
    
    Dim strTest as string
    
    strTest = obj.GetRespose()
    

    Run-time error' -2147024894 (80070002'):
    Automation error
    The system cannot find the file specified
    

    GetResponse 方法。这正常吗?

    5 回复  |  直到 15 年前
        1
  •  13
  •   Mike Burton    15 年前

    您需要将您的.NET程序集放在GAC中,或者使用/codebase命令行开关运行RegAsm(它会抱怨,但这至少会起作用)。不幸的是,没有智能是正常的。

        2
  •  4
  •   Jonathan Allen    15 年前

    上次我看到我忘了硬编码guid。所以每次我重新编译VB都找不到我的代码。这是一个VB.NET的模板(不要使用这些guid,请自己制作。)

    <ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
    Public Class ComClass1
    
    #Region "COM GUIDs"
        ' These  GUIDs provide the COM identity for this class 
        ' and its COM interfaces. If you change them, existing 
        ' clients will no longer be able to access the class.
        Public Const ClassId As String = "eaf83044-f0a7-417b-b333-e45aec398ca5"
        Public Const InterfaceId As String = "84e0fb8f-266d-40e6-9e8c-3d4eb37d3bf0"
        Public Const EventsId As String = "22ea2214-032f-4eb6-b2d4-c5dd213bab87"
    #End Region
    
        ' A creatable COM class must have a Public Sub New() 
        ' with no parameters, otherwise, the class will not be 
        ' registered in the COM registry and cannot be created 
        ' via CreateObject.
        Public Sub New()
            MyBase.New()
        End Sub
    
    End Class
    
        3
  •  1
  •   Esteban Villalobos    15 年前

    我注意到您不需要手动运行RegAsm,实际上只需将AssemblyInfo属性ComVisible设置为true:

    也可以转到“项目属性”->应用程序->装配信息->使程序集COM可见,并设置复选框。

    从VB6使用程序集不需要将要创建的程序集注册到GAC中。

        4
  •  0
  •   shahkalpesh    15 年前

    我认为tlb文件是在framework目录下生成的,而不是这个目录(c:\programfiles\TestApp)。

    这就是问题所在吗?

        5
  •  0
  •   Alex    15 年前

    我有-2147024894错误,或其他错误,无论我尝试,直到我运行vb6消费代码直接从exe。有关VB6调试器的某些内容阻止我在运行时使用dll。我甚至不能实例化这个对象。我可以参考tlb在设计时,并有完善的智能支持。当我在visualstudio之外启动应用程序时,一切都运行得很好。希望这对别人有帮助。