The .NET Assembly “Incorrect Format” Error

If you get this error in a Windows Forms project…

Look in your Project–>Properties (ALT+F7) and go to the “Build” tab.
You’ll see the “Platform target”

– If it’s set to “x64”, then make sure you reference the 64-bit Chilkat assembly.
– If it’s set to “x86”, then make sure you reference the 32-bit Chilkat assembly.
– If it’s set to “Any CPU” and the “Prefer 32-bit” checkbox is checked, then your app will run in 32-bit mode and you should reference the 32-bit Chilkat assembly.
– If it’s set to “Any CPU” and the “Prefer 32-bit” checkbox is NOT checked, then your app will run according to the computer (64-bit or 32-bit). It is unlikely you’re developing on a 32-bit Windows computer, so in that case you’d reference the 64-bit Chilkat assembly.

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

Using Assembly in ASP.NET – Simplest Example

This example demonstrates the simplest method for using a .NET assembly in an ASP.NET web page.

1) Create a new file “helloWorld.aspx” in your web site’s root directory. Using a text editor, add this:

<%@ Page Language="C#" %>
<%@ Import Namespace="Chilkat" %>

<html>
<head>
<title>ASP.NET Hello World</title>
</head>
<body bgcolor="#FFFFFF">

<p>
<%
	Chilkat.Ftp2 ftp = new Chilkat.Ftp2();
	Response.Write(ftp.Version);
%>
</p>

</body>
</html>

2) Create a “bin” directory in the same directory where the helloWorld.aspx file is located.

3) Copy the managed assembly (in this case it is ChilkatDotNet2.dll) into the “bin” directory.

4) That’s it. Browse to http://www.yourwebsite.com/helloWorld.aspx. The time to load the first page hit will be significantly longer than subsequent page loads. Errors are most likely permission related. Feel free to send error listings to support@chilkatsoft.com