在最近的項目中,牽涉到項目源代碼保密問題,因爲代碼是C#寫的,容易被反編譯,所以決定抽取核心算法部分使用C++編寫,C++到目前爲止好像還不能被很好的反編譯,固然若是你是反彙編高手的話,也許仍是有可能反編譯。這樣一來,就涉及C#託管代碼與C++非託管代碼互相調用,因而調查了一些資料,順便與你們分享一下:html
一. C# 中靜態調用C++動態連接算法
1. 創建VC工程CppDemo,創建的時候選擇Win32 Console(dll),選擇Dll。設計模式
2. 在DllDemo.cpp文件中添加這些代碼。ide
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;spa
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目錄下htm
二. 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++寫的代碼了.
轉自:http://www.cnblogs.com/Jianchidaodi/archive/2009/03/09/1407270.html