visual-studio-2010c#-4.0quantlibquantlib-swig

QuantLib+SWIG+C# 4.0+Visual Studio 2010: TypeInitializationException


I would like to add a small feature to QuantLib and compile it together with SWIG bindings to use in a C# project in Visual Studio 2010. I am however having problems at almost every turn. What are the steps involved in building QuantLib in Visual Studio 2010, creating the SWIG bindings, and building the C# project?

  1. I downloaded QuantLib from http://sourceforge.net/projects/quantlib/files/
  2. I downloaded Boost from http://sourceforge.net/projects/boost/files/boost/1.49.0/
  3. I downloaded the QuantLib+SWIG bindings from http://sourceforge.net/projects/quantlib/files/QuantLib/1.0/bindings/QuantLib-SWIG-1.0.zip/download
  4. I set an environment variable QL_DIR to "C:\pathToFolder\QuantLib-1.2\lib" (computer > properties > advanced system settings > advanced > environment variables)
  5. I ran the swig.cmd file located in C:\pathToFolder\QuantLib-SWIG-1.0\CSharp
  6. I opened QuantLib_vc9.sln in Visual Studio 2010
  7. For the NQuantLibc project:
    1. I included my Boost and QuantLib directories in the header directories.
    2. I included my QuantLib/lib directory in the library directories.
    3. I successfully built the NQuantLibc project
  8. For the NQuantLib_vc9 project:
    1. I made it dependent on the NQuantLibc project.
    2. I successfully built the NQuantLib_vc9 project.
  9. For the EquityOption_vc9 project:
    1. I made it dependent on the NQuantLib_vc9 project.
    2. I successfully built the EquityOption_vc9 project.
  10. When I try to run the EquityOption_vc9 project, I get a TypeInitializationException, "An attempt was made to load a program with an incorrect format."

Here's the full exception:

System.TypeInitializationException was unhandled
  Message=The type initializer for 'QuantLib.NQuantLibcPINVOKE' threw an exception.
  Source=NQuantLib
  TypeName=QuantLib.NQuantLibcPINVOKE
  StackTrace:
       at QuantLib.NQuantLibcPINVOKE.new_Date__SWIG_1(Int32 jarg1, Int32 jarg2, Int32 jarg3)
       at QuantLib.Date..ctor(Int32 d, Month m, Int32 y) in C:\Users\JRobinson\Desktop\QuantLib-SWIG-1.0\CSharp\csharp\Date.cs:line 48
       at EquityOptionTest.EquityOption.Main(String[] args) in C:\Users\JRobinson\Desktop\QuantLib-SWIG-1.0\CSharp\examples\EquityOption.cs:line 43
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.TypeInitializationException
       Message=The type initializer for 'SWIGExceptionHelper' threw an exception.
       Source=NQuantLib
       TypeName=SWIGExceptionHelper
       StackTrace:
            at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..ctor()
            at QuantLib.NQuantLibcPINVOKE..cctor() in C:\Users\JRobinson\Desktop\QuantLib-SWIG-1.0\CSharp\csharp\NQuantLibcPINVOKE.cs:line 126
       InnerException: System.BadImageFormatException
            Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
            Source=NQuantLib
            StackTrace:
                 at [long string removed]
                 at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..cctor() in C:\Users\JRobinson\Desktop\QuantLib-SWIG-1.0\CSharp\csharp\NQuantLibcPINVOKE.cs:line 106
        InnerException: 

Note that I built everything with the Debug configuation. I also tried this using the Release configuration. It didn't work.

I wish I could find a complete set of instructions detailing how to build this type of project. I found some instructions here, Compiling Quantlib via SWIG for C# but i couldn't get it to work.

The QuantLib page contains instructions for building QuantLib in Visual Studio 2010, http://quantlib.org/install/vc10.shtml but I need help creating the SWIG bindings.

Resolver Systems has pre-built C# bindings that work for me. http://www.resolversystems.com/products/quantlib-binary/ I was able to run QuantLib code in C# just fine with this package. My problem is that I need to add a small feature to the QuantLib code for use in my C# project. This is the reason I need to re-build QuantLib and re-create the SWIG bindings.

I know about QLNet, the C# port of QuantLib, http://sourceforge.net/projects/qlnet/, but this project is missing some pieces and I think that it is no longer being actively developed. Specifically, I need to be able to price options that pay discrete dividends. QLNet is missing some of the code for this. I tried porting the necessary code from QuantLib to QLNet, but my C++ must be rusty because I was getting incorrect output.

Note that the small feature I need to add to QuantLib is the ability to handle fractional days. I was able to add this feature to QLNet, and it is a small feature indeed. This tiny edit is delaying my project. I would greatly appreciate help on this issue.


Solution

  • There indeed seems to be a problem with the SWIG wrappers as distributed and .Net 4.0.

    I'm not working on that platform, so I can't speak based on personal experience. However, the issue was discussed recently on the QuantLib mailing list, and the solution contributed there by Mark Gillis was reported to work. You can read the relevant thread at http://thread.gmane.org/gmane.comp.finance.quantlib.user/8238. Hope this helps...