与早期版本的Framework的Interop错误

时间:2013-06-27 08:20:52

标签: c# .net interop

我有一个在我的项目中引用的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

我错过了其他什么吗?

2 个答案:

答案 0 :(得分:1)

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

的链接

答案 1 :(得分:0)

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