简体   繁体   English

在Visual Studio中添加VB6引用会导致“类型库导入程序在类型验证期间遇到错误”

[英]Adding a VB6 reference in Visual Studio gives “Type library importer encountered an error during type verification”

I am in the process of convering a rather large project written in VB6 into C#. 我正在将用VB6编写的一个相当大的项目转换为C#。 Given the size of the project being moved, it is being done in phases over the course of 18-months. 鉴于项目的规模正在移动,它将在18个月的时间内分阶段完成。 I am running into an issue with adding a reference of a VB6 ActiveX dll to a .Net project. 我遇到了一个问题,即将一个VB6 ActiveX dll的引用添加到.Net项目中。

If you follow exactly these steps, you too should be able to recreate the problem. 如果您完全遵循这些步骤,您也应该能够重新创建问题。

I have written an interface in .Net that is COM visible: 我在.Net中编写了一个COM可见的界面:

<ComVisible(True)>
Public Interface ITestInterface
    Property A As String
    Function TestMethod() As String
End Interface

By selecting "Register for COM interop" in Compile tab of project properties, you get a TLB file. 通过在项目属性的“编译”选项卡中选择“注册COM互操作”,可以获得TLB文件。

I've created a VB6 project that references this TLB and a class that implements the interface exposed. 我创建了一个引用此TLB的VB6项目和一个实现暴露接口的类。

Implements ITestInterface

Private mA As String

Public Property Get ITestInterface_A() As String
    ITestInterface_A = mA
End Property

Public Property Let ITestInterface_A(ByVal value As String)
    mA = value
End Property

Public Function ITestInterface_TestMethod() As String
    ITestInterface_TestMethod = "From VB6"
End Function

If I set the Component tab of project properties in VB6 to use "Remote Server Files" then a TLB is automatically created when compiling. 如果我在VB6中将项目属性的“组件”选项卡设置为使用“远程服务器文件”,则在编译时会自动创建TLB。 I can view that TLB in OleView and see the following (in addition to the details of the concrete implementation done in VB6 of the interface defined in the .Net project): 我可以在OleView中查看TLB并查看以下内容(除了在.Net项目中定义的接口的VB6中完成的具体实现的细节):

// typelib filename: TestVB6Interface.dll

[
  uuid(**EF005573-BFC7-436D-A382-F906CA09F94A**),
  version(3.0)
]

// ... some other stuff

// TLib :     // TLib :  : {79EC733A-0267-4506-8D38-C4D4655E0755}
importlib("SimpleDotNetLibrary.tlb");

Now, I create a completely new .Net project. 现在,我创建了一个全新的.Net项目。 If I add a reference to the VB6 dll, I get the following error: 如果我添加对VB6 DLL的引用,我会收到以下错误:

Could not resolve COM reference " ef005573-bfc7-436d-a382-f906ca09f94a " version 3.0. 无法解析COM参考“ ef005573-bfc7-436d-a382-f906ca09f94a ”3.0版。 The type library importer encountered an error during type verification. 类型库导入器在类型验证期间遇到错误。 Try importing without class members. 尝试导入没有类成员。

However, if I launch a Visual Studio Command Prompt and run the following: 但是,如果我启动Visual Studio命令提示符并运行以下命令:

tlbimp TestVB6Interface.tlb /out:TestVB6Interface.MyInterop.dll

Then I can add that dll as a reference in my .Net solution and it works perfectly fine. 然后我可以在我的.Net解决方案中添加该dll作为参考,它完全正常。

My question. 我的问题。 What is tlbimp doing on the command line that is not being done when I just add the reference directly? 当我直接添加引用时,tlbimp在命令行上执行的操作是什么? When the message in Visual Studio says "try importing without class members" how exactly do I do that within Visual studio? 当Visual Studio中的消息说“尝试导入没有类成员”时,我在Visual Studio中是如何做到的? I know how to do that in tlbimp. 我知道如何在tlbimp中做到这一点。

I apologize for the wall of text, but I wanted to describe the situation as best I could keeping the information I felt was relevant. 我为文本墙道歉,但我想尽可能地描述这种情况,我可以保持我认为相关的信息。

The Visual Studio IDE definately takes a different path when registering DLLs for COM Interop then it does when running the command line tools from a command prompt. 在为COM Interop注册DLL时,Visual Studio IDE肯定采用不同的路径,然后在从命令提示符运行命令行工具时执行。

I doubt that Microsoft has documented this anywhere. 我怀疑微软已经在任何地方记录过这一点 However, my years of experience have proven this to be the case. 然而,我多年的经验证明了这一点。 I once ran into a situation in which a "regsvcs" command from the .NET 2.0 Framework would actually cause an infinite loop. 我曾经遇到过一种情况,即.NET 2.0 Framework中的“regsvcs”命令实际上会导致无限循环。 If you Google it you'll probably find others that have had this problem. 如果你谷歌它你可能会发现其他人有这个问题。 I was able to make it one step further by using the VS IDE to perform the COM registration of a .NET Serviced Component. 通过使用VS IDE执行.NET服务组件的COM注册,我能够更进一步。 However, it inevitably ended in error. 但是,它不可避免地以错误结束。 The error was a step forward over the infinite loop. 错误是在无限循环上向前迈出的一步。 Either way it proved to me that the VS IDE takes a different code path / business logic when dealing with COM Interop and registry entries. 无论哪种方式,它都证明了VS IDE在处理COM Interop和注册表项时采用了不同的代码路径/业务逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM