<div></div>Dear all,<br>as I posted on stackoverflow (<a href="http://stackoverflow.com/q/27839367/4218160">http://stackoverflow.com/q/27839367/4218160</a>) I'm having serious problems in trying to get the results of an optimization problem into my c# application by importing a c++ dll that defines the problem and calls IpOpt precompiled dlls to run the solver.<br>The c++ dll that I'm trying to import into my C# application has been written my some consultants of my company, so I have access to the code but I if it's possible I prefer to not translate it int C#. I know that this would allow me to use IpOpt C# interface, but on the other hand our consultants would have difficulties in using C# under Visulal Studio.<br><br>Can anyone give me some help by answering to my post on stackoverflow?<br>Any help would be greatly appreciated!<br><br>Regards<br><br>Roberto<br><br><br><br>PS for you convenience I also copy my post on stack overflow:<br><br><br><br><div class="post-text" itemprop="text">

        <p>In my c# code I need to call a c++ function (<em>myWrapper</em>) that is exported by a dll that I've created.</p>

<p>When <em>myWrapper</em> returns I get the following runtime error:</p>

<blockquote>
  <p>Run-Time Check Failure #0 - The value of ESP was not properly saved
 across a function call.  This is usually a result of calling a function
 declared with one calling convention with a function pointer declared 
with a different calling convention.</p>
</blockquote>

<p>As I will show below, I already specified the calling conventions a __cdecl.</p>

<p>In detail, my C# code:</p>

<pre style="" class="default prettyprint prettyprinted"><code><span class="kwd">class</span><span class="pln"> myClass
</span><span class="pun">{</span><span class="pln">
    </span><span class="pun">[</span><span class="typ">DllImport</span><span class="pun">(</span><span class="str">"MyWrapper.dll"</span><span class="pun">,</span><span class="pln"> </span><span class="typ">CallingConvention</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="typ">CallingConvention</span><span class="pun">.</span><span class="typ">Cdecl</span><span class="pun">)]</span><span class="pln">
    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">extern</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> myWrapper</span><span class="pun">();</span><span class="pln">

    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> myMethod</span><span class="pun">()</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
        myWrapper</span><span class="pun">();</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code></pre>

<p>c++ code for <em>myWrapper</em>:</p>

<pre style="" class="default prettyprint prettyprinted"><code><span class="com">#include</span><span class="pln"> </span><span class="str">"IpIpoptApplication.hpp"</span><span class="pln">

</span><span class="kwd">extern</span><span class="pln"> </span><span class="str">"C"</span><span class="pln"> __declspec</span><span class="pun">(</span><span class="pln">dllexport</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="pun">(</span><span class="pln">__cdecl myWrapper</span><span class="pun">)()</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
    </span><span class="typ">SmartPtr</span><span class="pun">&lt;</span><span class="typ">IpoptApplication</span><span class="pun">&gt;</span><span class="pln"> solver </span><span class="pun">=</span><span class="pln"> </span><span class="typ">IpoptApplicationFactory</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">ApplicationReturnStatus</span><span class="pln"> status </span><span class="pun">=</span><span class="pln"> solver</span><span class="pun">-&gt;</span><span class="typ">Initialize</span><span class="pun">();</span><span class="pln">  
</span><span class="pun">}</span></code></pre>

<p>The <em>IpoptAppliationFactory</em> function is imported from an external dll in <em>IpOptApplication.hpp</em> (which is part of an open source project and can be viewed from <a href="https://projects.coin-or.org/svn/Ipopt/stable/3.11/Ipopt/src/Interfaces/IpIpoptApplication.hpp" rel="nofollow">https://projects.coin-or.org/svn/Ipopt/stable/3.11/Ipopt/src/Interfaces/IpIpoptApplication.hpp</a>) with this line:</p>

<pre style="" class="default prettyprint prettyprinted"><code><span class="kwd">extern</span><span class="pln"> </span><span class="str">"C"</span><span class="pln"> __declspec</span><span class="pun">(</span><span class="pln">dllexport</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Ipopt</span><span class="pun">::</span><span class="typ">IpoptApplication</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> __cdecl </span><span class="typ">IpoptApplicationFactory</span><span class="pun">();</span></code></pre>

<p>The strange thing is that the error happens only when <em>"solver-&gt;Initialize()"</em> in <em>myWrapper</em> is called. If I comment the call to this method <em>myWrapper</em>
 returns without errors.
The problem is not related to the definition of 
"Ipopt::IpoptApplication", nor in the implementation of 
IpoptApplicationFactory() or Initialize() because 1) they are from a 
well known open source project (<a href="http://www.coin-or.org/projects/Ipopt.xml" rel="nofollow">http://www.coin-or.org/projects/Ipopt.xml</a>) used by thousands of programmers, 2) myWrapper works correctly if used in a standalone executable written in c++ code.</p>

<p>I've already googled for hours and I believe that the problem is in the way I call <em>myWrapper</em> but I can't find a solution.</p>

<p>Can anyone give me some suggestion? Thanks a lot.</p>

<p>Roberto</p>

    </div><br>