TypeInitializationException with QuantLib-SWIG 1.6 x64 for C#

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

TypeInitializationException with QuantLib-SWIG 1.6 x64 for C#

grantathon
This post was updated on .
Hello,

I recently setup QuantLib-SWIG 1.6 for C# using the Visual Studio 2012 compiler (vc120), and I'm very happy that I can finally use QuantLib 1.6 in our Visual Studio 2013 C# solutions.  I'm able to run the BermudanSwaption and EquityOption examples under the Win32 platform in Debug and Release mode, but for the x64 platform I'm only able to run in Release mode.

The thing is, I need it to be able to run in Debug mode when using the x64 platform.  Unfortunately, when running x64/Debug with debugging (F5) I get the following TypeInitializationException

The type initializer for 'QuantLib.NQuantLibcPINVOKE' threw an exception.

and under the same settings without debugging (Ctrl+F5) I get

Unhandled Exception: System.TypeInitializationException: The type initializer for 'QuantLib.NQuantLibcPINVOKE' threw an
exception. ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception. -
--> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRES
ULT: 0x8007000B)
   at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_NQuantLibc(ExceptionDelegate applica
tionDelegate, ExceptionDelegate arithmeticDelegate, ExceptionDelegate divideByZeroDelegate, ExceptionDelegate indexOutOf
RangeDelegate, ExceptionDelegate invalidCastDelegate, ExceptionDelegate invalidOperationDelegate, ExceptionDelegate ioDe
legate, ExceptionDelegate nullReferenceDelegate, ExceptionDelegate outOfMemoryDelegate, ExceptionDelegate overflowDelega
te, ExceptionDelegate systemExceptionDelegate)
   at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..cctor() in c:\Program Files\QuantLib\QuantLib-SWIG-1.6\CSharp\csha
rp\NQuantLibcPINVOKE.cs:line 105
   --- End of inner exception stack trace ---
   at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..ctor()
   at QuantLib.NQuantLibcPINVOKE..cctor() in c:\Program Files\QuantLib\QuantLib-SWIG-1.6\CSharp\csharp\NQuantLibcPINVOKE
.cs:line 125
   --- End of inner exception stack trace ---
   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:\Program Files\QuantLib\QuantLib-SWIG-1.6\CSharp\csharp\Date.c
s:line 102
   at BermudanSwaption.Run.Main(String[] args) in c:\Program Files\QuantLib\QuantLib-SWIG-1.6\CSharp\examples\BermudanSw
aption.cs:line 72

I have read other solutions to this problem, like checking to see if the proper Library Dependencies are set and setting the platform properly, but these solutions didn't help as I have already implemented them.  I even rebuilt Boost and QuantLib 1.6 under the x64/Debug configurations, but this still didn't help.

Is there anyone that could help me out with this issue?  This is rather critical for me and I can't seem to figure it out.  I should be able to give you any other information you request.

Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Using QuantLib-SWIG 1.6 x64 for C# in Debug mode

grantathon
Hi Luigi,

