其實把舊的VB6 Code編成DLL來用,其實也蠻不錯的。
(1)首先開啟一個ActiveX DLL 專案
(2)定義好你的Class名稱
(3)寫好(貼好)程式碼
Public Function Add(ByVal A As Double, ByVal B As Double) As Double Dim sum As Double c = A + B Add = c End Function
(4)編成DLL (如果VB6裝在WIN7以上,開啟VB IDE時記得用管理者,否則編譯會出錯)
(5)在VS2015裡引用這個DLL
(6)撰寫程式碼 ,一樣會出現Func提示
(7)完整程式碼如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { VBFunc.VB_dll mVB6_func = new VBFunc.VB_dll(); double sum = mVB6_func.Add(10.5, 1.8); label1.Text = sum.ToString(); } } }
(8)執行結果
題外話
傳說中,VB6是原生的程式碼,會不會比C#快呢,
我們來試試看在C#與呼叫跟VB6 DLL的執行速度。
先來準備一個要RUN 1000000次的迴圈(執行環境為Win10 x64)
VB6 程式碼
Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Command1_Click() Dim i As Long, temp As Long, temp1 As Long temp = GetTickCount() For i = 0 To 9 test Next temp1 = GetTickCount() Form1.Print temp1 - temp & "ms" End Sub Public Function test() As Long Dim sum As Long For i = 0 To 1000000 sum = 1 + 2 Next tst = 10 End Function
VB6 執行數次,大約都在110~140ms
C#程式碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { VBFunc.VB_dll mVB6_func = new VBFunc.VB_dll(); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); /*sw.Reset(); sw.Start(); for ( int i=0;i<10;i++) { long sum = mVB6_func.test(); } sw.Stop(); string result1 = sw.Elapsed.TotalMilliseconds.ToString(); label1.Text = result1;*/ sw.Reset(); sw.Start(); for (int i = 0; i < 10; i++) { long sum = test(); } sw.Stop(); string result2 = sw.Elapsed.TotalMilliseconds.ToString(); label2.Text = result2; } private long test() { long sum = 0; for (long i = 0; i < 1000000; i++) { sum = 1 + 2; } return 10; } } }
C# 只要60ms就執行完了。
有點怪異,VB6慢好多,是不是Win10不喜歡它。
我來做個嘗試,把VB6編成DLL,讓C# 呼叫看看
如果我把剛才那段程式碼編成DLL讓C#呼叫呢
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Project1.Class1 class1 = new Project1.Class1(); System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch(); sw2.Reset(); sw2.Start(); for (int i = 0; i < 10; i++) { long sum = test(); } sw2.Stop(); string result2 = sw2.Elapsed.TotalMilliseconds.ToString(); label2.Text = "C# " + result2; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Reset(); sw.Start(); for (int i = 0; i < 10; i++) { long sum = class1.test(); } sw.Stop(); string result1 = sw.Elapsed.TotalMilliseconds.ToString(); label1.Text = "VB6 " + result1; } private long test() { long sum = 0; for (long i = 0; i < 1000000; i++) { sum = 1 + 2; } return 10; } } }
雖然執行變快了,但是VB6在執行這段程式碼,仍然比C慢上一些。
沒有留言:
張貼留言