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

c#调用C++的dll时,接口函数里的数组如何传递
C/C++ code

extern "C" __declspec(dllexport) BOOL Initdata(double* d_Conc, double* d_RLU,double d_Cal1RLU, double d_Cal2RLU,double d_Cal1Conc, double d_Cal2Conc, int nLen,int FitMethod);



数组地址如何传递呢??

C# code

            double[] arrConc = new double[]
            {
                0,4,16,
                32,64,128
            };
            double[] arrRlu = new double[]
            {
                8487,36329,159167,
                315859,654285,1190543
            };
            double Cal1Rlu = 36330;
            double Cal1Conc = 4;
            double Cal2Rlu = 654285;
            double Cal2Conc = 64;
     
            int len = 6;
            int fitWay = 1;
            //初始化无法获取到正确的
            bool rt = Initdata(ref arrConc[0], ref arrRlu[0], Cal1Rlu, Cal1Conc, Cal2Rlu, Cal2Conc, len, fitWay);
   [DllImport("Cal_datadll.dll")]
        public static extern bool Initdata(ref double dConc, ref double dRlu,
             double Cal1Rlu, double Cal1Conc, double Cal2Rlu, double Cal2Conc, int len, int fitWay);


/************************/
[DllImport("Cal_datadll.dll")]
        public static extern bool Initdata(ref double[] dConc, ref double[] dRlu,
             double Cal1Rlu, double Cal1Conc, double Cal2Rlu, double Cal2Conc, int len, int fitWay);
     bool rt = Initdata(ref arrConc, ref arrRlu, Cal1Rlu, Cal1Conc, Cal2Rlu, Cal2Conc, len, fitWay);
//也不行.



------解决方案--------------------
C++数据类型与C#对应关系
------解决方案--------------------
C#调用C++编写的DLL函数, 以及各种类型的参数传递
------解决方案--------------------
double* 用 ref double
数组的话 doubel[]
------解决方案--------------------
bool rt = Initdata(ref arrConc[0], ref arrRlu[0], Cal1Rlu, Cal1Conc, Cal2Rlu, Cal2Conc, len, fitWay);
这个有问题吧,如果double*表示数组,那就直接用double[],不要用ref,还有C++中的指针在C#中可以用Intptr表示,不管指针表示的是数组还是字符串。
------解决方案--------------------
http://wenku.baidu.com/view/5126f84b2b160b4e767fcf85.html?st=1在给各连接看看
------解决方案--------------------
[MarshalAs(UnmanagedType.LPArray,SizeParamIndex=1)] double [] Z,,, [MarshalAs(UnmanagedType.LPArray,SizeParamIndex=1)】在C#中数组是直接使用的,而在C++中返回的是数组的指针,这句用来转化这两种不同的类型.