(筆記) 如何使用C#使用Win32 DLL? (.NET) (C#) (Windows Form)
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#
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++)
全文完。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如果单表数据量大,只能考虑分库分表吗?
· 一文彻底搞懂 MCP:AI 大模型的标准化工具箱
· 电商平台中订单未支付过期如何实现自动关单?
· 用 .NET NativeAOT 构建完全 distroless 的静态链接应用
· 为什么构造函数需要尽可能的简单
· 短信接口被刷爆:我用Nginx临时止血
· .NET 平台上的开源模型训练与推理进展
· Google发布A2A开源协议:“MCP+A2A”成未来标配?
· C# 多项目打包时如何将项目引用转为包依赖
· 一款让 Everything 更加如虎添翼的 .NET 开源辅助工具!
2009-02-13 (筆記) 如何為ModelSim加入永久性的library mapping? (SOC) (ModelSim)
2008-02-13 (原創) 如何在VC8編譯libdecodeqr? (C/C++) (VC++) (Image Processing)