8

We have an old C/C++ .dll that is COM registered. Our customers have both native- and .NET clients that use this .dll.

We have built a new .NET .dll to replace the old one, i.e. their COM interface is identical. We would like to replace the old .dll without our customer need to recompile or do anyting to their clients.

For native clients it works fine to simply unregister the old .dll and register the new one (with regasm). It also works for some .NET clients. However, in those cases the both the client and the new .dll is compiled with the same .NET version it throws the exception below.

In other words, this works:

.dll is .NET 3.5 -> client is .NET 4.0
.dll is .NET 4.0 -> client is .NET 3.5
.dll is any .NET -> Client is native

This throws the exeption below:

.dll is .NET 4.0 -> client is .NET 4.0
.dll is .NET 3.5 -> client is .NET 3.5

[A]BARAPIXLib.barcom5 cannot be cast to [B]BARAPIXLib.barcom5.

Type A originates from 'BARAPIXLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location C:\arkiv\S_BTW\BTW\BARAPIXWebService\Barapix\bin\BARAPIXLib.dll'.

Type B originates from 'BartrackTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\arkiv\Bartrack\BartrackTest\x86\Src\BartrackTest\bin\x86\Release\BartrackTest.exe'."}

Any ideas would be appreciated.

Nate
  • 30,286
  • 23
  • 113
  • 184
Poppert
  • 447
  • 1
  • 5
  • 16
  • Any chance we can see the declaration of the DLL method reference as well as some calling code? – pickypg May 11 '11 at 16:28
  • Do you mean how the clients call our .dll? That is unknown to us, but they will likely have done "Add Reference" in Visual Studio. We want to replace our old C/C++ COM .dll with a new .NET one without the clients need to be recompiled or anything. If this is possible. – Poppert May 16 '11 at 15:19

2 Answers2

1

Try unregistering any previous version and check that the dll is in the same folder as the executable. Also try looking at where it is you are loading the dll from. I think that you are loading it manualy so look at the address you are referencing the wrong dll.

  • Thanks for your reply! We unregistered the old C/C++ .dll, replaced the file with the new .NET one (using same file name and in the same directory), and registered it using regasm. But the client still don't work (without having to recompile etc.). It basically says Cannot cast SomeTypeA to SomeTypeAClass ("Class" is somehow appended to type name). – Poppert May 16 '11 at 15:14
1

This might be because in the case where you are using the same version of .net framework, the instance returned to the client is not longer a COM wrapper but a pure .Net object, so when you try to cast it to a COM interface it fails. There is a similar question here. The solution involves using Primary Interop Assembly.

Community
  • 1
  • 1
yms
  • 10,361
  • 3
  • 38
  • 68
  • Thanks for your reply! Our customers have already made "Add Reference" to our old C/C++ COM .dll, so there is an "Interop.blablabla.dll" in the client dir that the client .exe uses. Can we force our customers' clients to use a new Primary Interop Assembly instead of the one they are already using (without they having to recompile etc. their clients)? – Poppert May 16 '11 at 15:02
  • @Poppert I am affraid this is not possible. As far as I know the PIA will probably have different type names. Either way it might be a good idea to post a new SO question on this topic specifically, you may get some creative solutions. – yms May 16 '11 at 15:25