C#调用C++中导出类

  C#要对数据进行封送处理,因为这涉及到托管与非托管代码的交互。

//! 导入C++ DLL内函数
        [DllImport("CPlusPlusCSharpMix.dll", EntryPoint = "Init")]
        public static extern bool Init();
        [DllImport("CPlusPlusCSharpMix.dll")]
        public static extern bool Run();
        [DllImport("CPlusPlusCSharpMix.dll")]
        public static extern bool Clean();
        
        //[DllImport("CPlusPlusCSharpMix.dll")]
        //DLL_API bool Init1(std::string str);
        [DllImport("CPlusPlusCSharpMix.dll")]
        public static extern bool Init2(int a);
        [DllImport("CPlusPlusCSharpMix.dll")]
        public static extern bool Init3(int a);
        [DllImport("CPlusPlusCSharpMix.dll")]
        public static extern bool Init4(ref int[] a);
        [DllImport("CPlusPlusCSharpMix.dll")]
        public static extern bool Init5(ref int a);
        

        const string testDLL = @"CPlusPlusCSharpMix.dll";
        [StructLayout(LayoutKind.Sequential)]
        public class MyOGRE
        {
            //该方式是直接调用C++ DLL内的类的成员函数。
            [DllImport("CPlusPlusCSharpMix.dll", EntryPoint = @"?MyOGREHello@MyOGRE@@QAEXXZ")]
            public static extern void MyOGREHello();

            [DllImport("CPlusPlusCSharpMix.dll", EntryPoint = @"?LoadLuaScript@MyOGRE@@QAE_NPAD@Z")]
            public static extern bool LoadLuaScript([MarshalAs(UnmanagedType.LPStr)]string filename);

            [DllImport("CPlusPlusCSharpMix.dll", EntryPoint = @"?SetNewParent@MyOGRE@@QAEPAUHWND__@@HH@Z")]
            public static extern int SetNewParent(int hwnd, IntPtr newparent);

            [DllImport(testDLL, EntryPoint = @"?TestEntry@MyOGRE@@QAEXPAUHWND__@@@Z")]
            public static extern void TestEntry(IntPtr hwnd);

            [DllImport(testDLL, EntryPoint = @"?Render@MyOGRE@@QAEXXZ")]
            public static extern void Render();
        }
        
        [DllImport("CPlusPlusCSharpMix.dll")]
        public static extern MyOGRE GetOGREInst();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值