Dynamically Loading a 32-bit or 64-bit .NET Assembly at Runtime

The Chilkat .NET assembly is a mixed-mode assembly, meaning that it exposes a managed API, but the inner core is native. Therefore, as for any DLL, a 32-bit process must load a 32-bit DLL, and a 64-bit process must load a 64-bit DLL.

On a 32-bit system, there is never any choice and therefore this whole subject is moot.

On a 64-bit system, a 100% managed .NET app can run as either a 64-bit process or as a 32-bit process. Visual Studio projects have a setting named “Prefer 32-bit”, which is by-default ON, which means the app will always run as a 32-bit process. In that case, it is the 32-bit DLL that must be loaded (always), and again this subject is moot.

I recommend this: If your app does not use more than 4GB of memory, then simply choose to run 32-bit everywhere. You have better things to do with your time. Performance is usually just as good (if not better) running 32-bit.

I also recommend this: As time goes on, it gets more and more difficult to find yourself actually running on a 32-bit system. I’m sure it’s been many years since it’s been possible to buy a new computer running 32-bit Windows, and Microsoft is ending support for the OS’s what can run 32-bit Windows. So.. if your app will NEVER encounter a 32-bit system, and it needs more than 4GB of memory, then uncheck the “Prefer 32-bit” setting in your Visual Studio app, and ALWAYS run 64-bit. Again, you have better things to do with your time than monkeying around with additional unnecessary complexity.

If you’re in the situation where you desire either 32-bit or 64-bit, then there are two solutions:

  1. Make sure the 32-bit Chilkat DLL is distributed with your app to systems where it will run 32-bit, and the 64-bit Chilkat DLL is distributed with your app to systems where it will run 64-bit. Or…
  2. Dynamically load either the 32-bit or 64-bit DLL at runtime. Here are links to two possible solutions for doing so.

http://cknotes.com/dynamically-loading-net-assembly-32-bit-or-64-bit/

http://www.codeproject.com/Articles/528178/Load-DLL-From-Embedded-Resource

UNC Path — Could not load assembly — Failed to grant minimum permission requests.

The following error can occur when trying to load  ChilkatDotNet2.dll from a UNC path located on another computer.

Could not load file or assembly 'ChilkatDotNet2, Version=9.0.4.0, Culture=neutral, PublicKeyToken=eb5fc1fc52ef09bd' 
or one of its dependencies. Failed to grant minimum permission requests. (Exception from HRESULT: 0x80131417)

It is because the .NET runtime does not give full trust to the external UNC location.   You must do one of the following:

  1. Move the ChilkatDotNet2.dll to a directory on the local computer.
  2. Grant full trust to the remote directory.
  3. Grant full trust to the ChilkatDotNet2.dll