PInvoke调用Bug一例

xiaoxiao2021-02-28  90

开发工具:vs2015

问题描述:

C API升级后,客户端工具相应的按照修改后的API升级。

测试反馈,批量获取标签的函数会导致vs在debug模式下崩溃。

c api的定义如下:

rtdb_error_t DBPT_GetTagsBaseAttribute(IN rtdb_server_handle_t* pServerHandle, IN int nTagCount, IN_OUT int* pErrorCodeArray, IN int* pTagIDArray, IN_OUT rtdb_tag_attribute_t* pTagAttrArray); 除了rtdb_tag_attribute_t是自定义的结构体,其他参数都是基本类型。 c#的封装

[DllImport(RTDB_DLL,EntryPoint = 'DBPT_GetTagsBaseAttribute',CharSet = CharSet.Ansi,CallingConvention= CallingConvention.StdCall] public static extern int GetTagsAttribute(int nHandle,int nTagCount,out int[] errorArray,out TagAttr[] TagAttary);

问题分析:

1. c 测试代码运行正常,可以确定不是c api本身的问题; 

2. C# dllimport代码中去掉out ,程序不崩溃,但是无传出数据;

3.  在vs2015下面,通过“启用本机代码调试”,发现崩溃发生在c api中给传入数组赋值时。

推测可能长度不匹配。核对后无异常。

c api中结构体定义使用1字节对齐,c#中对应结构体也采用Pack= 1。核对无异常。

不直接使用数组,而采用IntPtr,测试通过,推断是c#分配的对象未正常传递给c api。

问题解决:

修改out 为 [In,Out],bug排除。

[DllImport(RTDB_DLL,EntryPoint = 'DBPT_GetTagsBaseAttribute',CharSet = CharSet.Ansi,CallingConvention= CallingConvention.StdCall] public static extern int GetTagsAttribute(int nHandle,int nTagCount,[In,Out] int[] errorArray,[In,Out] TagAttr[] TagAttary);

小结:

细节很重要。

----------------------------------------------------------------

附带测试中使用的代码:

如果结构体对应麻烦,选择IntPtr也是个快捷的方法,缺点是每个api都需要单独编码转换:

[DllImport(RTDB_DLL,EntryPoint = 'DBPT_GetTagsBaseAttribute',CharSet = CharSet.Ansi,CallingConvention= CallingConvention.StdCall] public static extern int GetTagsAttributeWithPtr(int nHandle,int nTagCount,[In,Out] int[] errorArray,IntPtr TagAttr[] TagAttary); int size = Marshal.SizeOf(typedef(TagAttr)) ; IntPtr attrInput = Mashal.AllocHGlobal(count*size); DBPT.GetTagsAttributeWithPtr(nHandle,count,out errors,ids, attrInput); for(int i = 0;i< count;i++) { IntPtr ptr = (IntPtr)((UInt32)attrInput + i* size); tagsArray[i] = (TagAttr)Mashal.PtrToStructure(ptr,typeof(TagAttr)); } Marshal.FreeHGlobal(attrInput);

下列代码来自网络: 碰到C++指针参数,需要将地址转为不被垃圾收集器清理的固定地址。

c api 原型

bool FireSDK_GetDeviceList(int loginid, DEVICE ** devinfo, int * devcount, CHANNEL ** chinfo, int * chcount);

struct devinfo { public string devID; public string devName; //…… } //调用 devinfo di = new devinfo(); di.devID = "KD0000299"; di.devName="myDevice"; GCHandle C_di = GCHandle.Alloc(di, GCHandleType.Pinned); FireSDK_GetDeviceList(loginid,out C_di.AddrOfPinnedObject(),...); C_di.Free();

转载请注明原文地址: https://www.6miu.com/read-60094.html

最新回复(0)