ITQuants blog

Sophis Risque: how to link with SphPortfolioGUI.dll?

Jul 10

Written by:
7/10/2014 8:47 AM  RssIcon

Some functions of the Sophis toolkit API are exported and stored in this dll. It contains at least the classes that permit to add contextual menus to the portfolio view, overloading the CFD deal input dialog, the portfolio headers (which correspond to the dialog in which the main sensitivities of the folio are displayed) and payment methods.

By the way, this dll implements some functionalities for the .Net toolkit, it means that it is linked to the MS C# .Net framework, loading other assemblies when it is loaded by dlls or executables linked to SphPortfolioGUI.lib.

I have the case where most of the dialog overloadings and Sophis toolkit calls are stored in one toolkit dll. Unfortunately, this dll exports a lot of functionalities that were used by night batches. It means that the night batches load the dll twice:

  • one using the static link, when MS starts the process
  • the second one, by the Sophis API, since it is declared as a toolkit/plugin dll and loaded dynamically when the CSRApi::Initialise method is called.

I wanted to add contextual menus to the porfolioview. It causes no problem when the toolkit dll is loaded by Sophis Risque, but does not work when night batches are launched, since it tries to load assemblies without initialilzing the .Net security as SphRisque does it (creating specific AppDomain for example). I got an access violation as below:

I will explain in this post how to resolve this problem.

The resolution is quite simple: we just need to add another toolkit dll that will be loaded either in the UNIVERSAL_MAIN of the first toolkit dll, if we don't want to change the configuration files, or by SphRisque by adding it in the DLL section of the configuration files. In this second dll, if we want to access to  other functionalities that are stored in the first dll, we have to load dynamically the first dll using the MS LoadLibrary method.

To resume, the architecture that is forbidden is the following one (red lines represent static links):

The following one is permitted (green lines represent dynamic links calling a factory callback for instance):

The load of the second dll without changing the configuration files can be done using this code:

 

    // add toolkit strong dependencies (the ones that cannot be configured in SQL)
#ifdef _NDEBUG
    const char* _dllNames[] = { "Toolkit2DotNet64d.dll" };
#else
    const char* _dllNames[] = { "Toolkit2DotNet64.dll" };
#endif
    for(int i=0;i<sizeof(_dllNames)/sizeof(const char*);i++)
    {
        try
        {
            CSRApi::gAPI->AddDll_API(_dllNames[i]);
        }
        catch(const ExceptionBase& ex)
        {
            //add some log there
        }
        catch(...)
        {
            //add some log there
        }
    }

Tags:
Categories: Sophis, C++

Search blog