(筆記) 如何使用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

cs_sum00

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。

執行結果

cs_sum02

Conclusion
在C#與C/C++作interop時,最重要的就是兩個語言型別的對應,整理如下:

Unmanage type in Wtypes.hUnmanaged C typeManaged class nameManaged C# typeDescription
HANDLEvoid *System.InPtrN/A32 bits on 32-bit windows operation systems, 64 bits on 64-bit windows operation systems.
BYTEunsigned charSystem.Bytebyte8 bits
SHORTshortSystem.Int16short16 bits
WORDunsigned shortSystem.UInt16ushort16 bits
INTintSystem.Int32int32 bits
UINTunsigned intSystem.UInt32uint32 bits
LONGlongSystem.Int32int32 bits
BOOLlongSystem.Int32int32 bits
DWORDunsigned longSystem.Int32int32 bits
ULONGunsigned longSystem.Int32int32 bits
CHARcharSystem.CharcharDecorate with ANSI
LPSTRchar *System.String or System.Text.StringBuilderstringDecorate with ANSI
LPCSTRconst char *System.String or System.Text.StringBuilderstringDecorate with ANSI
LPWSTRwchar_t*System.String or System.Text.StringBuilderstringDecorate with ANSI
LPCWSTRconst wchar_t*System.String or System.Text.StringBuilderstringDecorate with ANSI
FLOATFloatSystem.Singlefloat32 bits
DUOBLEDoubleSystem.Doubledouble64 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++)

全文完。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值