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);
?