none
c#调用C++的dll文件问题 RRS feed

  • 问题

  • c++的头文件如下:

    struct mcsEvent4Device_t{
     char device[64];
     int  type;
     int  state;
      int  param;
    };
    struct mcsEvent4Agent_t{
     int  state;
     char agent[64];
     char device;//LoginDevice OR Queue
     int  param;//Reason
    };
    struct mcsEventServiceInitiated_t{
     char station[64];
     char callid[32];
    };
    struct mcsEventOrignated_t{
     char alerting[64];
     char caller[64];
     char calling[64];
     char calleed[64];
     char callid[32];
    };
    struct mcsEventDelivered_t{
     char alerting[64];
     char caller[64];
     char router[64];
     char calling[64];
     char calleed[64];
     char callid[32];
    };
    struct mcsEventConnectionClear_t{
     char dropDevice[64];
     char callid[32];
     int reason;
    };
    struct mcsEventCallClear_t{
     char callid[32];
     int reason;
    };
    struct mcsEventCallActive_t{
     char callid[32];
     char actionDevice[64];
     char heldDevice[64];
     char calling[64];
     char calleed[64];
    };
    struct mcsEventCallHeld_t{
     char callid[32];
     char actionDevice[64];
     char heldDevice[64];
     char calling[64];
     char calleed[64];
    };
    struct mcsEventCallTransfered_t{
     char newcall[32];
     char actionDevice[64];
     char heldcall[32];
     char heldDevice[64];
     char activecall[32];
     char activeDevice[64];
     char calling[64];
     char calleed[64];
    };
    struct mcsEventCallEstanblished_t{
     char callid[32];
     char firstDevice[64];
     char secondDevice[64];
     char calling[64];
     char calleed[64];
    };
    struct mcsEvent4Call_t{
     int code;
     union{
      mcsEventServiceInitiated_t serviceInitiated;
      mcsEventOrignated_t orignated;
      mcsEventDelivered_t delivered;
      mcsEventConnectionClear_t connClear;
      mcsEventCallClear_t callClear;
      mcsEventCallActive_t callActive;
      mcsEventCallTransfered_t callTransfered;
      mcsEventCallHeld_t callHeld;
      mcsEventCallEstanblished_t callEstanb;
     }u;

    };
    struct mcsEvent4Media_t{
    };
    struct mcsEvent_t{
     int code;
     char timestamp[64];
     union{
      mcsEvent4Device_t deviceEvt;
      mcsEvent4Agent_t agentEvt;
      mcsEvent4Call_t  callEvt;
      mcsEvent4Media_t mediaEvt;
     }u;
    };

    下面是我访问的C#代码:

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct mcsEvent_t
        {
            public int code;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
            public Char[] timestamp;
            [StructLayout(LayoutKind.Explicit, Size = 8, CharSet = CharSet.Ansi, Pack = 1)]
            public struct union
            {
                [FieldOffset(0)]
                public mcsEvent4Media_t deviceEvt;
                [FieldOffset(0)]
                public mcsEvent4Media_t agentEvt;
                [FieldOffset(0)]
                public mcsEvent4Media_t callEvt;
                [FieldOffset(0)]
                public mcsEvent4Media_t mediaEvt;
            };
            public union tempUnion;
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
            public struct mcsEvent4Media_t
            {
     
            }
            public mcsEvent4Media_t tempEvent4Media;

            [DllImport("cmapi.dll", EntryPoint = "mcsGetEvent")]
            public static extern int mcsGetEvent(ref mcsEvent_t Medthevent);
            mcsEvent_t tempEvent = new mcsEvent_t();
            tempEvent.code
            这样访问出来的code的值是正确的,但是我想访问deviceEvt中的值怎么访问
            如果还是按照mcsEvent4Device_t中的结构来定义结构体,每次访问的值都是初始值0
            这事不正确的!

    2009年4月23日 6:14

答案

  • 试试看这个声明
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEvent4Device_t {
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string device;
        
        /// int
        public int type;
        
        /// int
        public int state;
        
        /// int
        public int param;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEvent4Agent_t {
        
        /// int
        public int state;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string agent;
        
        /// char
        public byte device;
        
        /// int
        public int param;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEventServiceInitiated_t {
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string station;
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string callid;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEventOrignated_t {
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string alerting;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string caller;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calling;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calleed;
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string callid;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEventDelivered_t {
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string alerting;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string caller;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string router;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calling;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calleed;
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string callid;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEventConnectionClear_t {
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string dropDevice;
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string callid;
        
        /// int
        public int reason;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEventCallClear_t {
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string callid;
        
        /// int
        public int reason;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEventCallActive_t {
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string callid;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string actionDevice;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string heldDevice;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calling;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calleed;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEventCallHeld_t {
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string callid;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string actionDevice;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string heldDevice;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calling;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calleed;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEventCallTransfered_t {
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string newcall;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string actionDevice;
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string heldcall;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string heldDevice;
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string activecall;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string activeDevice;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calling;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calleed;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEventCallEstanblished_t {
        
        /// char[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=32)]
        public string callid;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string firstDevice;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string secondDevice;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calling;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string calleed;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)]
    public struct Anonymous_0f64d6c1_fbf4_4d4f_9fa0_1ba46122bef2 {
        
        /// mcsEventServiceInitiated_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEventServiceInitiated_t serviceInitiated;
        
        /// mcsEventOrignated_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEventOrignated_t orignated;
        
        /// mcsEventDelivered_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEventDelivered_t delivered;
        
        /// mcsEventConnectionClear_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEventConnectionClear_t connClear;
        
        /// mcsEventCallClear_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEventCallClear_t callClear;
        
        /// mcsEventCallActive_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEventCallActive_t callActive;
        
        /// mcsEventCallTransfered_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEventCallTransfered_t callTransfered;
        
        /// mcsEventCallHeld_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEventCallHeld_t callHeld;
        
        /// mcsEventCallEstanblished_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEventCallEstanblished_t callEstanb;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct mcsEvent4Call_t {
        
        /// int
        public int code;
        
        /// Anonymous_0f64d6c1_fbf4_4d4f_9fa0_1ba46122bef2
        public Anonymous_0f64d6c1_fbf4_4d4f_9fa0_1ba46122bef2 u;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct mcsEvent4Media_t {
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)]
    public struct Anonymous_1240dec0_ef4a_4d9f_ae22_0b02794610bd {
        
        /// mcsEvent4Device_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEvent4Device_t deviceEvt;
        
        /// mcsEvent4Agent_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEvent4Agent_t agentEvt;
        
        /// mcsEvent4Call_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEvent4Call_t callEvt;
        
        /// mcsEvent4Media_t
        [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
        public mcsEvent4Media_t mediaEvt;
    }
    
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct mcsEvent_t {
        
        /// int
        public int code;
        
        /// char[64]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=64)]
        public string timestamp;
        
        /// Anonymous_1240dec0_ef4a_4d9f_ae22_0b02794610bd
        public Anonymous_1240dec0_ef4a_4d9f_ae22_0b02794610bd u;
    }
    
    


    MSMVP VC++
    2009年4月23日 19:03
    版主