Unable to Load Native DLL Error

There are two situations (outlined below) in which you might see the following error:

Copy
1
2
3
4
5
6
7
'The type initializer for 'PSPDFKitInternal.swigPINVOKE' threw an exception.'

Inner exception 1
TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception.

Inner exception 2:
DllNotFoundException: Unable to load DLL 'pspdfkit_dot_net': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Incorrect Build Configuration

In order for the PSPDFKit .NET Library to function, the project and solution build configuration must be set to either x86 or x64. Any CPU will not work because the application does not understand which native binary to use. For more information on configuration, please see the Microsoft documentation.

Aggressive Caching

Often, .NET build tools get into a situation where the cache is too aggressive. This can happen when switching between branches with fundamental build changes or when switching the OS when using the same project folder. Because of this issue, sometimes the applicable library files are not carried over from the PSPDFKit .NET NuGet package.

One of the best ways to get around this is to completely clean out the project build folders. This involves a few manual steps:

  1. From the Visual Studio toolbar, click Build > Clean Solution.
  2. In a file browser, navigate to the project folder and remove the bin, obj, and packages folders.
  3. From the Visual Studio toolbar, click Build > Clean Solution.
  4. From the Visual Studio toolbar, click Build > Rebuild Solution.
  5. Attempt to run the program again.