You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Visual Studio 17中加载libsodium.dll时触发BadImageFormatException

Troubleshooting BadImageFormatException with libsodium-net

First, let’s cut to the chase: BadImageFormatException almost always boils down to a mismatch between your managed code’s architecture (x86/x64) and the native libsodium.dll it’s trying to load. Here’s how to fix this step by step:

1. Fix DLL Placement (Critical!)

You mentioned placing 32-bit libsodium.dll in SysWOW64 and 64-bit in System32—that’s technically correct for system-wide access, but this approach is prone to conflicts and not ideal for development. Instead:

  • Skip system directories for development: Copy the appropriate libsodium.dll directly to your project’s output directory (e.g., bin\x86\Debug or bin\x64\Debug) based on your build configuration. This ensures your app grabs the exact DLL version it needs without system-wide interference.
  • Double-check DLL architecture: Make 100% sure the DLL matches your build target. x86 builds need the 32-bit DLL; x64 builds need the 64-bit one. A mismatch here is the #1 cause of this exception.

2. Let NuHandle Native DLLs (Stop Manual Copies!)

The libsodium-net NuGet package should handle native DLL deployment automatically if you install the full set of dependencies. Confirm you’ve installed both:

  • libsodium-net (the managed wrapper)
  • libsodium-native (the native DLLs, which are pulled in as a dependency if you install via NuGet properly)

If set up correctly, NuGet will copy the right DLL to your output directory based on your build platform. Delete any manually copied DLLs to avoid conflicts—let the package manager do the work.

3. Fix Visual C++ Redistributable Issues

Even though you have 2017 installed, libsodium.dll requires the architecture-specific version of the redistributable:

  • For x86 builds: Install the x86 version of Visual C++ Redistributable 2017 (2017 is backward compatible with 2015, but x86 is non-negotiable here).
  • For x64 builds: Install the x64 version.
  • If you get an "already installed" message, repair the 2017 redistributable: Go to Control Panel > Programs > Programs and Features, find the Visual C++ 2017 Redistributable, right-click, and select "Repair" to fix missing components.

4. Correct Project Build Configuration

  • Set an explicit platform target: Go to your project properties > Build > Platform target. Pick either x86 or x64 (avoid Any CPU for native interop scenarios—its behavior can be unpredictable).
  • Disable "Prefer 32-bit": If you must use Any CPU temporarily, uncheck this option to prevent the runtime from forcing 32-bit mode on a 64-bit system.
  • Clean and rebuild: After changing configurations, run Build > Clean Solution then rebuild to wipe old DLLs and ensure fresh copies are deployed.

5. Debug DLL Loading (Advanced)

If you’re still stuck, use tools to diagnose where your app is looking for the DLL:

  • Dependency Walker: Open your output executable to check for missing dependencies in libsodium.dll.
  • Process Monitor: Filter for LoadDLL events to see exactly which directories your app is scanning for libsodium.dll—this can reveal if it’s picking up the wrong version from a random system folder.

Quick Note on Your Code

Looking at your VB.NET code, there’s nothing obvious that would trigger this exception—BadImageFormatException is purely a native DLL loading issue, not a managed code problem. Once you fix the architecture/DLL mismatch, your calls to CryptoHash.Sha512, PasswordHash.ScryptGenerateSalt, etc., should work as expected.


内容的提问来源于stack exchange,提问作者TOTM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:26:26