title: Simulink and DLLs
body: <p>I need to integrate a .EXE with simulink. I´m trying to do is create a C++ DLL in Simulink. This DLL should later be incorporated in a S-Function call in simulink.</p>
  
  <p>The flow of information would be:</p>
  
  <ol>
  <li>Simulink executes a S-function block</li>
  <li>This block calls a method from C++ DLL</li>
  <li>The C++ method executes a EXE, and return me a result</li>
  <li>The result goes all the way back to simulink</li>
  </ol>
  
  <p>ps: I dont have the source code of the EXE and it is a black box. That's why I am creating a c++ wrapper in order to execute it from simulink.</p>
  
  <p>So far, the C++ DLL wrapper is not working. The code is below.</p>
  
  <pre><code>#ifdef MATHFUNCSDLL_EXPORTS
  #define MATHFUNCSDLL_API __declspec(dllexport) 
  #else
  #define MATHFUNCSDLL_API __declspec(dllimport) 
  #endif
  
  #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
  extern "C" {
  #endif
  
  namespace MathFuncs
  {
      // This class is exported from the MathFuncsDll.dll
      class MyMathFuncs
      {
      public: 
          // Returns a + b
          static __declspec(dllexport)  double Add(double a, double b); 
      };
  }
  
  #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
  }
  #endif
  </code></pre>
  
  <p>*.cpp</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;stdexcept&gt;
  #include "MathFuncsDll.h"
  using namespace std;
  namespace MathFuncs
  {
      double MyMathFuncs::Add(double a, double b)
      {  return a + b;   }
  }
  </code></pre>
  
  <p>Please notice that I´m using (extern "c") command in order to generate a DLL that can be used in Matlab C style.</p>
  
  <p>When I try to load the DLL:</p>
  
  <ul>
  <li>loadlibrary('myDLL', 'myDLL.h')</li>
  <li>libfunctions MathFuncsDll</li>
  </ul>
  
  <p>"No methods for class lib.MathFuncsDll or no class lib.MathFuncsDll."</p>
  
  <p>So, my question is:</p>
  
  <ol>
  <li>Whats wrong with my dll project?</li>
  <li>Is it possible to integrate the exe with simulink using this approach?</li>
  </ol>
  
  <p>obs: <a href="http://www.mathworks.com/matlabcentral/answers/45337-connecting-matlab-to-c-language-dll" rel="nofollow">Similar question here</a></p>