C#托管代码与C++非托管代码互相调用

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

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

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

2. 在DllDemo.cpp文件中添加这些代码。
  1. extern "C" __declspec(dllexport) int Add(int a,int b)
  2. {
  3.     
  4.     return a+b;
  5. }
复制代码
3. 编译工程。

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

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

6. 在pulic class Program添加如下代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;

  5. namespace InteropDemo
  6. {
  7.     class Program
  8.     {
  9.         [DllImport("CppDemo.dll", EntryPoint = "Add", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  10.         public static extern int Add(int a, int b); //DllImport请参照MSDN

  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine(Add(1, 2));
  14.             Console.Read();
  15.         }
  16.     }
  17. }
复制代码
好了,现在您可以测试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
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;

  5. namespace InteropDemo
  6. {
  7.     public static class NativeMethod
  8.     {
  9.         [DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
  10.         public static extern int LoadLibrary(
  11.             [MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

  12.         [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
  13.         public static extern IntPtr GetProcAddress(int hModule,
  14.             [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

  15.         [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
  16.         public static extern bool FreeLibrary(int hModule);
  17.     }
  18. }
复制代码
2. 使用NativeMethod类动态读取C++Dll,获得函数指针,并且将指针封装成C#中的委托。原因很简单,C#中已经不能使用指针了,如下
  1.             int hModule = NativeMethod.LoadLibrary(@"c:"CppDemo.dll");


  2.             IntPtr intPtr = NativeMethod.GetProcAddress(hModule, "Add");
复制代码
详细请参见代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;

  5. namespace InteropDemo
  6. {
  7.     class Program
  8.     {
  9.         //[DllImport("CppDemo.dll", EntryPoint = "Add", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  10.         //public static extern int Add(int a, int b); //DllImport请参照MSDN


  11.         static void Main(string[] args)
  12.         {
  13.             //1. 动态加载C++ Dll
  14.             int hModule = NativeMethod.LoadLibrary(@"c:\CppDemo.dll");
  15.             if (hModule == 0) return;

  16.             //2. 读取函数指针
  17.             IntPtr intPtr = NativeMethod.GetProcAddress(hModule, "Add");

  18.             //3. 将函数指针封装成委托
  19.             Add addFunction = (Add)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(Add));

  20.             //4. 测试
  21.             Console.WriteLine(addFunction(1, 2));
  22.             Console.Read();
  23.         }

  24.         /// <summary>
  25.         /// 函数指针
  26.         /// </summary>
  27.         /// <param name="a"></param>
  28.         /// <param name="b"></param>
  29.         /// <returns></returns>
  30.         delegate int Add(int a, int b);

  31.     }
  32. }
复制代码
通过如上两个例子,我们可以在C#中动态或者静态的调用C++写的代码了。(文/ Jianchidaodi

源码下载:  ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值