.NET runtime threw DllNotFoundException when loading a native library
System.DllNotFoundException: Unable to load DLL '<library>': The specified module could not be found.
The .NET runtime threw a DllNotFoundException because a P/Invoke call (or a library using one) attempted to load a native DLL that could not be found in any of the expected search paths. The DLL may be missing from the deployment, in the wrong location, targeting the wrong architecture, or missing its own dependencies.
System.DllNotFoundException: Unable to load DLL '<library>': The specified module could not be found.
Why it happens
The native DLL file is not present in the application directory, system32, or any directory listed in the PATH environment variable.
The DLL is present but targets a different architecture (x86 vs x64) than the .NET process loading it.
The DLL itself has dependencies on other native DLLs (Visual C++ Redistributable, platform SDK libraries) that are not installed on the target system.
On Linux/macOS, the shared library (.so or .dylib) is not installed, the library name mapping is incorrect, or the library is not in the dynamic linker search path.
The `DllImport` attribute specifies only the DLL name without an extension and the runtime cannot locate the correct file by probing.
How to fix it
Confirm the DLL exists in the application's output directory or a path on the system PATH. For .NET applications, the directory containing the executable is searched first.
Verify architecture alignment: if the .NET app is 64-bit, the native DLL must also be 64-bit. Use the `dumpbin /headers` tool on Windows to check the DLL's machine type.
Use Dependency Walker or `dumpbin /dependents` on Windows to check what other DLLs the native library depends on, then ensure those are also present. Missing Visual C++ Redistributables are a very common cause.
Install the correct Microsoft Visual C++ Redistributable package for the DLL's build toolchain version from the Microsoft website.
On Linux, verify the shared library is installed: `ldconfig -p | grep <libname>`. Install via the system package manager if missing. On macOS, check with `otool -L <binary>`.
If distributing the app, set the DLL to Copy to Output Directory in project properties, or include it as a NativeLibrary in your project file.
Use `NativeLibrary.SetDllImportResolver` to implement custom DLL resolution logic for cross-platform or non-standard deployment scenarios.