- Index
- » Programming
- » Modelica Language
- » trying to call function defined in...
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.
- sjoelund.se
- 1700 Posts
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.
- sjoelund.se
- 1700 Posts
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
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
- Index
- » Programming
- » Modelica Language
- » trying to call function defined in...