views:

458

answers:

2

In a C# project we add a reference to a COM object via the Add References setup pointing to a COM object which results in the IDE auto-generating the interop assembly. So this is fine and good, but we are building based on .net 3.5 SP1 aka CLR 2.0, and the generated interops are using the 4.0 CLR making them incompatible. Is there a way to prevent this?

I assume the other option is configure our build script to try using tlbimp.exe with the /references parameter? to point to mscorlib v2.0?

Anyhow, I'm hoping there's a flag somewhere to allow this.

+1  A: 

I encountered exactly this issue. The solution I found was to use version 3.5 of tlbimp from the .Net Framework SDK (or Windows Platform SDK?) located in %ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin that used CLR 2.

I also found I needed this info to get the correct type library from the exe file i was importing, as VS would use only the first type library:

"A resource ID can optionally be appended to a type library file when importing a type library from a module containing multiple type libraries."

tlbimp MyModule.dll\1

from http://msdn.microsoft.com/en-us/library/tt0cf3sx%28VS.80%29.aspx

Skym
A: 

I had the same exact problem, but even with v2.0 of tlbimp.exe, I still get a 4.0 dll, which won't work.
I ended up with a simpler solution, in case someone runs into this:
Register the dll with regsvr32 (make sure you run that as admin or you'll get an error) then when adding the reference in the project, you'll find your dll in the COM tab.
Worked like a charm!

Unless you want to create the interop dll to ship it with your application, then you'll need to figure out the tlbimp.exe route.

Beemer