Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

trying to call function defined in c++ file in model

trying to call function defined in c++ file in model

  I am new to openmodelica and trying to call external function using external keyword in modelica langugae.
I wonder if any one have done this before. I have tried to implement  example http://book.xogeny.com/behavior/functio … rpolation/
but failed to implement

Re: trying to call function defined in c++ file in model

If you are compiling using a C++ compiler, you will need to use

Code:

external "C" {

double myfunc(double d) {
  return d;
}
}

Or similar in your C++ code. If you don't, the function is not visible from the Modelica code since it only looks for C functions.

Re: trying to call function defined in c++ file in model

Hello hu72005

Welcome to the Forum!

Please detail us the problems you have had.

Best regards
Koldo

Re: trying to call function defined in c++ file in model

Thanks for your quick reply sjoelund.se , I am trying to use Visual c++ file I am able to access simple function from vc++ file but  having trouble  in calling COM dll methods.

Re: trying to call function defined in c++ file in model

Note that OpenModelica uses MinGW, which will give you trouble trying to use some Windows API's. As I am a Linux user myself I can not help you much further.

Re: trying to call function defined in c++ file in model

hu72005@gmail.com wrote:


Thanks for your quick reply sjoelund.se , I am trying to use Visual c++ file I am able to access simple function from vc++ file but  having trouble  in calling COM dll methods.

Hello hu72005

As sjoelund.se has said, it is easier to work with MinGW. There is a copy of it bundled with OpenModelica. It is very easy to use it but you can ask if you have problems using it.

You say that you have problems calling DLLs. Does it mean that you have a DLL and you want to call it from OpenModelica?. That would be easy too. Please confirm if this is your problem.

Best regards
Koldo

Edited by: Koldo - Dec-05-14 13:13:14

Re: trying to call function defined in c++ file in model

Yes Koldo, exactly.
I want to call dll function in openmodelica

Re: trying to call function defined in c++ file in model

Hello hu72005

If you know how to run your external C code from OpenModelica, you just need to create a .C file with a function like this:

#include <windows.h>

static HINSTANCE hinstLib = 0;
static double (* MyFunctionPointer)(double) = 0;

double MyFunction(double arg) {
    if (hinstLib == 0)
        hinstLib = LoadLibraryEx(TEXT("MyDLLFullFilePath"), NULL, LOAD_IGNORE_CODE_AUTHZ_LEVEL); // Loads the DLL
    if (MyFunctionPointer == 0)
        MyFunctionPointer = GetProcAddress(hinstLib, "MyFunctionName");  // Loads the function pointer
    return (*MyFunctionPointer)(arg);  // Calls it
}

This is in Windows. In Linux this is also possible to be done as easily calling dynamic libraries.

Best regards
Koldo

Re: trying to call function defined in c++ file in model

Hello hu72005

If you know how to run your external C code from OpenModelica, you just need to create a .C file with a function like this:

#include <windows.h>

static HINSTANCE hinstLib = 0;
static double (* MyFunctionPointer)(double) = 0;

double MyFunction(double arg) {
    if (hinstLib == 0)
        hinstLib = LoadLibraryEx(TEXT("MyDLLFullFilePath"), NULL, LOAD_IGNORE_CODE_AUTHZ_LEVEL); // Loads the DLL
    if (MyFunctionPointer == 0)
        MyFunctionPointer = GetProcAddress(hinstLib, "MyFunctionName");  // Loads the function pointer
    return (*MyFunctionPointer)(arg);  // Calls it
}

This is in Windows. In Linux this is also possible to be done as easily calling dynamic libraries.

Best regards
Koldo

There are 0 guests and 0 other users also viewing this topic
You are here: