Managed code links between any x64 processor

I have a C ++ / CLI mixed mode that compiled as x86 and x64 frozen dlls.

I have a C # application that should use this project, which is compiled as "Any processor." How can I correctly reference the correct dll from a C # application? If I right-click the link to the link, I have to select only one of the two dll libraries.
My "ANY CPU" C # application sometimes runs as x64, and sometimes as an x86 process.

I heard that you can do this through a smart configuration file.

+4
source share
2 answers

Even if your C ++ project is compiled twice, it should have the same managed interface for x86 and x64 DLL. Therefore, if you do not use strong name signing, it does not matter which version of the DLL you are referencing. The important thing is that the correct version is deployed on the target machine.

+1
source

There are several ways to handle this. The first one is simple, acknowledge the fact that your application really has a dependency on the target architecture. And that AnyCPU is not a proper setting. Very rarely, the huge memory space of the virtual address that you get from x64 is required, especially since you also need and need to make it work on x86. So set the target of the EXE platform to x86, and you're done.

Secondly, this means that it’s just a deployment problem. All you have to do is copy the x64 assembly of the mixed mode assembly when your application is installed on a 64-bit operating system and x86 on a 32-bit operating system. You will need to create an installation project that takes care of this. The easiest way is to create two of them. Also the only way afaik.

Thirdly, the one who has the bells, where he works in any case, the one that you undoubtedly ask. This requires changes to the code, project, and installer. What you need to do is write a post-build event that will create two subdirectories with names like "x86" and "x64". And copy the corresponding version of the DLL into them. This ensures that the CLR cannot find these assemblies.

In your code, you must write an event handler for the AppDomain.CurrentDomain.AssemblyResolve event. Sign it in your Main () method before trying to use any types from the assembly. An event handler should use Assembly.LoadFrom () to load the correct assembly from a subdirectory based on the value of IntPtr.Size. This is 8 when you run 64-bit mode.

I should mention another approach, but usually frowned at SO. Install both assemblies in the GAC. Everything is automatic.

+5
source

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


All Articles