日期:2014-05-17  浏览次数:20880 次

c++的一个结构怎么转换成C#
typedef struct _BtSdkRemoteServiceAttrStru
{
unsigned short mask; union
{
unsigned short svc_class;           unsigned short service_class;
}; unsigned long dev_hdl; unsigned char svc_name[BTSDK_SERVICENAME_MAXLENGTH];
void * ext_attributes;
unsigned short status;
} BtSdkRemoteServiceAttrStru, *PBtSdkRemoteServiceAttrStru;

C++里的结构如上,函数调用的时候是一个BtSdkRemoteServiceAttrStru*的指针,
请问在C#里该怎么写,谢谢各位!!!!

------解决方案--------------------
按照struct的顺序依次翻译:
struct直接对应 

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct
{
   //字符串使用
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = BTSDK_SERVICENAME_MAXLENGTH)]
    public string svc_name;

}

Union与struct对应

  [System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
   public struct unionStruct
   {
      [FieldOffset(0)]
      Uint16 svc_class;
      [FieldOffset(0)]
      Uint16 service_class;
   }



------解决方案--------------------

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)]
public struct Anonymous 
{
    /// unsigned short
    [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
    public ushort svc_class;
    
    /// unsigned short
    [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
    public ushort service_class;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct BtSdkRemoteServiceAttrStru 
{    
    /// unsigned short
    public ushort mask;
    
    /// Anonymous
    public Anonymous Union1;
    
    /// unsigned int
    public uint dev_hdl;
    
    /// unsigned char[]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=-1)]
    public string svc_name;
    
    /// void*