c#引用dll文件 参数为结构体引用,结果显示尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSy...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;//引入dll文件的函数
namespace dll文件链接
{
class Program
{
[DllImport("BurningEx.dll")]
//这是c++提供的dll接口函数
// private static extern bool _CalcBurning(const InputStruct & inputParams, OutputStruct & outParams);//引入dll中写好的函数
private static extern bool _CalcBurning(ref InputStruct inputParams, OutputStruct outParams);//引入dll中写好的函数
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct InputStruct
{
public double m_H2O;//H2O(mol/kg)
public double m_CO2;//CO2(mol/kg)
public double m_H2;//H2(mol/kg)
public double m_N2;//N2(mol/kg)
public double m_CO;//CO(mol/kg)
public double m_P0;//压力段(MPa)开始
public double m_P1;//压力段(MPa)结束
public double m_rfsy;//发射药密度(g/cm3)
public double m_xadh;//硝胺的粒度(cm)
public double m_rxa;//硝胺的密度(g/cm3)
public double m_hlxa;//硝胺的百分含量
public double m_Ptz;//特征压力(MPa)
public double m_pn1;//拟合的压力范围(MPa)开始
public double m_pn2;//拟合的压力范围(MPa)结束
}
public struct OutputStruct
{
public double m_u1;//燃速系数(mm.MPa-n.s-1)
public double m_n;//压力指数
public int GS1;//点的个数
public double[] UP;//Y坐标的值,单位U(mm/s),该数组从1开始计数
public double [] PP;//X坐标的值,单位P(MPa),该数组从1开始计数
}
static void Main(string[] args)
{
InputStruct a;
a.m_H2O = 1.0;
a.m_CO2 = 1.0;
a.m_H2 = 1.0;
a.m_N2 = 1.0;
a.m_CO = 1.0;
a.m_P0 = 1.0;
a.m_P1 = 1.0;
a.m_rfsy = 1.0;
a.m_xadh = 1.0;
a.m_rxa = 1.0;
a.m_hlxa = 1.0;
a.m_Ptz = 1.0;
a.m_pn1 = 1.0;
a.m_pn2 = 1.0;
OutputStruct b;
b.m_u1 = 1.0;
b.m_n = 1.0;
b.GS1 = 1;
b.UP = new double []{1.0};
b.PP = new double[] { 1.0 };
bool c= _CalcBurning( ref a,b);
Console.WriteLine(c);
Console.ReadKey();
}
}
}
贴上代码 展开
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;//引入dll文件的函数
namespace dll文件链接
{
class Program
{
[DllImport("BurningEx.dll")]
//这是c++提供的dll接口函数
// private static extern bool _CalcBurning(const InputStruct & inputParams, OutputStruct & outParams);//引入dll中写好的函数
private static extern bool _CalcBurning(ref InputStruct inputParams, OutputStruct outParams);//引入dll中写好的函数
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct InputStruct
{
public double m_H2O;//H2O(mol/kg)
public double m_CO2;//CO2(mol/kg)
public double m_H2;//H2(mol/kg)
public double m_N2;//N2(mol/kg)
public double m_CO;//CO(mol/kg)
public double m_P0;//压力段(MPa)开始
public double m_P1;//压力段(MPa)结束
public double m_rfsy;//发射药密度(g/cm3)
public double m_xadh;//硝胺的粒度(cm)
public double m_rxa;//硝胺的密度(g/cm3)
public double m_hlxa;//硝胺的百分含量
public double m_Ptz;//特征压力(MPa)
public double m_pn1;//拟合的压力范围(MPa)开始
public double m_pn2;//拟合的压力范围(MPa)结束
}
public struct OutputStruct
{
public double m_u1;//燃速系数(mm.MPa-n.s-1)
public double m_n;//压力指数
public int GS1;//点的个数
public double[] UP;//Y坐标的值,单位U(mm/s),该数组从1开始计数
public double [] PP;//X坐标的值,单位P(MPa),该数组从1开始计数
}
static void Main(string[] args)
{
InputStruct a;
a.m_H2O = 1.0;
a.m_CO2 = 1.0;
a.m_H2 = 1.0;
a.m_N2 = 1.0;
a.m_CO = 1.0;
a.m_P0 = 1.0;
a.m_P1 = 1.0;
a.m_rfsy = 1.0;
a.m_xadh = 1.0;
a.m_rxa = 1.0;
a.m_hlxa = 1.0;
a.m_Ptz = 1.0;
a.m_pn1 = 1.0;
a.m_pn2 = 1.0;
OutputStruct b;
b.m_u1 = 1.0;
b.m_n = 1.0;
b.GS1 = 1;
b.UP = new double []{1.0};
b.PP = new double[] { 1.0 };
bool c= _CalcBurning( ref a,b);
Console.WriteLine(c);
Console.ReadKey();
}
}
}
贴上代码 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询