ITPub博客

首页 > > > C#调用C++的dll问题

C#调用C++的dll问题

作者:gamebus 时间:2019-10-18 11:12:29 0 删除 编辑

C++中的方法为api_ihuReadCurrentValue1,方法的各项参数说明如下:

//api_ihuReadCurrentValue1

//功能:查询多个标签点的实时值

//参数说明:

//long serverhandle                 in连接句柄

//int number_of_tags                 in查询点数

//char *tagnames                         in      点名字符串,以逗号拼接,如:"a,b"

//float *tagvalues                         out 数组,查询返回的值,与tagnames数组对应

//IHU_TIMESTAMP *timestampsout结构体数组,返回的时间戳

//int *tagerrs                                out数组,1-查询成功,0-查询失败

//char *retmessage                         out错误信息描述

//返回值:

//>0--IH函数返回错误

//0--成功

//<0-失败,其中-50:异常,-100:加载错误,错误信息参考retmessage

我在C#中声明该方法如下:

定义C++中的结构体:

[StructLayout(LayoutKind.Sequential)]

public struct IHU_TIMESTAMP

{

public int Seconds;

public int Subseconds;

}

声明C++中的方法

[DllImport("IHAPIDLLS.dll", EntryPoint = "api_ihuReadCurrentValue1", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]

public static extern int Api_ihuReadCurrentValue1(int serverhandle, int number_of_tags, string tagnames, float[] tagvalues,IntPtr timestamps, ref int tagerrs, StringBuilder retmessage);

输出的结构体数组我用IntPtr类型,请问,C#中对应C++的类型是否正确?另外,我传入结构体数组调用该方法是这样做的:

string[] strarr = pointStr.Split(',');

if (strarr == null || strarr.Length <= 0)

return "";

int count = strarr.Length;

//记录温度点数据的数组

float[] floatA = new float[count];

int size = Marshal.SizeOf(typeof(IHU_TIMESTAMP));

IHU_TIMESTAMP[] IHU_TIMESPAN = new IHU_TIMESTAMP[count];

//查询点数据成功与否的标志变量,1表示查询成功;0表示查询失败

int tagerrs = 0;

//接收查询记录的相关信息(比如查询成功、查询失败等)

StringBuilder queryLog = new StringBuilder(15);

IntPtr parrStudyRec = Marshal.AllocHGlobal(Marshal.SizeOf(size * count));

int run = (int)parrStudyRec;

for (int i = 0; i < count; i++)

{

//IHU_TIMESPAN[i] = new IHU_TIMESTAMP();

Marshal.StructureToPtr(IHU_TIMESPAN[i], (IntPtr)run, true);

run += size;

}

//读取输出的点数值

int result1 = IHUOperatorClass.Api_ihuReadCurrentValue1(connectHandler, count, pointStr, floatA, parrStudyRec, ref tagerrs, queryLog);

但是执行的时候报错了:运行时遇到了错误。此错误的地址为 0x663af09d,在线程 0x16c0 上。错误代码为 0xc0000005。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。

 

请问,我在C#中声明的方法对吗?如果声明的参数没有问题,那我封送结构体数组有问题吗?

由于我是第一次处理这种平台调用的问题,所以找了好几天的资料都没有解决好,在这里,我希望能得到各位的帮助。谢谢!

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4687/viewspace-3164032/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-10

  • 博文量
    155
  • 访问量
    13187