(筆記) 如何使用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编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2009-02-13 (筆記) 如何為ModelSim加入永久性的library mapping? (SOC) (ModelSim)
2008-02-13 (原創) 如何在VC8編譯libdecodeqr? (C/C++) (VC++) (Image Processing)