Yubico.NET.SDK icon indicating copy to clipboard operation
Yubico.NET.SDK copied to clipboard

[BUG] BadImageFormatException when using the library in a .NET 4.7 project compiled with AnyCPU on 32bit PC.

Open romerod opened this issue 5 months ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current Behavior

When the library is used in a project compiled with AnyCPU which is compiled on a x64 bit system the applications fails with a BadImageFormatException on 32bit PC as the Yubico.NativeShims.dll in the build output folder is the x64 version. The same thing happens when setting the Prefer32Bit flag.

The Yubico.NativeShims.targets file in the Yubico.NativeShims nuget package copies the file over depending of the architecture of the build system.

Expected Behavior

When a project is build with AnyCPU the build output should contain the x86 and the x64 bit of the dll.

And the dll to load should be detected on runtime. For example by using the LoadLibrary method of kernel32.dll. For the moment we worked around the issue with copying both dlls to the build output and then with a code similar to this:

if(Environment.Is64BitProcess)   
    NativeMethods.LoadLibrary("x64\Yubico.NativeShims.dll")
else
   NativeMethods.LoadLibrary("x86\Yubico.NativeShims.dll")

before calling any of the library methods.

Steps To Reproduce

  1. Create .NET 4.72 project with AnyCPU
  2. Add Yubico.YubiKey nuget package reference
  3. Call YubiKeyDevice.FindByTransport(Transport.All);
  4. Build it on a x64 PC and run it on a x32bit system

Can also be reproduced by setting the <Prefer32Bit>true</Prefer32Bit> instead of running it on 32Bit system.

Version

1.11.0

Version

5.4.3

Anything else?

No response

romerod avatar Sep 26 '24 10:09 romerod