国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C#調(diào)用C++編寫的DLL函數(shù), 以及各種類型的參數(shù)傳遞 (轉(zhuǎn)載)

2019-11-06 06:07:28
字體:
供稿:網(wǎng)友

C#調(diào)用C++編寫的DLL函數(shù), 以及各種類型的參數(shù)傳遞  

1. 如果函數(shù)只有傳入?yún)?shù),比如:

C/C++ Code Copy Code To Clipboard//C++中的輸出函數(shù)int __declspec(dllexport) test(const int N){return N+10;}

對應(yīng)的C#代碼為:

C# Code Copy Code To Clipboard[DllImport("test.dll", EntryPoint = "#1")]public static extern int test(int m); PRivate void button1_Click(object sender, EventArgs e){textBox1.Text= test(10).ToString();}

2. 如果函數(shù)有傳出參數(shù),比如:

C/C++ Code Copy Code To Clipboard//C++void __declspec(dllexport) test(const int N, int& Z){Z=N+10;}

對應(yīng)的C#代碼:

C# Code Copy Code To Clipboard[DllImport("test.dll", EntryPoint = "#1")]public static extern double test(int m, ref int n); private void button1_Click(object sender, EventArgs e){int N = 0;test1(10, ref N);textBox1.Text= N.ToString();}

3. 帶傳入數(shù)組:

C/C++ Code Copy Code To Clipboardvoid __declspec(dllexport) test(const int N, const int n[], int& Z){for (int i=0; i<N; i++){Z+=n[i];}}

C#代碼:

C# Code Copy Code To Clipboard[DllImport("test.dll", EntryPoint = "#1")]public static extern double test(int N, int[] n, ref int Z); private void button1_Click(object sender, EventArgs e){int N = 0;int[] n;n = new int[10];for (int i = 0; i < 10; i++){n[i] = i;}test(n.Length, n, ref N);textBox1.Text= N.ToString();}

4. 帶傳出數(shù)組:

C++不能直接傳出數(shù)組,只傳出數(shù)組指針,

C/C++ Code Copy Code To Clipboardvoid __declspec(dllexport) test(const int M, const int n[], int *N){for (int i=0; i<M; i++){N[i]=n[i]+10;}}

對應(yīng)的C#代碼:

C# Code Copy Code To Clipboard[DllImport("test.dll", EntryPoint = "#1")]public static extern void test(int N, int[] n, [MarshalAs(UnmanagedType.LPArray,SizeParamIndex=1)] int[] Z); private void button1_Click(object sender, EventArgs e){int N = 1000;int[] n, Z;n = new int[N];Z = new int[N];for (int i = 0; i < N; i++){n[i] = i;}test(n.Length, n, Z);for (int i=0; i<Z.Length; i++){textBox1.AppendText(Z[i].ToString()+"n");}}

這里聲明函數(shù)入口時,注意這句 [MarshalAs(UnmanagedType.LPArray,SizeParamIndex=1)] int[] Z

在C#中數(shù)組是直接使用的,而在C++中返回的是數(shù)組的指針,這句用來轉(zhuǎn)化這兩種不同的類型.

關(guān)于MarshalAs的參數(shù)用法以及數(shù)組的Marshaling,可以參見這篇轉(zhuǎn)帖的文章: http://www.kycis.com/blog/read.php?21

5. 傳出字符數(shù)組:

C++定義:

C/C++ Code Copy Code To Clipboardvoid __declspec(dllexport) test(int i, double &a, double &b, char t[5])  

C#對應(yīng)聲明:

C# Code Copy Code To Clipboard[DllImport("dll.dll", EntryPoint = "test")]  public static extern void test(int i, ref double a, ref double b, [Out, MarshalAs(UnmanagedType.LPArray)] char[] t);   。。。              char[] t = new char[5];              test(i, ref a, ref b, t);  

字符數(shù)組的傳遞基本與4相似,只是mashalAs 時前面加上Out。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 乌审旗| 维西| 九台市| 棋牌| 左权县| 达州市| 锡林郭勒盟| 辽阳县| 桃园市| 兴城市| 龙门县| 若羌县| 建湖县| 锡林浩特市| 汝城县| 内江市| 衢州市| 策勒县| 游戏| 老河口市| 承德县| 米易县| 张家港市| 榆中县| 禄劝| 峨边| 安庆市| 莱芜市| 奉新县| 吴川市| 阿克苏市| 平谷区| 民丰县| 修水县| 黄浦区| 华坪县| 衡水市| 句容市| 买车| 富平县| 宁蒗|