[Ipopt] Run-Time Check Failure #0 when importing c++ dll into my c# application
olmi.roberto at libero.it
olmi.roberto at libero.it
Thu Jan 8 09:19:33 EST 2015
Dear all,
as I posted on stackoverflow (http://stackoverflow.com/q/27839367/4218160) 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.
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.
Can anyone give me some help by answering to my post on stackoverflow?
Any help would be greatly appreciated!
Regards
Roberto
PS for you convenience I also copy my post on stack overflow:
In my c# code I need to call a c++ function (myWrapper) that is exported by a dll that I've created.
When myWrapper returns I get the following runtime error:
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.
As I will show below, I already specified the calling conventions a __cdecl.
In detail, my C# code:
class myClass
{
[DllImport("MyWrapper.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void myWrapper();
public void myMethod()
{
myWrapper();
}
}
c++ code for myWrapper:
#include "IpIpoptApplication.hpp"
extern "C" __declspec(dllexport) void (__cdecl myWrapper)()
{
SmartPtr<IpoptApplication> solver = IpoptApplicationFactory();
ApplicationReturnStatus status = solver->Initialize();
}
The IpoptAppliationFactory function is imported from an external dll in IpOptApplication.hpp (which is part of an open source project and can be viewed from https://projects.coin-or.org/svn/Ipopt/stable/3.11/Ipopt/src/Interfaces/IpIpoptApplication.hpp) with this line:
extern "C" __declspec(dllexport) class Ipopt::IpoptApplication * __cdecl IpoptApplicationFactory();
The strange thing is that the error happens only when "solver->Initialize()" in myWrapper is called. If I comment the call to this method myWrapper
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 (http://www.coin-or.org/projects/Ipopt.xml) used by thousands of programmers, 2) myWrapper works correctly if used in a standalone executable written in c++ code.
I've already googled for hours and I believe that the problem is in the way I call myWrapper but I can't find a solution.
Can anyone give me some suggestion? Thanks a lot.
Roberto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://list.coin-or.org/pipermail/ipopt/attachments/20150108/e7a23b63/attachment.html>
More information about the Ipopt
mailing list