ektrah/libsodium-core

Getting BadImageFormatException when building for AnyCPU

Closed this issue · 6 comments

When I try to use this library and build my project for AnyCPU I get this error:

System.TypeInitializationException: Der Typeninitialisierer für "Sodium.SodiumCore" hat eine Ausnahme verursacht. ---> System.BadImageFormatException: Es wurde versucht, eine Datei mit einem falschen Format zu laden. (Ausnahme von HRESULT: 0x8007000B)

When I build for x64 I do not get errors.

Well, as this library is just a "wrapper" for a native library which are compiled for either 32 or 64 bit, that seems like a reasonable error to get.

I would have expected that the wrapper is able to determine which runtime is currently running to use the correct native library.
Is this unrealistic?

Are you using .NET Core or .NET Framework? If you're using .NET Core, then the correct library should automatically be selected.

Facing this issue only for .NET framework. For .NET Core the library gets loaded correctly. How can I get this working for .NET Framework? The issue seems to be that the libsodium.dll is not getting copied to the output directory during Build.

.NET Framework unfortunately lacks some of the capabilities for native libraries that .NET Core has.
That means you basically have two options for a .NET Framework project:

  1. Set the target platform to x64 instead of AnyCPU.
    (You also need a recent version of .NET Framework.)

or

  1. Switch to .NET Core.

What I'm looking for is something like what has been implemented in the repo from which this repo is forked, adamcaudill/libsodium-net. Here, programmatically the libsodium.dll is copied correctly to the output directory. I'm not sure why that code is not in this repository or why it was removed.