C原型:
typedef enum _SUPPORT_LAYER
{
HDLC = 0x01,
TCPIP,
UDPIP
}SUPPORT_LAYER;

typedef union _ADDR
{
    struct
    {
        unsigned int ServerLowerAddr : 14;  //服务器低地址
        unsigned int ServerUpperAddr : 14;  //服务器高地址
        unsigned int res : 4;
        unsigned char ClientAddr;
        unsigned char ServerAddrLen;
    }HDLC_ADDR1;    struct
    {
        unsigned int ServerAddr : 28;
        unsigned int res : 4;
        unsigned char ClientAddr;
        unsigned char ServerAddrLen;
    }HDLC_ADDR2;    struct
    {
        unsigned int Client_TCP_Port;
        unsigned int Server_TCP_Port;
        unsigned char Client_IP_Addr[4];
        unsigned char Server_IP_Addr[4];
    }TCPIP_ADDR;    struct
    {
        unsigned int Client_wPort;
        unsigned int Server_wPort;
        unsigned int Client_UDP_Port;
        unsigned int Server_UDP_Port;
        unsigned char Client_IP_Addr[4];
        unsigned char Server_IP_Addr[4];
    }UDPIP_ADDR;  
}ADDR;  int ProcessServicePrimitive(unsigned char **OUTData, unsigned int &OUTDataLen,SUPPORT_LAYER SupportLayerType, ADDR Addr,const unsigned char *Xml, unsigned int XmlLen);
请问如何用C#调用ProcessServicePrimitive参数?C#Cstruct

解决方案 »

  1.   

     public enum SUPPORT_LAYER
            {
                HDLC = 0x01,
                TCPIP,
                UDPIP
            }
          
            [StructLayout(LayoutKind.Sequential)]
            public struct HDLC_ADDR1
            {
               public uint ServerLowerAddr;
               public uint ServerUpperAddr;
               public uint res;
               public char ClientAddr;
               public char ServerAddrLen;
            };
            [StructLayout(LayoutKind.Sequential)]
            public struct HDLC_ADDR2
            {
                public uint ServerAddr;
                public uint res;
                public char ClientAddr;
                public char ServerAddrLen;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct TCPIP_ADDR
            {
                public uint Client_TCP_Port;
                public uint Server_TCP_Port;
                public char [] Client_IP_Addr;
                public char [] Server_IP_Addr;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct UDPIP_ADDR
            {
                public uint Client_wPort;
                public uint Server_wPort;
                public uint Client_UDP_Port;
                public uint Server_UDP_Port;
                public char [] Client_IP_Addr;
                public char [] Server_IP_Addr;
            }
            [StructLayout(LayoutKind.Explicit,Size=500)]        public struct ADDR
            {            [FieldOffset(0)]
                public HDLC_ADDR1 a;            [FieldOffset(50)]
                public HDLC_ADDR2 b;            [FieldOffset(180)]
                public TCPIP_ADDR c;            [FieldOffset(300)]
                public UDPIP_ADDR d;
            }
     [DllImport("pro_C62056.dll")]
            public static extern int ProcessServicePrimitive(
     ref string str, ref UInt32 len, SUPPORT_LAYER SupportLayerType, ADDR Addr,
     string  strxml, int len1);
    我这样调用老是提示外部组件异常
      

  2.   

    Union应该是共享地址的,大小为最大的结构体所占的大小
            [StructLayout(LayoutKind.Explicit,Size=24)]
            public struct ADDR
            {            [FieldOffset(0)]
                public HDLC_ADDR1 a;            [FieldOffset(0)]
                public HDLC_ADDR2 b;            [FieldOffset(0)]
                public TCPIP_ADDR c;            [FieldOffset(0)]
                public UDPIP_ADDR d;
            }