For training purposes, I am trying to load a DLL in MATLAB, which calls functions defined in another DLL. I am new to all of this and have not yet been able to understand how I will deal with this, and I have not been able to find any relevant resources.
I wrote a small DLL in C ++ that looks something like this:
//example_dll.h
and source file:
//example_dll.cpp
I created this using MinGW w64 and loaded into matlab with loadlibrary('example_dll') without any problems.
Now I want to define a function
int Double(int x) { return 2 * x; }
In another DLL (let her name DLL2) and call this function from my example_dll. What would be the easiest way to do this?
I would appreciate a short sample code (preferably for dynamic linking at runtime and without using module definition files (.def)) or a link to the corresponding resource on the networks. Thanks!
SOLUTION FOR A SIMPLE EXAMPLE:
I think I got a solution. It seems to work.
I created a DLL called interface_DLL , which I loaded into MATLAB, and from which I named my function in example_dll
there he is:
//interface_dll.h
and source file:
//interface_dll.cpp
I load it from MATLAB as follows:
%loadDLL.m path = 'C:\Path\to\DLL\'; addpath(path); loadlibrary('interface_dll') i = 2; x = calllib('interface_dll', 'Quadruple', i)
The reason I'm going through this process is because the MATLAB shared library interface only supports C library routines, not C ++ classes.
My idea of a workaround is to use an intermediate DLL to work as an interface between MATLAB and the DLL that I am going to access. Is there a better way to do this?
FURTHER QUESTIONS:
Can anyone explain the value of the string typedef int (__stdcall * pICFUNC)(int); How is it applicable here? What do I need to add or what changes would I have to make if I wanted to call a function in a class in example_dll ?
EDIT: I added the following code to the example_dll header file:
class EXAMPLE_DLL MyClass { public: int add2(int); }; #ifdef __cplusplus extern "C" { #endif MyClass EXAMPLE_DLL *createInstance(){ return new MyClass(); } void EXAMPLE_DLL destroyInstance(MyClass *ptrMyClass){ delete ptrMyClass; } #ifdef __cplusplus } #endif