I'm programming C# code that imports a C++ function similar to this:
//C++ code
extern "C" _declspec(dllexport) int foo(double *var1, double *var2){
double dubs[10];
RandomFunction(dubs);
*var1 = dubs[5];
*var2 = dubs[7];
return 0;
}
To import this function to C# I've tried 2 methods and both have resulted in a System.AccessViolationException. That means I'm trying to access protected memory...
//C# import code
//method 1
[DllImport(example.dll,CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern int foo(ref double var1, ref double var2);
//method 2
[DllImport(example.dll,CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern int foo(double *var1, double *var2);
//C# calling code
//method 1
public unsafe int call_foo(){
....Stuff
double var1 = 0;
double var2 = 0;
foo(ref var1, ref var2);
....More Stuff
}
//method 2
public unsafe int call_foo(){
....Stuff
double var1 = 0;
double var2 = 0;
double *v1 = &var1;
double *v2 = &var2;
foo(v1, v2);
....More Stuff
}
Up until now, I've thought it was a problem with my C# code, but would the C++ code's use of an array cause a problem? Am I missing something really obvious here?