c++ c#结构体调用

2、还存在另外一种情况,是结构体中嵌套有结构体时需要做一些特殊处理,具体如下:

结构体

typedef struct {
  int ctx_num; /*/< Index to the context to use */
  int sec_level; /* < Security Level to validate frames per context based */
  int drop_maxpn; /* < Enable packet drop when max pn is reached for this context>*/
  int drop;    /*/< Drop this Packet */
  int redir;   /*/< For Egress, redirect the packet to ingress path (NDL). For Ingress, redirect the packet to alternate destination. */
  int auth_en; /*/< Encapsulate and authenticate this packet. */
  int enc_en;  /*/< Encrypt this packet. auth_en must also be set when this bit is set. (Valid only for egress path). */
} ACT_FLD;

typedef struct {
  ACT_FLD   *lk_act_fld;   /*/< Action to take for an entry within a port */
} LKUP_T;

C++ 函数

uint  msec_port_set_egr_entry (IN int port, IN int ent_num, IN LKUP_T *egr_lkup)

{

//  

}

C#在调用时首先将相应dll import进来,进行相应结构体的定义和相应函数的声明,具体如下:

        [DllImport("..\\debug\\mgd_MacSec.dll")]
        private static extern UInt32 msec_set_igr_gen_cfg(int port, IntPtr igr_gen);

    [StructLayout(LayoutKind.Sequential)]
    public class ACT_FLD 
    {
        public int ctx_num; /*/< Index to the context to use */
        public int sec_level; /* < Security Level to validate frames per context based */
        public int drop_maxpn; /* < Enable packet drop when max pn is reached for this context>*/
        public int drop;    /*/< Drop this Packet */
        public int redir;   /*/< For Egress, redirect the packet to ingress path (NDL). For Ingress, redirect the packet to alternate destination. */
        public int auth_en; /*/< Encapsulate and authenticate this packet. */
        public int enc_en;  /*/< Encrypt this packet. auth_en must also be set when this bit is set. (Valid only for egress path). */
        public ACT_FLD()
        {
            ctx_num = 0;
            sec_level = 0;
            drop_maxpn = 0;
            drop = 0;
            redir = 0;
            auth_en = 0;
            enc_en = 0;
        }
    } 

    [StructLayout(LayoutKind.Sequential)]
    public class LKUP_T
    {
        public IntPtr lk_act_fld;

        public LKUP_T()
        {
            ACT_FLD lk_act_fld_s = new ACT_FLD();
            lk_act_fld = Marshal.AllocHGlobal(Marshal.SizeOf(lk_act_fld_s));
            Marshal.StructureToPtr(lk_act_fld_s, lk_act_fld, false);
        }
    } 

    具体在代码中引用时如下所示:

            IntPtr egr_lkup_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(egr_lkup));
            Marshal.StructureToPtr(egr_lkup, egr_lkup_ptr, false);
            uint ret = _msec_port_set_egr_entry(port, ent_num, egr_lkup_ptr);
            egr_lkup = (LKUP_T)Marshal.PtrToStructure(egr_lkup_ptr, typeof(LKUP_T));
            Marshal.FreeHGlobal(egr_lkup_ptr);

 

文章链接: https://www.mfisp.com/21895.html

文章标题:c++ c#结构体调用

文章版权:梦飞科技所发布的内容,部分为原创文章,转载请注明来源,网络转载文章如有侵权请联系我们!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
建站教程

C#调用

2023-7-7 14:44:32

建站教程

JAVA数据结构

2023-7-11 13:54:03

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索

梦飞科技 - 最新云主机促销服务器租用优惠