Using Chilkat Mono in a Visual Studio C# Project

The Chilkat Mono assembly can be used in a Visual Studio project (any version of Visual Studio).

(From a programming perspective, using the Chilkat Mono classes is the same as using the Chilkat .NET classes. The Chilkat .NET assemblies (available from Chilkat .NET Downloads are mixed-mode assemblies. The outer-layer is fully managed, and the inner core contains the native C++ implementation. The Chilkat Mono solution is different: The .NET/Mono assembly is 100% managed, and there is a 2nd DLL that contains the native C++ implementation. The 100% managed assembly (internally) calls into the native DLL using P/Invoke.)

Actually.. you don’t even need to build the 100% managed assembly.  You can instead just add the Chilkat *.cs source files to your project directly.  (If you open one of the .cs source files, you’ll see how P/Invoke is used.)

  1. Created a Visual Studio project in C:\MyProject
  2. Downloaded the Chilkat Mono .zip from https://www.chilkatsoft.com/mono.asp  and unzipped in C:\MyProject
  3. You now have a directory c:\MyProject\chilkatMono-9.5.0.    Within that directory, I have a chilkatCs directory, and a nativeDll directory.
  4. Add all of the .cs source files from chilkatCs to your Visual Studio C# project.  (Note: If you find an “Accounts.cs” source file, delete it and do not add it.)
  5. You don’t necessarily need to add all of the .cs sources, but you must add whatever is necessary to resolve all references.  For example, if you add MailMan.cs, you’ll certainly need to add Email.cs, and that in turn will require Cert.cs, etc.  A good strategy is to add everything, then delete various things that are likely unreferenced by other things.  For example, you can safely get rid of Csr.cs if not using CSR’s.
  6. Add code to your app.  For example:

    Chilkat.Zip zip = new Chilkat.Zip();
    MessageBox.Show(zip.Version);

  7. You can try building and running, but it’ll fail because the native DLL won’t be found..
  8. Copy the desired 32-bit or 64-bit native DLL into the same directory where your .exe is created.  For example, copy c:\MyProject\chilkatMono-9.5.0\nativeDll\windows\x64\chilkatMono-9_5_0.dll to c:\MyProject\bin\Debug
  9. Assuming your Visual Studio project is “Any CPU”, and you don’t have a “Prefer 32-bit” checkbox checked (in Project Settings), and that you’re running on a 64-bit system, then the .NET runtime should locate and load the 64-bit chilkatMono-9_5_0.dll, and your program runs fine.

Chilkat .NET Assemblies – Matching Visual Studio versions to .NET Framework Versions to VC++ Runtime Versions.

 

The .NET Framework version that corresponds to each Visual Studio release is as follows:

VS2017 — .NET 4.6, .NET 4.7
VS2015 — .NET 4.6
VS2013 — .NET 4.5 (actually 4.5.1, but we only care about major/minor numbers)
VS2012 — .NET 4.5
VS2010 — .NET 4.0
VS2008 — .NET 3.5
VS2005 — .NET 2.0

When using Chilkat .NET for a particular .NET Framework version, the corresponding VC++ Runtime redist (from Microsoft) needs to be installed on the system. The VC++ redist that is needed, and the download links, are shown below:

VS2017 .NET 4.6/4.7 Chilkat .NET for 4.6/4.7 needs VC++ 2017 runtime  
VS2015 .NET 4.6 Chilkat .NET for 4.6 needs VC++ 2015 runtime (also known as VC++ 14)
VS2013 .NET 4.5 Chilkat .NET for 4.5 (built with VS2013) needs VC++ 2013 runtime (also known as VC++ 12)
VS2012 .NET 4.5 Chilkat .NET for 4.5 (built with VS2012) needs VC++ 2012 runtime (also known as VC++ 11)
VS2010 .NET 4.0 Chilkat .NET for 4.0 needs VC++ 2010 runtime (x86)(x64) (also known as VC++ 10)
VS2005/2008 .NET 2.0/3.5 Chilkat .NET for 2.0/3.5 needs VC++ 2005 runtime (x86)(x64) (also known as VC++ 8)

.NET Assembly Incorrect Format Error

"Could not load file or assembly 'ChilkatDotNet2, Version=9.3.0.0, Culture=neutral,
PublicKeyToken=eb5fc1fc52ef09bd' or one of its dependencies.
 An attempt was made to load a program with an incorrect format."

The “incorrect format” error is a 32-bit vs. 64-bit mismatch.  It means the application is trying to load a 64-bit .NET assembly into a 32-bit process,  or the reverse (a 32-bit assembly into a 64-bit process).

Possible solutions are:

1) Target your app for x86 instead of “Any CPU”.  If you do this, then your application will run as a 32-bit process regardless of the machine architecture and you may always use the 32-bit Chilkat assembly.

2) If you really need to target “Any CPU”, then make sure to deploy the 32-bit Chilkat assembly w/ your app on 32-bit systems, and deploy the 64-bit Chilkat assembly to 64-bit systems.

3) If your app is running within ASP.NET or a Windows Service and you’re not quite sure whether it’s 32-bit or 64-bit, then install the 32-bit Chilkat assembly in the 32-bit GAC (Global Assembly Cache), and also install the 64-bit Chilkat assembly into the 64-bit GAC.  The .NET runtime will automatically look in the correct GAC and will find the correct assembly.

Could not load file or assembly ‘ChilkatDotNet4.dll’ or one of its dependencies.

(Thank you Josh for your helpful information!)

“I see that occasionally information from clients (with the client’s permission) end up on the blog and thought that perhaps this one would qualify. If there is mention of the C++ Runtime, I didn’t find it.

At any rate, I was having trouble getting the ChilkatDotNet4.dll to load. After a conversation with Matt, I tried installing the Microsoft Visual C++ 2010 Redistributable Package – this resolved my issue:

x86 Microsoft Visual C++ 2010 Redistributable Package
x64 Microsoft Visual C++ 2010 Redistributable Package

I’ve included the error messages below to illustrate that the .dll appears to be being found when registered to the GAC (and certainly when placed directly in the web site’s bin), but cannot load its dependencies.

When loaded into GAC but not found (or when in bin):

Could not load file or assembly ‘ChilkatDotNet4.dll’ or one of its dependencies. The specified module could not be found.

When not loaded into GAC and not in bin:

Could not load file or assembly ‘ChilkatDotNet4, Version=9.0.8.0, Culture=neutral, PublicKeyToken=eb5fc1fc52ef09bd’ or one of its dependencies. The system cannot find the file specified.”

The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Question:
We want to use the 64-bit version of the Chilkat email functionality, but we get a could not load error message (see below for full error) when i use the v8.5.0 release of the Chilkat library. This also happens when i use the 32-bit version. Is it possible that the license key is linked to a specific version of the Chilkat library in our case version 8.2.2.0?

If so how can we upgrade to 8.5.0, because we need to be able to install our software on a 64-bit machine.

Could not load file or assembly ‘ChilkatDotNet2, Version=8.2.2.0, Culture=neutral, PublicKeyToken=eb5fc1fc52ef09bd’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Answer:
The .NET runtime requires that the assembly version matches exactly. The version of the assembly used in your Visual Studio project (i.e. on your development machine) must match the version deployed to your production system. The platform may be different: win32 vs. x64, but the version must match. The solution is to use Chilkat v8.5.0 in your Visual Studio development environment. New-version upgrades are always free, so you may download the latest at Chilkat Software Component Downloads