1

I am using Magick.NET in my project, which renders maps of a game. Is it possible to load the X86 version when I build the tool for x86 processors and the X64 version of Magick.NET for 64bit processors? I guess the the main problem is, that the different versions of the dll have different identities.

I have written a prebuild event which copes the right dll on build, depending on the selected build configuration. I also loaded the copied assembly using Assembly.LoadFrom();, but the app always throws an exception that the assembly I added to references in VS2010 was not found. I can not add both references because they have the same namespace and the same methods.

Is there a better way to handle this except changing the reference in VS2010 for each build configuration?

Prebuild process:

echo."$(ConfigurationName)"|findstr /C:"x64" >nul 2>&1
if not errorlevel 1 (
   copy /Y "$(ProjectDir)lib\Magick.NET-x64.dll" "$(TargetDir)lib"
) else (
   copy /Y "$(ProjectDir)lib\Magick.NET-x86.dll" "$(TargetDir)lib"
)

Load assembly on x86 build:

if (IntPtr.Size == 4)
{
    Assembly assembly = Assembly.LoadFrom("lib/Magick.NET-x86.dll");
}

Exception:

System.BadImageFormatException
Magick.NET-x64, Version=6.0.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec not found
2
  • Would it bother you to deploy both dlls?. You could load the correct one at run time then, with AppDomain.AssemblyResolve. Commented Nov 19, 2013 at 22:20
  • The X64 version is 10 MB and the X86 version is 8 MB large. Too much to add them both. Commented Nov 19, 2013 at 22:23

1 Answer 1

1

I made the experience that it's best to fully separate x86 and x64 builds. So create a build configuration for each platform/build combination (usually, there will be at last four: x86 debug, x86 release, x64 debug, x64 release), with a separate output directory for each configuration for every project in the solution. It is a bit of effort in the beginning because some manual tweaking of the project files might be necessary (config dependent reference hint paths), but in the end it causes the least problems.

Sign up to request clarification or add additional context in comments.

4 Comments

I have different build configurations (the same you mentioned) but I don't want to replace the references in VS2010 all the time.
Err... You don't have to. If you manually put something like $(TargetPath)\bin\$(Configuration)\$(Platform)\Referenced.dll into the hint path, that always works. Visual Studio can't do this automatically, but it won't complain if you use macros there.
Yeah! It works like a charm. I have edited the .csproj file and changed the hint path to lib\Magick.NET-$(Platform).dll. I didn't know that I can use macros there.
Yea, took us a while to find that, too. I still find it strange that VS is making such a mess when it comes to referencing libraries for different build configurations in C#. It always worked for C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.