C#调用C++中导出类

15 篇文章 0 订阅
7 篇文章 0 订阅

  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();
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSharp 调用C++ DLL; 参数为指针导出函数 c# Csharp调用 c++库 参数为导入和导出指针两种 包含C++ DLL源码 如fun(cont char* A,char*B) A为输入参数,B为输出参数-C# CSharp call C++ DLL lib dll function param use export and import eg: fun(cont char* A,char*B) A IN,B OUT TestDll\Debug\TestCallDll.exe .......\.....\TestCallDll.vshost.exe .......\.....\TestCallDll.vshost.exe.manifest .......\.....\TestDll.dll .......\.....\TestDll.lib .......\TestCallDll\Form1.cs .......\...........\Form1.Designer.cs .......\...........\Form1.resx .......\...........\obj\Debug\TestCallDll.csproj.FileListAbsolute.txt .......\...........\...\.....\TestCallDll.csproj.GenerateResource.Cache .......\...........\...\.....\TestCallDll.exe .......\...........\...\.....\TestCallDll.Form1.resources .......\...........\...\.....\TestCallDll.pdb .......\...........\...\.....\TestCallDll.Properties.Resources.resources .......\...........\Program.cs .......\...........\...perties\AssemblyInfo.cs .......\...........\..........\Resources.Designer.cs .......\...........\..........\Resources.resx .......\...........\..........\Settings.Designer.cs .......\...........\..........\Settings.settings .......\...........\TestCallDll.csproj .......\....Dll\dllmain.cpp .......\.......\ReadMe.txt .......\.......\stdafx.cpp .......\.......\stdafx.h .......\.......\targetver.h .......\.......\TestDll.cpp .......\.......\TestDll.def .......\.......\TestDll.h .......\.......\TestDll.vcproj .......\.......\TestDll.vcproj.PC-201008261742.Administrator.user .......\TestDll.sln .......\TestDll.suo .......\....CallDll\obj\Debug\TempPE .......\...........\...\Debug .......\...........\obj .......\...........\Properties .......\Debug .......\TestCallDll .......\TestDll TestDll

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值