当前位置:编程学习 > C#/ASP.NET >>

C#调用C++dll时运算出错

一个项目是用C++写的,我将其编译成dll供.net4.0的C#项目调用,但是计算结果会有较大误差(或者说错误)诸如1*3.14159265857,结果会的3.1415927015。 --------------------编程问答-------------------- 谁知道你dll怎么工作的 --------------------编程问答-------------------- dll中所有的浮点数运算都出错,
C#中是通过静态引用动态链接库实现的,如下
        [DllImport("KinematicService.dll")]
        public static extern byte SetRobotJoints(double[] jointsdegree); --------------------编程问答-------------------- --------------------编程问答-------------------- 求高人指点…… --------------------编程问答-------------------- 把你的 c++ 中函数定义写出来
C# 中的引用和调用过程写出来 --------------------编程问答-------------------- dll的C++源码:
#define DllAPI extern "C" __declspec(dllexport)
using namespace  std;
DllAPI bool GP_InverseK(double position[],double closeJoints[], double &E1,double &A1,double &A2,double &A3,double &A4,double &A5,double &A6,double &E2,double &E3);
DllAPI bool SetRobotJoints(double jointsdegree[]);
DllAPI void GetRobotEOA(double &X,double &Y,double &Z,double &A,double &B,double &C);
C#中的调用:
      class Call
    {

        [DllImport("KinematicService.dll", EntryPoint="SetRobotJoints",CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern byte SetRobotJoints(double[] jointsdegree);
        [DllImport("KinematicService.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetRobotEOA(ref double X, ref double Y, ref double Z, ref double A, ref double B, ref double C);
        [DllImport("KinematicService.dll",CharSet = CharSet.Auto,CallingConvention= CallingConvention.Cdecl)]
        public static extern byte GP_InverseK(double[] position, double[] closeJoints, ref double E1, ref double A1, ref double A2, ref double A3, ref double A4, ref double A5, ref double A6, ref double E2, ref double E3);

    }
调用方式如:
byte isOK = Call.GP_InverseK(position, closeJoints,ref E1,ref A1,ref A2,ref A3,ref A4,ref A5,ref A6,ref E2,ref E3); --------------------编程问答-------------------- 函数里涉及到浮点数运算,我的项目是涉及到WPF的.net4.0程序,调用dll时,dll内部的浮点数运算出错(未涉及传参),但是我自己编写的一个简单的.net4.0测试程序调用这个dll时就能得到正确结果。 --------------------编程问答-------------------- 这是个很诡异的问题^up
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,