在未知模块中发生了类型为'System.AccessViolationException'的未处理异常

时间:2019-09-19 15:22:15

标签: c# visual-studio exception dll dllimport

计算时出现此错误。我很困惑,因为我本质上是复制了另一种方法并只是对其进行了修改以使其具有正确的参数和名称。我正在尝试调用THERM2dll,它是REFPROP(用Fortran编写)中的一种方法。我正在使用REFPROP C# wrapper来访问方法。如何正确实现方法THERM2dll(以及稍后将添加的其他方法)并消除这些错误?

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.

点击Continue Execution会提供以下信息:

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.'

我很迷路。这是相关的代码。正如我上面所说的,此代码与代码的其他部分(工作正常)几乎相同(参数除外)。在添加以下代码之前,该程序运行良好:

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 [-]
        );

包装类开发人员的说明,供您参考: IRefProp是REFPROP的有效C#接口。这是一个开源项目(将/将在github上托管),其中包含一个静态包装器类(IRefProp64),该类可访问REFPROP本机代码方法,例如SETPATHdll(…),SETUPdll(…),SATSPLNdll(…)等。 ……等等

0 个答案:

没有答案