欧美一区2区三区4区公司二百,国产精品婷婷午夜在线观看,自拍偷拍亚洲精品,国产美女诱惑一区二区

C#調用

1、最近做項目遇到,C#調用C++dll里的函數需要傳遞結構體參數,發現這個并不是簡單的在C#里定義相應的結構體就可以的,下面以一個例子來說明解決的辦法,C++中的函數和結構體如下:

uint msec_set_igr_gen_cfg(int port, IGR_GEN_T *igr_gen)
{

return 0;
}

typedef struct {
? int aa_disable; /*/< authentiation adjust checking disable */
? int badtag_rej; /*/< reject packet if it is bypassed due to badtag */
? int pad_en; /*/< pad non-rejected packets up to 64B */
? int byp_ctl_sl; /*/< bypass packet if SL field does not correspond to packet len */
? int byp_ctl_v; /*/< bypass packet if V bit is set */
? int byp_ctl_sc; /*/< bypass packet if SC bit and either ES or SCB bits are set */
? int byp_ctl_ec; /*/< bypass packet if DC bits are not 00 or 11 */
? int sectag_flag; /*/< select which flag bit indicates that a SEC tag was present in pkt */
} IGR_GEN_T;

在C#中 首先需要使用Dllimport將相應的C++dll load進來,然后定義相應的結構體,具體如下:

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

??

? ? ? ? 結構體定義:

? ? [StructLayout(LayoutKind.Sequential)]
? ? ?public class IGR_GEN_T?
? ? ?{
? ? ? ? ?int aa_disable; /*/< authentiation adjust checking disable */
? ? ? ? ?int badtag_rej; /*/< reject packet if it is bypassed due to badtag */
? ? ? ? ?int pad_en; /*/< pad non-rejected packets up to 64B */
? ? ? ? ?int byp_ctl_sl; /*/< bypass packet if SL field does not correspond to packet len */
? ? ? ? ?int byp_ctl_v; /*/< bypass packet if V bit is set */
? ? ? ? ?int byp_ctl_sc; /*/< bypass packet if SC bit and either ES or SCB bits are set */
? ? ? ? ?int byp_ctl_ec; /*/< bypass packet if DC bits are not 00 or 11 */
? ? ? ? ?int sectag_flag; /*/< select which flag bit indicates that a SEC tag was present in pkt */
? ? ? ? ?public IGR_GEN_T()
? ? ? ? ?{
? ? ? ? ? ? ?aa_disable = 0;
? ? ? ? ? ? ?badtag_rej = 0;
? ? ? ? ? ? ?pad_en = 0;
? ? ? ? ? ? ?byp_ctl_ec = 0;
? ? ? ? ? ? ?byp_ctl_sc = 0;
? ? ? ? ? ? ?byp_ctl_sl = 0;
? ? ? ? ? ? ?byp_ctl_v = 0;
? ? ? ? ? ? ?sectag_flag = 0;
? ? ? ? ?}
? ? ?} ;

? 在代碼中具體引用函數時如下所示,

? ? ?IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(igr_gen));
? ? ? ? ? ? ?Marshal.StructureToPtr(igr_gen, ptr, false);
? ? ? ? ? ? ?UInt32 ret = _msec_set_igr_gen_cfg(port, ptr);
? ? ? ? ? ? ?igr_gen = (IGR_GEN_T)Marshal.PtrToStructure(ptr, typeof(IGR_GEN_T));
? ? ? ? ? ? ?Marshal.FreeHGlobal(ptr);
? ? ? ? ? ? ?return ret;

從以上步驟可以看出,結構體參數的傳遞需要marshal做輔助做相應的轉化,以intptr的方式傳輸結構體參數。

文章鏈接: http://m.qzkangyuan.com/21894.html

文章標題:C#調用

文章版權:夢飛科技所發布的內容,部分為原創文章,轉載請注明來源,網絡轉載文章如有侵權請聯系我們!

聲明:本站所有文章,如無特殊說明或標注,均為本站原創發布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發布本站內容到任何網站、書籍等各類媒體平臺。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
建站教程

c++ c#結構體調用

2023-7-7 14:39:48

建站教程

c++ c#結構體調用

2023-7-7 14:45:58

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優惠劵
今日簽到
有新私信 私信列表
搜索
主站蜘蛛池模板: 子长县| 赣州市| 乌审旗| 吉安县| 武汉市| 霍林郭勒市| 秭归县| 手游| 阿拉善左旗| 都昌县| 弥渡县| 同仁县| 丽江市| 西华县| 威远县| 奉化市| 孝感市| 苏尼特左旗| 旬邑县| 泸西县| 湛江市| 秦皇岛市| 疏附县| 宣城市| 元朗区| 日照市| 治多县| 旌德县| 崇仁县| 铜梁县| 墨脱县| 乌兰县| 南漳县| 宝丰县| 襄城县| 白朗县| 犍为县| 当涂县| 合肥市| 万荣县| 陇川县|