顯示具有 VB6 標籤的文章。 顯示所有文章
顯示具有 VB6 標籤的文章。 顯示所有文章

2016年12月25日 星期日

[VB6] 把VB6的程式碼包成DLL,讓C#呼叫

如果有些舊的VB6 的Code不想用人腦翻譯機把它翻成C#,

其實把舊的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慢上一些。



















2015年5月5日 星期二

[VB6] 輸出資料到RS232

VB6 輸出資料到RS232


Open "COM1:9600,N,8,1,CD0,CS0,DS0,OP0,RS" For Output As #1

Print #1, Chr(27) + Chr(67) + Chr(13)

Close #1




上述的程式碼,就是對 posiflex pp2000  下全切紙命令,因此刀子會動,有聲音很明顯。

COM1後面指的是要對那一個Com 埠做傳輸


9600 為 Baud Rate , 每秒鐘多少位元bits per second (bit/s)


傳輸的時候,一般用 n, 8, 1, 即 n parity bit, 8 data bits, 1 stop bit

所以一個 byte 傳輸時,實際是 1 start bit + 8 data bits + 1 stop bit = 10 bits


CD0 的意思為等待多少亳秒做資料俞時的檢測


cd = # of ms to wait for the time-out on data carrier detect (DCD)line



RS 抑制檢測請求發送(RTS)信號

rs = suppress detection of request to send(RTS) signal




不過這個方法有時候會遇到,找不到檔案,或是機器完全不理你的情況。

誠心建議改用

MSComm 元件

專案->設定使用元件->瀏覽



































































選擇MSComm32.Ocx ->開啟






















勾選Microsoft Comm Control 6.0 ->確定






























工具箱會出現一個像電話的圖案,把它加入到Form裡


































































'先將預設的 COM 關掉
If (MSComm1.PortOpen = True) Then MSComm1.PortOpen = False
     
MSComm1.CommPort = Right(SalePrinter, 1)
'連線速度 9600 baud、無同位檢查、資料位元 8、停止位元 1
MSComm1.Settings = "9600,N,8,1"
'告訴控制項當使用 Input 時,讀取整個暫存區
MSComm1.InputLen = 0
     
   
'開啟序列連接埠
If (MSComm1.PortOpen = False) Then MSComm1.PortOpen = True


MSComm1.Output="輸出內容"

MSComm1.PortOpen = False '關閉連接

參考資料

http://eva54185418.blogspot.tw/2013/01/arduino-serial-port.html
http://www.freebasic.net/forum/viewtopic.php?t=9336

http://wukm.blogspot.tw/2007/05/mscomm-rs232.html




2015年4月9日 星期四

[VB6] 以指令編譯VB6 專案

之前修改老老的VB6 專案時,出現一個問題,VB6 IDE 在編譯EXE時會自己跳掉,

連續幾次都一樣,尋求強者肥佳洛的幫忙後,找到了一個指令。

切換到VB.exe的目錄下

C:\Program Files\Microsoft Visual Studio\VB98>cd "\Program Files\Microsoft Visual Studio\VB98"

執行 (myProject為專案的名稱

VB6.exe /make myProject

之後來檢查一下工作管理員,單核心CPU很忙碌的在編譯。




























看一下處理程序,等到C2.EXE 與VB6.EXE消失後,就代表編譯成功了。