如何将C ++ union转换为C#struct

时间:2017-04-13 02:38:32

标签: c# c++ struct unions

typedef struct _communicate_data
{
    unsigned  short     m_type;
    unsigned char m_response_result;
    union{
        struct request_set_integrity
        {
            char  m_version[32];
            char  m_installed_path[256];
        }REQUEST_INTEGRITY;

        struct _request_update
        {
            char m_install_dir[MAX_PATH];
            char m_unzip_dir[MAX_PATH];
        }REQUEST_UPDATE;    
        struct response_dna
        {
            int      m_len; 
            char m_dna[256];
        }RESPONSE_DNA;    
        struct request_setinfo
        {
            int m_len; 
            char m_host[128];
            char m_userid[64]; 
            char m_deviceid[128];
        }REQUEST_SETINFO;    
        char  m_reason[256];            
        struct response_realtime
        {
            int m_type;
            char m_path[256];
        }RESPONSE_REALTIME;    
        struct response_status
        {
            char m_dna_status;
            char m_realtime_status;
            char m_policy_status;
            char m_application_status;
            char m_login_status;
            char m_usb_status;
            char m_sendlog_status;
            char m_isolate_status;
            char m_mode;  
        }RESPONSE_STATUS;    
        struct response_version
        {
            unsigned short m_major_version;
            unsigned short m_minor_version;
            unsigned short m_build_version;
            unsigned short m_release_version;
        }RESPONSE_VERSION;    
        char  m_dbname[256];
        char  m_policyname[256];    
        struct request_set
        {
            DNA_PERFORMACE m_performace;
            XSP_RUN_MODE   m_mode;
        }REQUEST_SET;    
        struct response_compare
        {
            UINT  m_type;
            char m_path[MAX_PATH];
        }RESPONSE_COMP;
    }data;
}COMMUNICATE_DATA;

在C ++中有一个带有union的结构,所以任务是将这个结构转换为C#代码。我不得不转换2天。我无法正确设置[FieldOffset(0)]属性。

任何人都可以帮助将此结构转换为联合吗? 谢谢!

修改 这是我转换为C#代码。但是它给出了错误“附加信息:无法从程序集'ProjectName,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'加载类型'Common.dataunion',因为它包含偏移0处的对象字段,该字段未正确对齐或由非对象字段重叠。“

 public struct dataunion
        {       
            [FieldOffset(0)]
            public request_set_integrity REQUEST_INTEGRITY;
            [FieldOffset(0)]
            public _request_update REQUEST_UPDATE;
            [FieldOffset(0)]
            public response_dna RESPONSE_DNA;
            [FieldOffset(0)]
            public request_setinfo REQUEST_SETINFO;
            [FieldOffset(0)]
            public byte[] m_reason;
            [FieldOffset(0)]
            public response_realtime RESPONSE_REALTIME;
            [FieldOffset(0)]
            public response_status RESPONSE_STATUS;
            [FieldOffset(0)]
            public response_version RESPONSE_VERSION;
            [FieldOffset(320)]
            public byte[]  m_dbname;
            [FieldOffset(320)]
            public byte[] m_policyname;
            [FieldOffset(0)]
            public request_set REQUEST_SET;
            [FieldOffset(0)]
            public response_compare RESPONSE_COMP;
        }
      [StructLayout(LayoutKind.Sequential)]
        public struct comData
        {      
            public ushort m_type;        
            public byte m_response_result;        
            public dataunion data;       
        }

1 个答案:

答案 0 :(得分:0)

您可以阅读有关此here的更多信息。我相信那里的信息是可以理解的,我不必在这里复制它。 祝你好运!