日期:2014-05-17  浏览次数:20750 次

关于C# 调用C语言 DLL 的问题 !
 我现在有一个DLL需要我用C#将其引用。  函数原型是是这样的!

void __stdcall  QSCalc1(double f1,double f2,double f4,
int m,
double *F, double *tg, double *th,
double *tg0, double *th0,
double *tn0, double tw0, double Fz,
double Qz,
double *q,double *s);


我现在写了一个方法。  调用的时候没有问题,但是在返回的时候 总是报错! 
引发类型为“System.ExecutionEngineException”的异常。

QSCalc1(f1,f2,f4,m,ref F,ref tg, ref th,ref tg0,ref th0,ref tn0,Convert.ToDouble(tw0),Fz,Qz,ref q,ref s);

编译正常通过,但是在进行调用该函数的时候就报上面的错误。  不知道是哪出了问题, 还有,上面引用的方法中, ref q 和ref s 是DLL返回的两个数组。  我想把这两个数组中的数据读出来,应该怎么办呢?   (无法更改DLL文件)

------解决方案--------------------
ref q 和ref s 是DLL返回的两个数组,其它的double *参数也是数组吗?不是就需要改下.
最好说明下每个参数的含义

[DllImport("d:\\test\\Qscalculat.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
private static extern void QSCalc1(double f1, double f2, double f4, int m, ref double F, ref double tg, ref double th, ref double tg0, ref double th0, ref double tn0, double tw0, double Fz, double Qz, ref double []q, ref double []s);

------解决方案--------------------
double * Double[] 
[DllImport("", EntryPoint="")]
public static extern int Test(ref double a,ref double b);
//调用
Double[] a= new Double[3];
Double[] b= new Double[3];
Test(ref a,ref b);
------解决方案--------------------
声明加上 CallingConvention = CallingConvention.StdCall

[DllImport("d:\\test\\Qscalculat.dll", ExactSpelling=true, CharSet=CharSet.Ansi,CallingConvention = CallingConvention.StdCall, SetLastError=true)]