问题描述
各位大神们,我在C#中调用c++的dll提供的接口函数,函数中需要传递一个结构体dll中的函数接口是:ReadTag(HANDLEhHandle,REC_INFO&data);其中REC_INFO是一个结构体structREC_INFO{BYTEInfoType;union{//收集标签返回struct{BYTETagID[8];//BYTETagState;struct{BYTETagPower:2;BYTETagSensor:1;BYTETagInitialization:1;BYTERetention:4;}TagState;unsignedshortExecutionStatus;unsignedshortSlotNum;unsignedshortFileIdentifier;BYTEDataLen;BYTEData[255];BYTERSSI;}CollectTag;struct{BYTEDataLen;BYTEData[260];}TransparentTransmission;};};
现在调用接口的时候会提示:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。我怀疑是在C#里面定义的这个结构体的问题,求解大神们帮帮忙把这个C++的结构体转换为C#的
解决方案
解决方案二:
嵌套太多,没时间帮你手动翻译。直接找了个工具翻译滴。有些“花代码”你自己手动修改一下把[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]publicstructAnonymous_0e83c733_bea5_4f5e_be2f_e38cd4644be1{///TagPower:2///TagSensor:1///TagInitialization:1///Retention:4publicuintbitvector1;publicuintTagPower{get{return((uint)((this.bitvector1&3u)));}set{this.bitvector1=((uint)((value|this.bitvector1)));}}publicuintTagSensor{get{return((uint)(((this.bitvector1&4u)/4)));}set{this.bitvector1=((uint)(((value*4)|this.bitvector1)));}}publicuintTagInitialization{get{return((uint)(((this.bitvector1&8u)/8)));}set{this.bitvector1=((uint)(((value*8)|this.bitvector1)));}}publicuintRetention{get{return((uint)(((this.bitvector1&240u)/16)));}set{this.bitvector1=((uint)(((value*16)|this.bitvector1)));}}}[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]publicstructAnonymous_b974fe8e_c7a4_4c3b_9ab4_3e4f3fd415e2{///BYTE[8][System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst=8,ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]publicbyte[]TagID;///Anonymous_0e83c733_bea5_4f5e_be2f_e38cd4644be1publicAnonymous_0e83c733_bea5_4f5e_be2f_e38cd4644be1TagState;///unsignedshortpublicushortExecutionStatus;///unsignedshortpublicushortSlotNum;///unsignedshortpublicushortFileIdentifier;///BYTE->unsignedcharpublicbyteDataLen;///BYTE[255][System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst=255,ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]publicbyte[]Data;///BYTE->unsignedcharpublicbyteRSSI;}[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]publicstructAnonymous_a1eb1eaf_5656_44e5_9dfb_a13f49142b14{///BYTE->unsignedcharpublicbyteDataLen;///BYTE[260][System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst=260,ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]publicbyte[]Data;}[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)]publicstructAnonymous_fee68aea_d431_4797_ab3a_060273d18071{///Anonymous_b974fe8e_c7a4_4c3b_9ab4_3e4f3fd415e2[System.Runtime.InteropServices.FieldOffsetAttribute(0)]publicAnonymous_b974fe8e_c7a4_4c3b_9ab4_3e4f3fd415e2CollectTag;///Anonymous_a1eb1eaf_5656_44e5_9dfb_a13f49142b14[System.Runtime.InteropServices.FieldOffsetAttribute(0)]publicAnonymous_a1eb1eaf_5656_44e5_9dfb_a13f49142b14TransparentTransmission;}[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]publicstructREC_INFO{///BYTE->unsignedcharpublicbyteInfoType;///Anonymous_fee68aea_d431_4797_ab3a_060273d18071publicAnonymous_fee68aea_d431_4797_ab3a_060273d18071Union1;}
解决方案三:
必须要确定结构体大小是一致的,不然根本没法用啊,小了就是内存保护了,大了就是各种错位.
解决方案四:
1.c/c++中的struct和union在c#总都对应struct,2.如果嵌套,依次定义就可以使用。//这个是基础结构体[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=1)]publicstructSStruct1{//...}//嵌套使用[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=1)]publicstructSStruct2{publicSStruct1m1;//成员//others}3.Union也对应struct:union{ReadParamtReadParam;WriteParamtWriteParam;}op;这么翻译[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]publicstructop{[FieldOffset(0)]publicReadParamtReadParam;[FieldOffset(0)]publicWriteParamtReadParam;}