博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 中静态调用C++dll 和C# 中动态调用C++dll
阅读量:4596 次
发布时间:2019-06-09

本文共 3077 字,大约阅读时间需要 10 分钟。

在最近的项目中,牵涉到项目源代码保密问题,由于代码是C#写的,容易被反编译,因此决定抽取核心算法部分使用C++编写,C++到目前为止好像还不能被很好的反编译,当然如果你是反汇编高手的话,也许还是有可能反编译。这样一来,就涉及C#托管代码与C++非托管代码互相调用,于是调查了一些资料,顺便与大家分享一下:

一. C# 中静态调用C++动态链接

 

    1. 建立VC工程CppDemo,建立的时候选择Win32 Console(dll),选择Dll。

    2. 在DllDemo.cpp文件中添加这些代码。

extern "C" __declspec(dllexport) int Add(int a,int b)
{
    
     return a+b;
}

    3. 编译工程。

    4. 建立新的C#工程,选择Console应用程序,建立测试程序InteropDemo

    5. 在Program.cs中添加引用:using System.Runtime.InteropServices;

    6. 在pulic class Program添加如下代码: 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace InteropDemo
{
    class Program
    {
        [DllImport("CppDemo.dll", EntryPoint = "Add", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern int Add(int a, int b); //DllImport请参照MSDN
        static void Main(string[] args)
        {
            Console.WriteLine(Add(1, 2));
            Console.Read();
        }
    }
}

   好了,现在您可以测试Add程序了,是不是可以在C# 中调用C++动态链接了,当然这是静态调用,需要将CppDemo编译生成的Dll放在DllDemo程序的Bin目录下

二. C# 中动态调用C++动态链接

 在第一节中,讲了静态调用C++动态链接,由于Dll路径的限制,使用的不是很方便,C#中我们经常通过配置动态的调用托管Dll,例如常用的一些设计模式:Abstract Factory, Provider, Strategy模式等等,那么是不是也可以这样动态调用C++动态链接呢?只要您还记得在C++中,通过LoadLibrary, GetProcess, FreeLibrary这几个函数是可以动态调用动态链接的(它们包含在kernel32.dll中),那么问题迎刃而解了,下面我们一步一步实验

    1.  将kernel32中的几个方法封装成本地调用类NativeMethod

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace InteropDemo
{
    public static class NativeMethod
    {
        [DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
        public static extern int LoadLibrary(
            [MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
        [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
        public static extern IntPtr GetProcAddress(int hModule,
            [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
        [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
        public static extern bool FreeLibrary(int hModule);
    }
}

 

    2. 使用NativeMethod类动态读取C++Dll,获得函数指针,并且将指针封装成C#中的委托。原因很简单,C#中已经不能使用指针了,如下          

            int hModule = NativeMethod.LoadLibrary(@"c:"CppDemo.dll");

            IntPtr intPtr = NativeMethod.GetProcAddress(hModule, "Add");

详细请参见代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace InteropDemo
{
    class Program
    {
        //[DllImport("CppDemo.dll", EntryPoint = "Add", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        //public static extern int Add(int a, int b); //DllImport请参照MSDN
        static void Main(string[] args)
        {
            //1. 动态加载C++ Dll
            int hModule = NativeMethod.LoadLibrary(@"c:\CppDemo.dll");
            if (hModule == 0) return;
            //2. 读取函数指针
            IntPtr intPtr = NativeMethod.GetProcAddress(hModule, "Add");
            //3. 将函数指针封装成委托
            Add addFunction = (Add)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(Add));
            //4. 测试
            Console.WriteLine(addFunction(1, 2));
            Console.Read();
        }
        /// <summary>
        /// 函数指针
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        delegate int Add(int a, int b);
    }
}

 

 

 

通过如上两个例子,我们可以在C#中动态或者静态的调用C++写的代码了.

转自:

 

作者:
出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/lsgsanxiao/p/11175579.html

你可能感兴趣的文章
20145202马超《JAVA》预备作业1
查看>>
[导入]参考OpenSceneGraph的3ds插件学习lib3ds
查看>>
java基础-四大特征
查看>>
linux文档查看器
查看>>
如何使用 ccs7.2调试代码
查看>>
2016.8.22 Axure两级下拉框联动的实现
查看>>
C#集合类:动态数组、队列、栈、哈希表、字典(转)
查看>>
基于bootstrap 的datatable插件的使用(php版)
查看>>
展示图片的自动和手动切换
查看>>
机器学习分类
查看>>
kvm虚拟化关闭虚拟网卡virbr0的方法
查看>>
宫廷秘方,给大家分享一下,祝大家身体健康
查看>>
2016计蒜之道复赛A 百度地图的实时路况
查看>>
How to get md5 and SHA1 in objective c (iOS sdk)
查看>>
代动词
查看>>
虚拟私有云(Virtual Private Cloud,专有网络)配置方式总结
查看>>
用Latex写学术论文: IEEE Latex模板和文档设置(\documentclass)
查看>>
JQuery和Js中,如何让ajax执行完后再继续往下执行?(已解决,示例)
查看>>
VMWare12pro安装Centos 6.9教程
查看>>
Spark笔记之使用UDF(User Define Function)
查看>>