首页 新闻 会员 周边

C#中调用DLL中的函数出现异常:尝试读取或写入受保护的内存。这通常指示其他内存已损坏

0
悬赏园豆:30 [待解决问题]

求助大家,在读取完串口后,向调用一个外部DLL文件的处理函数对数据进行处理出现异常
1.结构体声明:
internal struct FactorArraysp
{
public int iFactorLength;
public unsafe float* Factor;
}

2.DLL中函数引用声明:
[DllImport("sp.dll")]
public static extern void linearCalibration(float[] power, float[] power_linear, FactorArraysp linearFactorStru);

[DllImport("Driver.dll")]
public static extern FactorArraysp getNonlinearityCorrectionCoefficient();

3.调用DLL中的函数:
private static FactorArraysp linearFactorArraysp = default(FactorArraysp);
linearFactorArraysp = PT50030dll.getNonlinearityCorrectionCoefficient();

float[] datapower = DataProcessor.GaussFilter(rawDatas, 1);
float[] linear_data = DataProcessor.LinearCalibration(datapower);//datapower为前面代码中调用同一个DLL中函数读取的float[]类型数组

public class DataProcessor{
public static float[] LinearCalibration(float[] power)
{
try
{
float[] array = new float[7296];
Algorithm.linearCalibration(power, array, linearFactorArraysp);//这句报错尝试读取或写入受保护的内存。这通常指示其他内存已损坏!!!
return array;
}
catch (Exception)
{
throw;
}
}
}

补充:有一点忘记说明了,我手上只有sp.dll和Driver.dll的库文件,没有它们的具体介绍说明,所以不清楚库文件中函数具体是怎么实现的,结构和方法的声明引用我是参考附带的一个简单例程

JaydenLuo的主页 JaydenLuo | 初学一级 | 园豆:174
提问于:2019-01-16 21:12
< >
分享
所有回答(1)
0

你可以双击引用,查看一下dll里面的函数名称,是否和你调用的名称一样,ilspy反编译工具可以查看具体dll里面的代码实现

Melody.Gkx | 园豆:196 (初学一级) | 2019-01-22 10:29

这是非托管的dll,不能直接在资源管理器中引用.需要DLLImport(XXX.dll),它们一般是用C++或C实现封装的,我在反编译工具例只能看到PE文件

支持(0) 反对(0) JaydenLuo | 园豆:174 (初学一级) | 2019-01-23 09:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册