如何在 .NET 中使用 VB6 dll 中的方法?

How to use method from VB6 dll in .NET?

我有 ActiveX VB6 DLL(无法访问其源代码),并且想调用方法 'GetUnitInfo'

我习惯像这样导入它:

[DllImport(@"C:\Users\R\Documents\lab.dll")]
public static extern long GetUnitInfo(String strRequest, String strInfo, String strName);

但我得到一个例外:

Unable to find an entry point named 'GetUnitInfo' in DLL

我也试过加载它:

Assembly myAssembly ;
myAssembly = Assembly.LoadFile("C:\Users\R\Documents\lab.dll");

但是出现异常

The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018)

我尝试清理解决方案、关闭项目选项中的清单、创建新的清单文件,但它对我没有帮助...

已找到解决方案,mb 其他人会发现有用的,(这在我的案例中有效):

  1. 创建 VB6 ActiveX dll 的 .Net 包装器

    1.1 运行 CMD 作为管理员

    1.2 移动到 .NET SDK 文件夹 - cd C:\Program Files\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\(在我的例子中)。

    1.3 运行 - TlbImp.exe C:\path_to_old.dll \out: new.dll

  2. 注册 ActiveX dll

    2.1 运行 CMD 作为管理员

    2.2 运行 - regsvr32.exe C:\path_to_old.dll

  3. 在 c# 项目中添加对转换后的 dll ("new.dll") 的引用

我曾经在注册 "old.dll" 之前添加 "new.dll" 引用,但出现以下异常

Retrieving the COM class factory for component with CLSID {F2D4F4E5-EEA1-46FF-A83B-A270C92DAE4B} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

清洁解决方案,删除参考并执行步骤 2,3 - 有帮助

您可能还会发现这篇文章有用

C-Sharp-and-activex-dll

Error adding reference to dll: Solution tlbimp.exe