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

歡迎訪問 生活随笔!

生活随笔

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

C#

C#调用C++的dll文件方法

發布時間:2023/12/18 C# 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#调用C++的dll文件方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??

???????????

首先我們介紹一下原理:

C#與C++的轉換會經過以下幾層轉換?

1. C# APP

2. C#接口類:C#數據 -> 純C數據結果包裝

3. C接口包裝:純C數據 -> C++輸入

4. C++實現:C++處理


接下來我們介紹一下具體實現方法:

首先建立一個C#項目


文件--添加新建項目--Win32 控制臺應用程序--AdderImpl


源文件--右鍵添加cpp文件

[cpp] view plaincopyprint?
  • //?the?code?is?write?in?c??
  • #ifdef?__cplusplus??
  • extern?"C"{??
  • #endif??
  • ??
  • ????__declspec(dllexport)?int?__cdecl?add(int?a,?int?b);??
  • ??
  • ??
  • #ifdef?__cplusplus??
  • }??
  • #endif??
  • ??
  • int?add(int?a,?int?b)??
  • {??
  • ????return?a?+?b;??
  • }??
  • // the code is write in c #ifdef __cplusplus extern "C"{ #endif__declspec(dllexport) int __cdecl add(int a, int b);#ifdef __cplusplus } #endifint add(int a, int b) {return a + b; }

    AdderImpl項目修改配置類型為動態庫(.dll)


    AdderImpl右鍵--生成


    把該dll文件復制到



    在testApp2右鍵--添加新建項--C#類--AdderWapper.cs


    其中AdderWapper.cs中代碼為:

    [csharp] view plaincopyprint?
  • using?System;??
  • using?System.Collections.Generic;??
  • using?System.Linq;??
  • using?System.Text;??
  • using?System.Threading.Tasks;??
  • using?System.Runtime.InteropServices;??
  • ??
  • namespace?TestApp??
  • {??
  • ????class?AdderWapper??
  • ????{??
  • ????????[DllImport("AdderImpl.dll",?CallingConvention?=?CallingConvention.Cdecl)]??
  • ????????static?extern?private?int?add(int?a,?int?b);??
  • ??
  • ????????static?public?int?performAdd(int?a,?int?b)??
  • ????????{??
  • ????????????//?convert?c#?data?to?c?data??
  • ????????????//?TODO:??
  • ??
  • ??
  • ????????????//?call?the?c?interface??
  • ????????????int?ret?=?add(a,?b);??
  • ??
  • ????????????//?convert?result?from?c?data?to?c#?data??
  • ????????????//?TODO:??
  • ??
  • ????????????//?return?the?result??
  • ????????????return?ret;??
  • ????????}??
  • ????}??
  • }??
  • using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices;namespace TestApp {class AdderWapper{[DllImport("AdderImpl.dll", CallingConvention = CallingConvention.Cdecl)]static extern private int add(int a, int b);static public int performAdd(int a, int b){// convert c# data to c data// TODO:// call the c interfaceint ret = add(a, b);// convert result from c data to c# data// TODO:// return the resultreturn ret;}} }
    Program.cs中代碼為:

    [csharp] view plaincopyprint?
  • using?System;??
  • using?System.Collections.Generic;??
  • using?System.Linq;??
  • using?System.Text;??
  • using?System.Threading.Tasks;??
  • ??
  • ??
  • namespace?TestApp??
  • {??
  • ????class?Program??
  • ????{??
  • ????????static?void?Main(string[]?args)??
  • ????????{??
  • ??
  • ????????????int?a?=?3;?int?b?=?5;??
  • ????????????int?c?=?AdderWapper.performAdd(a,?b);??
  • ??
  • ????????????System.Console.WriteLine(c);??
  • ????????}??
  • ????}??
  • }??
  • using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace TestApp {class Program{static void Main(string[] args){int a = 3; int b = 5;int c = AdderWapper.performAdd(a, b);System.Console.WriteLine(c);}} }
    生成,也就是讓dll和exe在同一文件夾下


    命令行運行exe文件



    end

    

    總結

    以上是生活随笔為你收集整理的C#调用C++的dll文件方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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