日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#托管代码与C++非托管代码互相调用一(C#调用C++代码.net 代码安全)

發布時間:2023/12/20 C# 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#托管代码与C++非托管代码互相调用一(C#调用C++代码.net 代码安全) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在最近的項目中,牽涉到項目源代碼保密問題,由于代碼是C#寫的,容易被反編譯,因此決定抽取核心算法部分使用C++編寫,C++到目前為止好像還不能被很好的反編譯,當然如果你是反匯編高手的話,也許還是有可能反編譯。這樣一來,就涉及C#托管代碼與C++非托管代碼互相調用,于是調查了一些資料,順便與大家分享一下:源代碼下載

一. C# 中靜態調用C++動態鏈接

?

??? 1. 建立VC工程CppDemo,建立的時候選擇Win32 Console(dll),選擇Dll。

??? 2. 在DllDemo.cpp文件中添加這些代碼。

Code
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添加如下代碼:?

?

Code
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

Code
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");

詳細請參見代碼

Code
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/zhuawang/archive/2011/12/13/2285788.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的C#托管代码与C++非托管代码互相调用一(C#调用C++代码.net 代码安全)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。