I am new to creating .NET applications, esp mixed applications.
Recently I have created a set of (static) libraries for a client in (unmanaged) C++ (under MS Visual Studio 2010), supporting both 32-bit and 64-bit compiles, which were loaded by some applications, and which generally was fine. (Internally this functionality is using various other third party libraries, like OpenGL, boost, glm, etc)
Now he would like to be able to also use some of the functionality that I provided as a custom control in his C# application.
So I created a dll with CLR support, using C++/CLI to create wrapper classes for my functionality, then went to create a custom WPF control which is public (and therefore can be accessed from outside), again it works fine to build in both 32 and 64 bit.
Then for testing I created a simple C# application (and it's really quite simple, as I am new to C#, my background is C/C++), which can be built in 32 or 64 bit, and can successfully load the control from my DLL and do what it is expected to do.
The problem I now have is that he said he wants to be able to only distribute a single executable as "Any CPU" which should then load the appropriate code (ie 32 or 64 bit) depending on where it is launched.
So what I did was add an "Any CPU" configuration to the C# test project, and set the "platform target" under the "build" tab to "Any CPU". But now I am not sure under Configuration Manager what platform to set for the other projects (ie the C++ static libs, consumed by the C++/CLI mixed mode dll). So for testing I left it at x64, but when I build it I get the warning:
Warning MSB3270: There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "(name of my 64 bit dll)", "AMD64". <br />
This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.
It runs fine however, even though it gives me that warning. I get the same warning when I set my other projects to "Win32", just mentioning "x86" instead, but in that case it throws an exception on startup saying "An unhandled exception of type 'System.BadImageFormatException' occurred in PresentationCore.dll"
So what do I need to change it to? How can I make it load the executable and then decide which dll to load? I am really not sure how to proceed and get it to work in the way that was requested of me.
I found the solution to the problem in an article about using the Perforce libraries:
http://scottbilas.com/blog/automatically-choose-32-or-64-bit-mixed-mode-dlls/
Essentially you have to create a proxy dll, which is your output target and used as reference for the hosting C# (WPF in my case) application.
Then you create the x64 and x86 versions of the dll, which you provide alongside the executable. You have to make sure the proxy dll does NOT exist alongside the executable.
On startup you create an event handler for AppDomain.CurrentDomain.AssemblyResolve which will be fired when it can't find the proxy dll.
Then based on whether you are in a 32 or 64 bit environment it can choose the correct dll and load it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With