I've recently figured out how to build and run QuantLib-SWIG x64 under Release and Debug mode for C# using the Visual Studio C++ 2013 (vc120) compiler.  I realized that not only do the include and library directory paths must be correct for both Boost and QuantLib, but those two applications must also have their output DLLs placed in appropriate directories.  After I better reorganized these locations to separate x64/x86 for Release and Debub mode, I was successful in running all versions in VS 2013.  Honestly, I'm not 100% sure this was the final push to provide me with a valid build, but after repeating the process laid out below I have been able to run it fine.  Below are the work instructions I followed.

  1. Download Boost, QuantLib, and QuantLib-SWIG
  2. Build Boost in 64-bit through the command line (see http://www.boost.org/doc/libs/1_58_0/more/getting_started/windows.html)
    1. Also, see first result from http://stackoverflow.com/questions/2322255/64-bit-version-of-boost-for-64-bit-windows, and replace --toolset=msvc-9.0 with --toolset=msvc-12.0 (or appropriate msvc version)
  3. Open a QuantLib solution
  4. In Property Manager->QuantLib, add main boost directory (e.g., C:\boost\boost_1_58_0) to VC++ Directories->Include Directories and add boost library directory (e.g., C:\boost\boost_1_58_0\lib\<platform>\lib, where platform is either Win32 or x64)  to VC++ Directories->Library Directories
    1. In regards to the platform option for the library path, the appropriate path needs to be set for all Microsoft.Cpp.Win32.user and Microsoft.Cpp.x64.user settings
  5. Open QuantLib->Properties, and under Configuration Properties->General with Configuration set to "All Configurations" and Platform set to "All Platforms" set Output Directory to ".\lib\$(Platform)\" and Intermediate Directory to ".\build\$(qlCompilerTag)\$(Platform)\$(Configuration)\", if not already done so
  6. Set the Active Solution Platform in the Configuration Manager to x64
  7. Build the QuantLib project under both Release and Debug mode
  8. Add to your system environmental variables QL_DIR and set it to the QuantLib installation directory (e.g., C:\QuantLib-1.6)
  9. Under QuantLib-SWIG C# installation directory, run swig.cmd
  10. Open a QuantLib-SWIG C# solution
  11. In Property Manager->NQuantLibc, add QuantLib library directory (e.g., C:\QuantLib-1.6\lib\<platform>", where platform is either Win32 or x64)  to Linker->Additional Library Directories
  12. Open NQuantLibc->Properties, and under Configuration Properties->General with Configuration set to "All Configurations" and Platform set to "All Platforms" set Output Directory to "bin\$(qlCompilerTag)\$(Platform)\$(Configuration)\" and Intermediate Directory to "build\$(qlCompilerTag)\$(Platform)\$(Configuration)\", if not already done so
  13. Open NQuantLib->Properties, and under Build->Output with Configuration set to "Release" and Platform set to "x64" set Output Path to "bin\x64\Release\", if not already done so, and, if you want, repeat for all setting combinations of Configuration and Platform
  14. Set the Active Solution Platform in the Configuration Manager to x64
  15. Build the NQuantLib project under both Release and Debug mode

To test whether or not this process produced for you valid 64-bit DLLs, run the BermudanSwaption and EquityOption projects provided in the QuantLib-SWIG solution.  Before doing this, be sure that the NQuantLibc and NQuantLib DLLs are correctly referenced in Properties->Build Events->Pre-Build Event Command Line.  Mine look like

copy "$(SolutionDir)cpp\bin\$(qlCompilerTag)\$(Platform)\$(ConfigurationName)\NQuantLibc.dll" "$(TargetDir)"
copy "$(SolutionDir)csharp\bin\$(Platform)\$(ConfigurationName)\NQuantLib.dll" "$(TargetDir)"

Maybe you could attempt this as well and see if you achieve the same results.  Also, there be some settings that I forgot to mention that might lead to unsuccessful builds.  Either way, I will post a more descriptive answer to the thread I created in quantlib-users.

Best,

Grant

On Fri, Jul 17, 2015 at 11:27 AM, Luigi Ballabio <[hidden email]> wrote:
Hello,
   I'm afraid I can't help here, as I have little experience with the Windows builds apart from trying them on a virtual machine before release.
In fact, you got further than I did: I didn't get x86 to work in Release either.  Did you have to change anything to make it work, or did it work for you out of the box?

Thanks,
    Luigi



On Thu, Jul 16, 2015 at 5:54 PM grantathon <[hidden email]> wrote:
Hello,

I recently setup QuantLib-SWIG 1.6 for C# using the Visual Studio 2012
compiler (vc120), and I'm very happy that I can finally use QuantLib 1.6 in
our Visual Studio 2013 C# solutions.  I'm able to run the BermudanSwaption
and EquityOption examples under the Win32 platform in Debug and Release
mode, but for the x64 platform I'm only able to run in Release mode.

The thing is, I need it to be able to run in Debug mode when using the x64
platform.  Unfortunately, when running x64/Debug with debugging (F5) I get
the following TypeInitializationException

The type initializer for 'QuantLib.NQuantLibcPINVOKE' threw an exception.

and under the same settings without debugging (Ctrl+F5) I get

Unhandled Exception: System.TypeInitializationException: The type
initializer for 'QuantLib.NQuantLibcPINVOKE' threw an
exception. ---> System.TypeInitializationException: The type initializer for
'SWIGExceptionHelper' threw an exception. -
--> System.BadImageFormatException: An attempt was made to load a program
with an incorrect format. (Exception from HRES
ULT: 0x8007000B)
   at
QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_NQuantLibc(ExceptionDelegate
applica
tionDelegate, ExceptionDelegate arithmeticDelegate, ExceptionDelegate
divideByZeroDelegate, ExceptionDelegate indexOutOf
RangeDelegate, ExceptionDelegate invalidCastDelegate, ExceptionDelegate
invalidOperationDelegate, ExceptionDelegate ioDe
legate, ExceptionDelegate nullReferenceDelegate, ExceptionDelegate
outOfMemoryDelegate, ExceptionDelegate overflowDelega
te, ExceptionDelegate systemExceptionDelegate)
   at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..cctor() in c:\Program
Files\QuantLib\QuantLib-SWIG-1.6\CSharp\csha
rp\NQuantLibcPINVOKE.cs:line 105
   --- End of inner exception stack trace ---
   at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..ctor()
   at QuantLib.NQuantLibcPINVOKE..cctor() in c:\Program
Files\QuantLib\QuantLib-SWIG-1.6\CSharp\csharp\NQuantLibcPINVOKE
.cs:line 125
   --- End of inner exception stack trace ---
   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:\Program
Files\QuantLib\QuantLib-SWIG-1.6\CSharp\csharp\Date.c
s:line 102
   at BermudanSwaption.Run.Main(String[] args) in c:\Program
Files\QuantLib\QuantLib-SWIG-1.6\CSharp\examples\BermudanSw
aption.cs:line 72

I have read other solutions to this problem, like checking to see if the
proper Library Dependencies are set and setting the platform properly, but
these solutions didn't help as I have already implemented them.  I even
rebuilt Boost and QuantLib 1.6 under the x64/Debug configurations, but this
still didn't help.

Is there anyone that could help me out with this issue?  This is rather
critical for me and I can't seem to figure it out.  I should be able to give
you any other information you request.

Thanks in advance.



--
View this message in context: http://quantlib.10058.n7.nabble.com/Using-QuantLib-SWIG-1-6-x64-for-C-in-Debug-mode-tp16732.html
Sent from the quantlib-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
--


------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Using QuantLib-SWIG 1.6 x64 for C# in Debug mode

Luigi Ballabio

Ok, thanks. Most of the steps seem to be those that we already describe in the installation guide. I'll try the others next week when I get back to work.

Luigi


On 16:51, Tue, Jul 21, 2015 Grant Bartel <[hidden email]> wrote:
Hi Luigi,

I've recently figured out how to build and run QuantLib-SWIG x64 under Release and Debug mode for C# using the Visual Studio C++ 2013 (vc120) compiler.  I realized that not only do the include and library directory paths must be correct for both Boost and QuantLib, but those two applications must also have their output DLLs placed in appropriate directories.  After I better reorganized these locations to separate x64/x86 for Release and Debub mode, I was successful in running all versions in VS 2013.  Honestly, I'm not 100% sure this was the final push to provide me with a valid build, but after repeating the process laid out below I have been able to run it fine.  Below are the work instructions I followed.

  1. Download Boost, QuantLib, and QuantLib-SWIG
  2. Build Boost in 64-bit through the command line (see http://www.boost.org/doc/libs/1_58_0/more/getting_started/windows.html)
    1. Also, see first result from http://stackoverflow.com/questions/2322255/64-bit-version-of-boost-for-64-bit-windows, and replace --toolset=msvc-9.0 with --toolset=msvc-12.0 (or appropriate msvc version)
  3. Open a QuantLib solution
  4. In Property Manager->QuantLib, add main boost directory (e.g., C:\boost\boost_1_58_0) to VC++ Directories->Include Directories and add boost library directory (e.g., C:\boost\boost_1_58_0\lib\<platform>\lib, where platform is either Win32 or x64)  to VC++ Directories->Library Directories
    1. In regards to the platform option for the library path, the appropriate path needs to be set for all Microsoft.Cpp.Win32.user and Microsoft.Cpp.x64.user settings
  5. Open QuantLib->Properties, and under Configuration Properties->General with Configuration set to "All Configurations" and Platform set to "All Platforms" set Output Directory to ".\lib\$(Platform)\" and Intermediate Directory to ".\build\$(qlCompilerTag)\$(Platform)\$(Configuration)\", if not already done so
  6. Set the Active Solution Platform in the Configuration Manager to x64
  7. Build the QuantLib project under both Release and Debug mode
  8. Add to your system environmental variables QL_DIR and set it to the QuantLib installation directory (e.g., C:\QuantLib-1.6)
  9. Under QuantLib-SWIG C# installation directory, run swig.cmd
  10. Open a QuantLib-SWIG C# solution
  11. In Property Manager->NQuantLibc, add QuantLib library directory (e.g., C:\QuantLib-1.6\lib\<platform>", where platform is either Win32 or x64)  to Linker->Additional Library Directories
  12. Open NQuantLibc->Properties, and under Configuration Properties->General with Configuration set to "All Configurations" and Platform set to "All Platforms" set Output Directory to "bin\$(qlCompilerTag)\$(Platform)\$(Configuration)\" and Intermediate Directory to "build\$(qlCompilerTag)\$(Platform)\$(Configuration)\", if not already done so
  13. Open NQuantLib->Properties, and under Build->Output with Configuration set to "Release" and Platform set to "x64" set Output Path to "bin\x64\Release\", if not already done so, and, if you want, repeat for all setting combinations of Configuration and Platform
  14. Set the Active Solution Platform in the Configuration Manager to x64
  15. Build the NQuantLib project under both Release and Debug mode

To test whether or not this process produced for you valid 64-bit DLLs, run the BermudanSwaption and EquityOption projects provided in the QuantLib-SWIG solution.  Before doing this, be sure that the NQuantLibc and NQuantLib DLLs are correctly referenced in Properties->Build Events->Pre-Build Event Command Line.  Mine look like

copy "$(SolutionDir)cpp\bin\$(qlCompilerTag)\$(Platform)\$(ConfigurationName)\NQuantLibc.dll" "$(TargetDir)"
copy "$(SolutionDir)csharp\bin\$(Platform)\$(ConfigurationName)\NQuantLib.dll" "$(TargetDir)"

Maybe you could attempt this as well and see if you achieve the same results.  Also, there be some settings that I forgot to mention that might lead to unsuccessful builds.  Either way, I will post a more descriptive answer to the thread I created in quantlib-users.

Best,

Grant

On Fri, Jul 17, 2015 at 11:27 AM, Luigi Ballabio <[hidden email]> wrote:
Hello,
   I'm afraid I can't help here, as I have little experience with the Windows builds apart from trying them on a virtual machine before release.
In fact, you got further than I did: I didn't get x86 to work in Release either.  Did you have to change anything to make it work, or did it work for you out of the box?

Thanks,
    Luigi



On Thu, Jul 16, 2015 at 5:54 PM grantathon <[hidden email]> wrote:
Hello,

I recently setup QuantLib-SWIG 1.6 for C# using the Visual Studio 2012
compiler (vc120), and I'm very happy that I can finally use QuantLib 1.6 in
our Visual Studio 2013 C# solutions.  I'm able to run the BermudanSwaption
and EquityOption examples under the Win32 platform in Debug and Release
mode, but for the x64 platform I'm only able to run in Release mode.

The thing is, I need it to be able to run in Debug mode when using the x64
platform.  Unfortunately, when running x64/Debug with debugging (F5) I get
the following TypeInitializationException

The type initializer for 'QuantLib.NQuantLibcPINVOKE' threw an exception.

and under the same settings without debugging (Ctrl+F5) I get

Unhandled Exception: System.TypeInitializationException: The type
initializer for 'QuantLib.NQuantLibcPINVOKE' threw an
exception. ---> System.TypeInitializationException: The type initializer for
'SWIGExceptionHelper' threw an exception. -
--> System.BadImageFormatException: An attempt was made to load a program
with an incorrect format. (Exception from HRES
ULT: 0x8007000B)
   at
QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_NQuantLibc(ExceptionDelegate
applica
tionDelegate, ExceptionDelegate arithmeticDelegate, ExceptionDelegate
divideByZeroDelegate, ExceptionDelegate indexOutOf
RangeDelegate, ExceptionDelegate invalidCastDelegate, ExceptionDelegate
invalidOperationDelegate, ExceptionDelegate ioDe
legate, ExceptionDelegate nullReferenceDelegate, ExceptionDelegate
outOfMemoryDelegate, ExceptionDelegate overflowDelega
te, ExceptionDelegate systemExceptionDelegate)
   at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..cctor() in c:\Program
Files\QuantLib\QuantLib-SWIG-1.6\CSharp\csha
rp\NQuantLibcPINVOKE.cs:line 105
   --- End of inner exception stack trace ---
   at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..ctor()
   at QuantLib.NQuantLibcPINVOKE..cctor() in c:\Program
Files\QuantLib\QuantLib-SWIG-1.6\CSharp\csharp\NQuantLibcPINVOKE
.cs:line 125
   --- End of inner exception stack trace ---
   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:\Program
Files\QuantLib\QuantLib-SWIG-1.6\CSharp\csharp\Date.c
s:line 102
   at BermudanSwaption.Run.Main(String[] args) in c:\Program
Files\QuantLib\QuantLib-SWIG-1.6\CSharp\examples\BermudanSw
aption.cs:line 72

I have read other solutions to this problem, like checking to see if the
proper Library Dependencies are set and setting the platform properly, but
these solutions didn't help as I have already implemented them.  I even
rebuilt Boost and QuantLib 1.6 under the x64/Debug configurations, but this
still didn't help.

Is there anyone that could help me out with this issue?  This is rather
critical for me and I can't seem to figure it out.  I should be able to give
you any other information you request.

Thanks in advance.



--
View this message in context: http://quantlib.10058.n7.nabble.com/Using-QuantLib-SWIG-1-6-x64-for-C-in-Debug-mode-tp16732.html
Sent from the quantlib-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
--

--

------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Using QuantLib-SWIG 1.6 x64 for C# in Debug mode

grantathon
For those interested in using QuantLib 1.6 in C# projects running in 64-bit mode, there is a newly available NuGet package I pushed named NQuantLib64.  This package was built following the steps detailed in my previous post, and no modifications were made to the original QuantLib library prior to building.

Enjoy!