0

I have a COM object that is referenced in my project. When I build the project with target framework 4.5 everything works. However if I change the target framework to 3.5 (or 2.0), the code throws an exception when trying to instantiate the class using DLLName.exe (I presume it is an ActiveX EXE)

The following is the error (I translated it from a foreign language so it may not be exact):

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). 

I have tried to remove interop.DLLname.dll from the bin/obj folders, I have checked that all the projects in the build are targeting Framework 2.0

Am I missing something else?

E Mett
  • 2,272
  • 3
  • 18
  • 37

2 Answers2

1

You are getting this error because the com wrapper dll (interop.DLLname.dll) was generated for .net 4.5, obviously it can not be used in earlier versions of .net due to the different CLR version. You need to get the wrapper for .net 2.0 and reference it. Here is link on how to generate a com wrapper http://msdn.microsoft.com/en-us/library/ms404285%28v=vs.80%29.aspx

Alexandr Mihalciuc
  • 2,537
  • 15
  • 12
  • As far as I can see, I am doing what that link instructs. As I wrote in the question, I deleted the old version of the interop and the build generated a new one. Why did it not generate a new one compatible with the minimum target framework set for the project? Is there another setting for the framework for the interop? – E Mett Jun 27 '13 at 09:46
  • 1
    You need to use the Tlbimp.exe for CLR 2.0 SDK here it is already answered http://stackoverflow.com/questions/2659738/visual-studio-2010-tlbimp-generates-net-4-0-interops-in-2-0-projects – Alexandr Mihalciuc Jun 27 '13 at 10:04
  • I tried everything mentioned in http://stackoverflow.com/questions/2659738/visual-studio-2010-tlbimp-generates-net-4-0-interops-in-2-0-projects nothing worked for me. – E Mett Jun 27 '13 at 14:16
  • You're right about the question being asked in http://stackoverflow.com/questions/2659738/visual-studio-2010-tlbimp-generates-net-4-0-interops-in-2-0-projects – E Mett Jun 27 '13 at 14:16
0

The only solution that worked in this case was to install Visual Studio 2008 and create the interop there.

E Mett
  • 2,272
  • 3
  • 18
  • 37