I have a function written in c++ that I put in a dll and use in c# using DllImport. Everything works; I am able to get the return value from c++ and display it in my c# GUI. Now I want to add to that function and have it return multiple values (3 so far). I've tried the methods given in Return C++ array to C# and How to return two different variable in c++? but neither work. The code from the first post gives me an access violation error and the code form the second one gives me all 0 values for the struct. For the first one, I even copied the given code exactly and tried to run it but to no avail. What could be causing the errors and wrong values given by these methods? How can I get them to work?
Just in case it is needed, my own code with the implementation of the second post is below.
bisection.h
struct Result
{
double root;
double relError;
double absError;
}result;
extern "C" {__declspec(dllexport) Result bisection( double l, double u, double stoppingError, int maxIter); }
bisection.cpp
Result bisection(double l, double u, double stoppingError, int maxIter) {
//code for this function
result.root = xr;
result.relError = e;
result.absError = 1;
return result;
}
c# code
[StructLayout(LayoutKind.Sequential)]
public struct Result
{
public double root;
public double relError;
public double absError;
}
[DllImport(dllPath)]
private static extern Result bisection(double l, double u, double stoppingError, int maxIter);
Result result = bisection(data[0], data[1], 0.1, 100);