title: Load a C++ DLL into matlab which calls functions in another DLL
body: <p>For learning purposes, I'm trying to load a DLL into MATLAB which calls functions defined in another DLL. I'm new to all of this, and haven't yet been able to figure out how I would go about doing this, and nor have I managed to find any relevant resources.</p>
  
  <p>I wrote a small DLL in C++ which goes something like this:</p>
  
  <pre><code>//example_dll.h
  #ifndef EXAMPLE_DLL_H
  #define EXAMPLE_DLL_H
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #ifdef BUILDING_EXAMPLE_DLL
  #define EXAMPLE_DLL __declspec(dllexport)
  #else
  #define EXAMPLE_DLL __declspec(dllimport)
  #endif
  
  int EXAMPLE_DLL Double(int x);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif  // EXAMPLE_DLL_H
  </code></pre>
  
  <p>and the source file:</p>
  
  <pre><code>//example_dll.cpp
  #include &lt;stdio.h&gt;
  #include "example_dll.h"
  
  int Double(int x)
  {
          return 2 * x;
  }
  </code></pre>
  
  <p>This I built using MinGW w64 and loaded into matlab using <code>loadlibrary('example_dll')</code> without any problems.</p>
  
  <p>I now want to define the function</p>
  
  <pre><code>int Double(int x)
  {
          return 2 * x;
  }
  </code></pre>
  
  <p>In another DLL, (let's call it DLL2) and to call that function from my example_dll.
  What would be the easiest way to do it?</p>
  
  <p>I would appreciate a short example code (preferably for run-time dynamic linking, and without the use of module definition (.def) files) or a link to a relevant resource on the interwebs.
  Thanks!</p>
  
  <p><strong>SOLUTION TO SIMPLE EXAMPLE:</strong></p>
  
  <p>I think I got the solution. It seems to be working anyway.</p>
  
  <p>I created a DLL named <code>interface_DLL</code> which I loaded into MATLAB and from which I called my function in <code>example_dll</code></p>
  
  <p>here it is:</p>
  
  <pre><code>//interface_dll.h
  #ifndef INTERFACE_DLL_H
  #define INTERFACE_DLL_H
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #ifdef BUILDING_INTERFACE_DLL
  #define INTERFACE_DLL __declspec(dllexport)
  #else
  #define INTERFACE_DLL __declspec(dllimport)
  #endif
  
  
  int INTERFACE_DLL Quadruple(int x);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif  // INTERFACE_DLL_H
  </code></pre>
  
  <p>and the source file:</p>
  
  <pre><code>//interface_dll.cpp
  #include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include "interface_dll.h"
  #include "example_dll.h"
  
  int Quadruple(int x)
  {
      /* get handle to dll */ 
      HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\Users\\uidr0605\\Documents\\ExampleDLL\\example_dll.dll"); 
  
      /* get pointer to the function in the dll*/
      FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"Double");
  
      /*
      Define the Function in the DLL for reuse. This is just prototyping the dll's function.
      A mock of it. Use "stdcall" for maximum compatibility.
      */
      typedef int (__stdcall * pICFUNC)(int);
  
      pICFUNC Double;
      Double = pICFUNC(lpfnGetProcessID);
  
      /* The actual call to the function contained in the dll */
      int intMyReturnVal = Double(x);
      intMyReturnVal = Double(intMyReturnVal);
  
      /* Release the Dll */
      FreeLibrary(hGetProcIDDLL);
  
      /* The return val from the dll */
      return intMyReturnVal; 
  }
  </code></pre>
  
  <p>I load it from MATLAB as follows:</p>
  
  <pre><code>%loadDLL.m
  path = 'C:\Path\to\DLL\';
  addpath(path);
  loadlibrary('interface_dll')
  i = 2;
  x = calllib('interface_dll', 'Quadruple', i)
  </code></pre>
  
  <p>The reason I'm going through this process is that <a href="http://www.mathworks.com/help/matlab/matlab_external/limitations-to-shared-library-support.html" rel="nofollow">the MATLAB shared library interface supports C library routines only and not C++ classes.</a></p>
  
  <p>My idea of a workaround is to use an intermediate DLL to act as an interface between MATLAB and the DLL who's classes I intend to access.
  Is there a better way of doing this?</p>
  
  <p><strong>FURTHER QUESTIONS:</strong></p>
  
  <p>Can anyone please explain the significance of the line <code>typedef int (__stdcall * pICFUNC)(int);</code> as applied here?
  What would I have to add or what changes would I have to make if I wanted to call a function in a class in <code>example_dll</code>?</p>
  
  <p>EDIT: I added the following code to the example_dll header file:</p>
  
  <pre><code>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
  </code></pre>