c# 调用 c++dll 时c#的代码怎么写

#include "stdlib.h "
#include
#include
#include
#include "windows.h"
#include

#define m_col 3
#define m_dim 3

void InvMatrix(const double M[][m_col], const int n, double invM[][m_col]);
void LinearFit(const double Coef[][m_dim], const int n, double *Para);

void InvMatrix(const double M[][m_col], const int n, double invM[][m_col])
{
if(n==1)
invM[0][0] = 1/M[0][0];
else
{
double cu, cd, normb;
double (*b)[m_col] = new double[n][m_col];
double (*invv)[m_col] = new double[n][m_col];
for(int j=0;j {
double *schu = new double[j];
double *schd = new double[j];
if(j>0)
{
for(int k=0;k<j;k++)
{
schu[k] = schd[k] = 0;
for(int i=0;i<n;i++)
{
schu[k]+= b[i][k]*M[i][j];
schd[k]+= b[i][k]*b[i][k];
}
}
}

        normb = 0;
        for(int i=0;i<n;i++)
        {
            b[i][j] = M[i][j];
            if(j>0)
            {
                for(int k=0;k<j;k++)
                    b[i][j]-= b[i][k]*schu[k]/schd[k];
            }
            normb+= b[i][j]*b[i][j];
        }
        normb = sqrt(normb);
        for(int i=0;i<n;i++)
            invv[j][i] = b[i][j]/normb;

        delete schu;
        delete schd;
    }

    double (*c)[m_col] = new double[n][m_col];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=i;j++)
        {
            cu = cd = 0;
            if(j<i)
            {
                for(int k=0;k<n;k++)
                {
                    cu+= M[k][i]*b[k][j];
                    cd+= b[k][j]*b[k][j];
                }
                c[j][i] = cu/sqrt(cd);
            }
            else
            {
                for(int k=0;k<n;k++)
                    cd+= b[k][j]*b[k][j];
                c[j][i] = sqrt(cd);
            }
        }
    }

    double (*invc)[m_col] = new double[n][m_col];
    for(int j=0;j<n;j++)
    {
        for(int i=n-1;i>=0;i--)
        {
            if(i>j)
                invc[i][j] = 0;
            else if(i==j)
                invc[i][j] = 1/c[i][j];
            else
            {
                invc[i][j] = 0;
                for (int k=i+1;k<=j;k++)
                    invc[i][j]-= c[i][k]*invc[k][j];
                invc[i][j]/= c[i][i];
            }
        }
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            invM[i][j] = 0;
            for(int k=0;k<n;k++)
                invM[i][j]+= invc[i][k]*invv[k][j];
        }
    }
    delete []b;
    delete []c;
    delete []invv;
    delete []invc;
}

}

void LinearFit(const double Coef[][m_dim], const int n, double *Para)

{
double (*A)[m_col] = new double[m_dim][m_col];
for(int i=0;i<m_dim-1;i++)
{
for(int j=0;j<m_dim-1;j++)
{
A[i][j] = 0;
for(int k=0;k<n;k++)
A[i][j]+= Coef[k][i]*Coef[k][j];
}
}
for(int i=0;i<m_dim-1;i++)
{
A[i][m_dim-1] = A[m_dim-1][i] = 0;
for(int j=0;j<n;j++)
A[i][m_dim-1]+= Coef[j][i];
A[m_dim-1][i] = A[i][m_dim-1];
}
A[m_dim-1][m_dim-1] = n;

double *b = new double[m_dim];
for(int i=0;i<m_dim-1;i++)
{
    b[i] = 0;
    for(int j=0;j<n;j++)
        b[i]+= Coef[j][m_dim-1]*Coef[j][i];
}
b[m_dim-1] = 0;
for(int i=0;i<n;i++)
    b[m_dim-1]+= Coef[i][m_dim-1];

double (*invA)[m_col] = new double[m_dim][m_col];
InvMatrix(A, m_dim, invA);

for(int i=0;i<m_dim;i++)
{
    Para[i] = 0;
    for(int j=0;j<m_dim;j++)
        Para[i]+= invA[i][j]*b[j];
}

delete []A;
delete []invA;
delete b;

}

extern "C" __declspec(dllexport) void nh( double data[12],double k[3] )
{

double A[4][3] ={data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10],data[11]};
double Para[3];
LinearFit(A, 4, Para);
k[0]=Para[1];
k[1]=Para[2];
k[2]=Para[3];

}

这个是c++函数,函数是我在网上找的,我自己写的是最后一个 void nh

我在c#的代码是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace attack
{
class Program
{
[DllImport("atta.dll", EntryPoint = "nh", CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern void nh( ref double[] data ,ref double []k);

    static void Main(string[] args)
    {
        double [] k=new double [3];
        k[0] = 1;
        double a=0;
        double[] data = { 0.1400, 0.4966, 0.0692, -0.2134, 0.6570, 0.0557, -0.3674, 0.2992, 0.0577, -0.0260, 0.1541, 0.0744 };
        nh( ref  data, ref k );
        a =  k[0];
        Console.WriteLine(Convert.ToString (a ) );//我想用a来看代码对不对
    }
}

}

最后出现了问题是a = k[0];这句
在 System.NullReferenceException 中第一次偶然出现的“attack.exe”类型的异常
未处理的“System.NullReferenceException”类型的异常出现在 attack.exe 中。
attack是我写的这个c#项目名

1个回答

如果就这么几行代码,直接改写成C#就可以了。真心担心你这糟糕的C++代码有内存泄漏。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问