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++)
全文完。