(筆記) 如何使用C#使用Win32 DLL? (.NET) (C#) (Windows Form)
2011-02-13 21:15
567 查看
Abstract
由於C#的乾淨俐落與Visual Studio工具的增強,越來越多人選擇用C#開發GUI而捨棄MFC,但以前已經有太多程式使用Visual C++ 6.0與MFC開發,一時之間又不可能將C/C++的code全部用C#改寫,所以將原本用C/C++寫的Business Rule整理成DLL給C#使用也是個不錯的選擇。
Introduction
使用環境:Visual Studio 2008
Step 1:
使用Visual Studio 2008建立一個Project,並使用以下的GUI
Step 2:
使用(筆記) 如何使用Visual C++ 6.0開發Win32 DLL? (C/C++)所寫的Win32 DLL
Step 3:
Form1.cs/ C#
9行
使用pinvoke (plateform invoke)的方式使用Win32 DLL時,必須使用System.Runtime.InteropServices這個namespace。
17行
在(筆記) 如何使用Visual C++ 6.0開發Win32 DLL? (C/C++),我們已經將所開發的win32_sum.dll放到c:\windows目錄下,Win32 DLL不像COM DLL一樣需要註冊,只要放在c:\windows下即可。
在此宣告sum()這個function,使用DLLImport這個attribute告訴C#到win32_sum.dll去找這個sum() function,此外,C#規定pivoke的function都必須要是static extern。
20行
實際使用此pinvoke function。
執行結果
Conclusion
在C#與C/C++作interop時,最重要的就是兩個語言型別的對應,整理如下:
完整程式碼下載
pinvoke_win32_sum.7z
Reference
MSDN Plateform Invoke Data Types
MSDN Built-In Types Table (C# Reference)
See Also
(筆記) 如何使用Visual C++ 6.0開發Win32 DLL? (C/C++)
全文完。
由於C#的乾淨俐落與Visual Studio工具的增強,越來越多人選擇用C#開發GUI而捨棄MFC,但以前已經有太多程式使用Visual C++ 6.0與MFC開發,一時之間又不可能將C/C++的code全部用C#改寫,所以將原本用C/C++寫的Business Rule整理成DLL給C#使用也是個不錯的選擇。
Introduction
使用環境:Visual Studio 2008
Step 1:
使用Visual Studio 2008建立一個Project,並使用以下的GUI
Step 2:
使用(筆記) 如何使用Visual C++ 6.0開發Win32 DLL? (C/C++)所寫的Win32 DLL
Step 3:
Form1.cs/ C#
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Runtime.InteropServices; 10 11 namespace pinvoke_win32_sum { 12 public partial class Form1 : Form { 13 public Form1() { 14 InitializeComponent(); 15 } 16 17 [DllImport ("win32_sum.dll")] 18 private static extern int sum(int x, int y); 19 private void button1_Click(object sender, EventArgs e) { 20 textBox3.Text = sum(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)).ToString(); 21 } 22 } 23 }
9行
using System.Runtime.InteropServices;
使用pinvoke (plateform invoke)的方式使用Win32 DLL時,必須使用System.Runtime.InteropServices這個namespace。
17行
[DllImport ("win32_sum.dll")] private static extern int sum(int x, int y);
在(筆記) 如何使用Visual C++ 6.0開發Win32 DLL? (C/C++),我們已經將所開發的win32_sum.dll放到c:\windows目錄下,Win32 DLL不像COM DLL一樣需要註冊,只要放在c:\windows下即可。
在此宣告sum()這個function,使用DLLImport這個attribute告訴C#到win32_sum.dll去找這個sum() function,此外,C#規定pivoke的function都必須要是static extern。
20行
textBox3.Text = sum(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)).ToString();
實際使用此pinvoke function。
執行結果
Conclusion
在C#與C/C++作interop時,最重要的就是兩個語言型別的對應,整理如下:
Unmanage type in Wtypes.h | Unmanaged C type | Managed class name | Managed C# type | Description |
HANDLE | void * | System.InPtr | N/A | 32 bits on 32-bit windows operation systems, 64 bits on 64-bit windows operation systems. |
BYTE | unsigned char | System.Byte | byte | 8 bits |
SHORT | short | System.Int16 | short | 16 bits |
WORD | unsigned short | System.UInt16 | ushort | 16 bits |
INT | int | System.Int32 | int | 32 bits |
UINT | unsigned int | System.UInt32 | uint | 32 bits |
LONG | long | System.Int32 | int | 32 bits |
BOOL | long | System.Int32 | int | 32 bits |
DWORD | unsigned long | System.Int32 | int | 32 bits |
ULONG | unsigned long | System.Int32 | int | 32 bits |
CHAR | char | System.Char | char | Decorate with ANSI |
LPSTR | char * | System.String or System.Text.StringBuilder | string | Decorate with ANSI |
LPCSTR | const char * | System.String or System.Text.StringBuilder | string | Decorate with ANSI |
LPWSTR | wchar_t* | System.String or System.Text.StringBuilder | string | Decorate with ANSI |
LPCWSTR | const wchar_t* | System.String or System.Text.StringBuilder | string | Decorate with ANSI |
FLOAT | Float | System.Single | float | 32 bits |
DUOBLE | Double | System.Double | double | 64 bits |
pinvoke_win32_sum.7z
Reference
MSDN Plateform Invoke Data Types
MSDN Built-In Types Table (C# Reference)
See Also
(筆記) 如何使用Visual C++ 6.0開發Win32 DLL? (C/C++)
全文完。
相关文章推荐
- Windows Form(.NET,C#)程序使用MFC控件(treeview)DLL,treeview上面的上下左右键不工作
- 求教C#使用(SnmpSharpNet.dll)Snmp协议,如何获取整个局域网段内的IP设备类型(例PC、路由器等)MAC和主机名?
- (筆記) 如何使用Visual C++ 6.0開發Win32 DLL? (C/C++) (VC++)
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- asp.net c#里如何使用 varchar(max) 参数
- 如何使用vs2008 C#程序联合调试C++dll
- 如何使用 Visual C# .NET 处理 Excel 事件
- 如何在VS.Net中使用C#语言调用CHM帮助文件
- 如何使用 Visual C# .NET 检查 Windows 版本[转]
- 如何在C#中使用 Win32和其他库
- 如何在C#中使用Win32和其他库之三
- 如何在C#中使用 Win32和其他库
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页
- 如何在 Visual C# .NET 中使用 SqlDataAdapter 对象更新 SQL Server 数据库
- 如何在C#中使用 Win32和其他库
- C# .NET中如何使用GetCursorPos函数
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- 如何使用 Visual C# .NET 创建用于窗体身份验证的密钥
- 如何高效使用SQLITE .NET (C#)
- 如何验证 Active Directory 使用表单身份验证和 Visual C#.NET