【问题标题】:Interop error with earlier versions of Framework与较早版本的框架的互操作错误
【发布时间】:2026-02-10 14:35:01
【问题描述】:

我的项目中引用了一个 COM 对象。当我使用目标框架 4.5 构建项目时,一切正常。但是,如果我将目标框架更改为 3.5(或 2.0),则代码在尝试使用 DLLName.exe 实例化类时会引发异常(我认为它是 ActiveX EXE)

以下是错误(我是从外语翻译过来的,所以可能不准确):

System.BadImageFormatException: Could not load file or assembly  'Interop.DLLname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
File name: 'Interop.DLLname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
....

Assembly loaded from:  C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
Running under file execution  C:\Users\Admin\Documents\Visual Studio 2012\Projects\....t\bin\Debug\EXEname.vshost.exe
#NAME?
=== Pre-bind state information ===
LOG: User = 
LOG: DisplayName = Interop.DLLname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/Users/Admin/Documents/Visual Studio 2012/Projects/............/bin/Debug/
LOG: Initial PrivatePath = NULL
Assembly calls : EXEname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
....
LOG: Using computer file configuration from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
....
ERR: Failed to complete assembly definition (hr = 0x8013101b). 

我尝试从 bin/obj 文件夹中删除 interop.DLLname.dll,我检查了构建中的所有项目都针对 Framework 2.0

我还错过了什么吗?

【问题讨论】:

  • 当我尝试它时效果很好。在互操作库上运行 corflags.exe 实用程序进行额外检查。

标签: c# .net interop


【解决方案1】:

您收到此错误是因为 com 包装器 dll (interop.DLLname.dll) 是为 .net 4.5 生成的,显然由于 CLR 版本不同,它不能在早期版本的 .net 中使用。您需要获取 .net 2.0 的包装器并引用它。这是有关如何生成 com 包装器的链接http://msdn.microsoft.com/en-us/library/ms404285%28v=vs.80%29.aspx

【讨论】:

  • 据我所知,我正在按照该链接的指示进行操作。正如我在问题中所写,我删除了旧版本的互操作,并且构建生成了一个新版本。为什么它没有生成一个与项目的最低目标框架集兼容的新框架?互操作框架是否还有其他设置?
  • 您需要使用 Tlbimp.exe for CLR 2.0 SDK 这里已经回答了*.com/questions/2659738/…
  • 我尝试了*.com/questions/2659738/… 中提到的所有内容,但对我没有任何效果。
  • 您对*.com/questions/2659738/…中提出的问题是正确的
【解决方案2】:

在这种情况下唯一可行的解​​决方案是安装 Visual Studio 2008 并在那里创建互操作。

【讨论】: