External Functions: Alternative Method to use .dll from a C-script

467 views Asked by At

This is a companion question to External Functions: Reference headers in C-script to compiled dll.

That stack overflow question is using a Modelica external function call to a c-script. That c-script then uses c-functions contained within a .dll. Below is the initial preferred method I had attempted and a working attempt that I do not prefer.


Initial Attempt:

The following code would not work. My assumption was since I loaded the .dll Library libgsl in Modelica I could then simply use headers in the C-script to use function calls from that .dll. However, the dslog.txt file indicated that it couldn't recognize gsl_sf_bessel_j0. Apparently, the C-script doesn't know anything about the libgsl.dll that I specified in Modelica.

Modelica Function:

function chirp

  input Modelica.SIunits.AngularVelocity w_start;
  input Modelica.SIunits.AngularVelocity w_end;
  input Real A;
  input Real M;
  input Real t;
  output Real u "output signal";

  external "C" u=chirp(w_start,w_end,A,M,t)
    annotation(Library="libgsl", Include="#include \"chirp.c\"");
end chirp;

C-Script:

#include <gsl/gsl_sf_bessel.h>

double chirp2(double w1, double w2, double A, double M, double time)
{
  double res;
  double y;
  res=A*cos(w1*time+(w2-w1)*time*time/(2*M));
  y = gsl_sf_bessel_j0(res);
  return y;
}

Working Attempt:

In order to use a .dll in an external function call using a C-script I found it necessary to independently load the library using the LoadLibrary command.

Is this method really necessary? This is much more complicated that my initial attempt I hoped would work where I was thinking Modelica contained the necessary "know-how" to load the .dll.

Bonus: It appears there exists a way to send error messages back to Modelica. void ModelicaVFormatError(const char* string, va_list). Know of a reference example of its usage so I can replace the printf statements which don't seem to send anything back to Modelica?

Modelica Function:

function chirp

  input Modelica.SIunits.AngularVelocity w_start;
  input Modelica.SIunits.AngularVelocity w_end;
  input Real A;
  input Real M;
  input Real t;
  input String fileName= Modelica.Utilities.Files.loadResource("modelica://ExternalFuncTest/Resources/Library/libgsl.dll") "Full file name for GSL library";

  output Real u "output signal";

  external "C" u=chirp(w_start,w_end,A,M,t)
    annotation(Include="#include \"chirp.c\""); // <-- Removed Library reference
end chirp;

C-Script:

#include <windows.h>
#include <stdio.h>

typedef double (__cdecl *BESSEL)(const double); 
double chirp3(double w1, double w2, double A, double M, double time, const char* fileName)
{
    HINSTANCE hinstLib; 
    BESSEL bessel_J0; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

    // Get a handle to the DLL module.

    hinstLib = LoadLibrary(fileName); 

     // If the handle is valid, try to get the function address.

    double res;
    double y = 0;

    res=A*cos(w1*time+(w2-w1)*time*time/(2*M));

    if (hinstLib != NULL) 
    { 
        bessel_J0 = (BESSEL) GetProcAddress(hinstLib, "gsl_sf_bessel_j0"); 

        // If the function address is valid, call the function.

        if (NULL != bessel_J0) 
        {
            fRunTimeLinkSuccess = TRUE;
            printf("Success loading library method.\n"); //<-- Alternative to send message back to Modelica log file?
            y = bessel_J0(res);
        } else
        {
            printf("Failed to load library\n"); //<-- Alternative to send message back to Modelica log file?
        }
        // Free the DLL module.

        fFreeResult = FreeLibrary(hinstLib); 
    } 

    return y;
}
0

There are 0 answers