How can a C # program use the COM version of any version?

This question is a continuation of this question.

We create a dll written in C ++, providing access to some equipment. This DLL implements and accesses using COM interfaces. We also have a C # program that uses this DLL through COM objects.

We have a version problem. Indeed, when running a program in C #, she absolutely wants to use the exact version of the COM / C ++ dll that she used when compiling. That is, if a C # program was compiled using COM / C ++ dll 1.2.3.4, then the program will refuse to run with COM / C ++ dll 1.2.3.5.

Unhandled Exception: System.TypeInitializationException: The type initializer for 'MyDllVerify.App' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'MyCorp.MyDll.Interop, Version=1.2.3.4, Culture =neutral, PublicKeyToken=ced78d295d1e0f2b' or one of its dependencies. The system cannot find the file specified. File name: 'MyCorp.MyDll.Interop, Version=1.2.3.4, Culture=neutral, PublicKey Token=ced78d295d1e0f2b' at MyDllVerify.App..cctor() 

I would like to instruct the C # program to use any COM version of COM / C ++ with version 1.2.anything.

Where can I customize this in a C # project?

+1
source share
2 answers

I would suggest you not directly reference COM-dll in your C # project. If you do this, a new COM-interop-dll is always created during build. This can lead to many problems.

It would be better to approach the creation of COM-interop-dll, save it in your library folder and reference that library in your C # project. Keep this COM-interop-dll as static as possible. If you do this, you can replace the used COM-dll as much as you want if your interfaces do not change.

You can try to manipulate this interop assembly and do a version check for 1.2. * there, if you really want this (I would not recommend this, this could cause serious confusion).

Explanation: COM-interop-dll is a normal .NET assembly. It works as a wrapper between your C # code and the COM-C ++ code that you want to use in C # code.

COM-interop-dll does not need to be registered for COM. You can install this assembly as many times as you like. But this requires that your COM-dll be registered for COM.

Useful tools:

  • tlbimp
  • regasm
  • sn
+3
source

Nothing is different than the way I registered it in your previous question. You are still using <bindingRedirect> to allow the loading of the wrong version of the interop assembly.

It is quite unlikely to work in practice, messing with the Hell DLL when you use COM is extremely unreasonable. If you use early binding, COM cannot verify that you are calling the correct method. Unlike .NET, where jitter can perform such checks at runtime from metadata in the assembly. If the C ++ programmer did it right, he changed the list of types that he changed. Which will make your code bomb with E_NOINTERFACE, since you will use the pointer of the old version.

If it did not, unfortunately, is too common, then your program may encounter something unpleasant, like AccessViolationException. Or, even worse, it will not work, but it will call a completely wrong method.

Failure mode is milder, if you use late binding, you will receive one of the IDispatch errors when the method does not exist or its arguments have changed. Not that it ultimately solves anything, you still have a program that doesn't work. A mess with a Hell dll like this is only if you like living dangerously.

+3
source

Source: https://habr.com/ru/post/949197/


All Articles