已回答 使用dll的FatalExecutionEngineError

Maylar

新成员
已加入
2021年1月22日
留言内容
4
编程经验
10+
我有一个要在C#应用程序中使用的第三方dll。我有C ++中的示例代码,包括函数声明,并且创建了一个控制台应用程序,并用(我认为)等效的数据类型和我自己的printf()进行了修改。当我运行该应用程序时,它会在第一个函数调用时出现致命异常而窒息,

发生FatalExecutionEngineError
由于本机框架位于调用堆栈的顶部,因此无法评估表达式。

该功能的原始原型是这个

函数声明C ++:
void __cdecl C_lin_coeff_batch(double z[], int32_t z_fpp, double theta[],
                               int32_t C_in[], bool inverse, int32_t C_fpp,
                               int32_t C_int_fpp, uint16_t method, int32_t
                               ci_mean_sel, double cc[], uint32_t cc_fpp[],
                               int32_t ccx[], double cci[], double best_fit[],
                               uint32_t cn_div[], uint32_t cn_shift[],
                               uint32_t n_samples, int32_t n_sample_x_devices,
                               int32_t n_cc, int32_t n_cn_div, int32_t n_cn_shift);

我翻译成这个

C# Dllimport:
[DllImport("linearize_r01.dll")]
        public static extern void c_lin_coeff_dut(Int32[] C_in, double[] z, double[] theta,
        Int32 C_fpp, Int32 C_int_fpp, Int32 z_fpp, int inverse,
        ref double[] cc, ref UInt32[] cc_fpp, ref Int32[] ccx, ref double[] cci,
        ref double[] error_vs_z, ref UInt32[] cn_div, ref Int32[] cn_shift,
        Int32 n_samples, Int32 n_cc, Int32 n_cn_div, Int32 n_cn_shift);

谁能给我指导以使它起作用?

谢谢

戴夫
 
Solution
I assume there is a reason why some of the arrays are declared as ref while others are not.

Anyway, I would first start off by trying to match the calling convention. The DllImport attribute takes a calling convention. You probably should pass in the fact that the function is a C calling convention instead of the StdCall or WinApi calling convention that C# normally expects.

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,500
地点
弗吉尼亚州切萨皮克
编程经验
10+
Are you sure you posted the right functions? The C function looks to be named C_lin_coeff_batch, but the P/Invoke declaration you posted is for c_lin_coeff_dut. Furthermore, the C function's parameters starts off with an array of doubles, but your P/Invoke starts off with an array of Int32s.
 

Maylar

新成员
已加入
2021年1月22日
留言内容
4
编程经验
10+
Are you sure you posted the right functions? The C function looks to be named C_lin_coeff_batch, but the P/Invoke declaration you posted is for c_lin_coeff_dut. Furthermore, the C function's parameters starts off with an array of doubles, but your P/Invoke starts off with an array of Int32s.
糟糕,我的糟糕。我从C ++标头复制了错误的声明。这是我尝试复制的原始原型:

c_lin_coeff_dut,C ++版本:
void __cdecl c_lin_coeff_dut(int32_t C_in[], double z[], double theta[],
        int32_t C_fpp, int32_t C_int_fpp, int32_t z_fpp, int inverse,
        double cc[], uint32_t cc_fpp[], int32_t ccx[], double cci[],
        double error_vs_z[], uint32_t cn_div[], int32_t cn_shift[],
        int32_t n_samples, int32_t n_cc, int32_t n_cn_div, int32_t n_cn_shift);
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,500
地点
弗吉尼亚州切萨皮克
编程经验
10+
I assume there is a reason why some of the arrays are declared as ref while others are not.

Anyway, I would first start off by trying to match the calling convention. The DllImport attribute takes a calling convention. You probably should pass in the fact that the function is a C calling convention instead of the StdCall or WinApi calling convention that C# normally expects.
 

Maylar

新成员
已加入
2021年1月22日
留言内容
4
编程经验
10+
非常感谢你。我添加了调用约定CallingConvention.Cdecl并消除了ref关键字,并且代码不再崩溃。我假设任何输出参数都应按照C#约定声明为ref或out。我想无论如何数组都是通过引用传递的,所以没关系。

再次感谢。

戴夫
 
最佳 底部