I'm getting this error when doing a calculation. I'm quite confused since I essentially copied another method and just modified it to have the right parameters and name. I'm trying to call the THERM2dll which is a method from REFPROP (written in Fortran). I'm using a REFPROP C# wrapper to access the methods. How do I properly implement the method THERM2dll
(and others which will be added later) and eliminate these errors?
An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Clicking Continue Execution
provides this:
Managed Debugging Assistant 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x2970390f, on thread 0x322c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.'
I'm pretty lost. Here is the relevant code. As I said above, this code is nearly identical (other than the parameters) to other portions of the code (which works fine). The program worked fine until this code was added:
Form1.cs
IRefProp64.THERM2dll(ref tk, ref density, x, ref zz);
IRefProp64.cs
[DllImport(@"C:\Program Files (x86)\REFPROP\REFPRP64.dll", CharSet = CharSet.Ansi)]
public static extern void THERM2dll
(
ref double t, // temperature [K]
ref double Dl, // molar density [mol/L]
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] x, // composition [array of mol frac]
ref double zz // (OUTPUT) compressibility factor [-]
);
Note from the developers of the wrapper class for your information: IRefProp is an effective C# Interface for REFPROP. It’s an open source project (is/will be hosted on github) that contains a static wrapper class (IRefProp64) allowing access to the REFPROP native-code methods such as SETPATHdll(…), SETUPdll(…), SATSPLNdll(…),………etc.
User contributions licensed under CC BY-SA 3.0