(1)用C++builder6調(diào)用DLL
站在用戶的角度思考問題,與客戶深入溝通,找到望城網(wǎng)站設(shè)計與望城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋望城地區(qū)。用VC2015創(chuàng)建包含MFC庫的DLL,如果是給C++Builder6使用,步驟如下:
1、工程屬性==》C++==》高級==》調(diào)用約定 選項為:__stdcall (/Gd)
2、VC++2015中的函數(shù)聲明如下:
extern "C" __declspec(dllexport) VOID __stdcall CreateMeter(const char* szTypeName);
3.VC++2015的def文件,EXPORTS可以不用寫,因為C++Builder6不用到這個.lib文件
不過,我發(fā)現(xiàn)要導(dǎo)出函數(shù),需要在def字聲明,否則在其他語言中找不到函數(shù),提示錯誤.
4.在C++Builder6中,需要重新導(dǎo)出DLL的引導(dǎo)庫.lib,方法如下:
implib my.lib my.dll
在BCB6工程中引入my.lib,同時添加函數(shù)聲明,也就是VC2015DLL中的函數(shù)聲明(與第2步相同)
(2)用Delphi7調(diào)用
unit ScaleWeight_dll; interface procedure CreateMeter(xktype:PChar; No:integer; Comm:integer; baud:integer; parity:Char; databit:integer; stopbit:integer );stdcall;external 'YuComm.dll'; //獲取重量字符串 function GetWeight( buff :pchar;index :integer = 1):PChar;stdcall; external 'YuComm.dll'; //寫串口 procedure WritePort( buff :array of byte;index :integer = 0);stdcall; external 'YuComm.dll'; function ClosePort(index :integer=1):integer;stdcall;external 'YuComm.dll'; //name 'ClosePort1'; //輔助函數(shù) //緩取緩沖區(qū)數(shù)據(jù)量 function GetBuffLength(index :integer =0):integer;stdcall; external 'YuComm.dll'; //或取通信狀態(tài);true串口打開但通信中斷,false通信正常 function GetStatus(index :integer =0):boolean;stdcall;external 'YuComm.dll'; //清除緩沖區(qū)數(shù)據(jù) //procedure Clear(index :integer =0);stdcall;external 'YuComm.dll'; //獲取原始數(shù)據(jù) function GetSourceData( buff :pbyte;index :integer = 0):integer;stdcall; external 'YuComm.dll'; implementation end.
將上面文件保存為擴(kuò)展名為pas的文件,添加到delphi7工程中.
(3)在VB6中調(diào)用DLL
Private Declare Function CreateMeter Lib "YuComm"(ByVal name As String, _ ByVal No As Integer, _ ByVal port As Integer, _ ByVal baud As Integer, _ ByVal parity As Integer, _ ByVal databit As Integer, _ ByVal stopbit As Integer) As Boolean'需要添加一個返回值聲明,即使不需要 Private Declare Function GetWeight Lib "YuComm"(ByVal buff As String, Optional ByVal index As Integer = 1) As Long Private Declare Function GetStatus Lib "YuComm"(Optional ByVal index As Integer = 1) As Integer Private Declare Function ClosePort Lib "YuComm"(Optional ByVal index As Integer = 1) As Boolean Private Sub Command1_Click() Dim name As String Dim port As Integer Dim baud As Integer name = Combo1.Text port = CInt(Text1.Text) baud = CInt(Text2.Text) CreateMeter name, 1, port, baud, 110, 8, 1 End Sub Private Sub Command2_Click() ClosePort (1) End Sub Private Sub Command3_Click() End Sub Private Sub Timer1_Timer() Dim re As Boolean re = GetStatus(1) If re = True Then Label2.Caption = "串口打開成功" Else Label2.Caption = "串口打不成功" End If Dim buff1 As String buff1 = Space(100) Call GetWeight(buff1, 1) Label1.Caption = buff1 End Sub
在XP平臺下,用VB6調(diào)用,請用release方式編譯,否則VB6會提示找不到相應(yīng)的DLL文件,即所這個文件就在運(yùn)行目錄下
在VB中調(diào)用,聲明函數(shù)時應(yīng)加上返回值,即使DLL中這個函數(shù)沒有返回值.返回值的類型可任意
(4)在C#中調(diào)用DLL
public partial class Form1 : Form { //======================================================================== //1、創(chuàng)建儀表,并打開儀 [DllImport("YuComm.dll")] public static extern bool CreateMeter(string xktype,int no,int comm, int baud,int parity,int databit,int stopbit); //2、讀取儀表重量,多臺儀表時,參數(shù)為索引 [DllImport("YuComm.dll")] public static extern string GetWeight(StringBuilder weight, int index = 1); //寫串口 [DllImport("YuComm.dll")] public static extern void Write(ref byte buff,int index = 1); //3、關(guān)閉串口 [DllImport("YuComm.dll")] public static extern void ClosePort( int index=1); //ScaleWeight輔助功能函數(shù):獲取緩沖區(qū)數(shù)據(jù)個數(shù) [DllImport("YuComm.dll")] public static extern int GetBuffLength(int index = 1); //獲取通信狀態(tài):false通信正常 ,true通信中斷 [DllImport("YuComm.dll")] public static extern bool GetStatus(int index=1); //輔助功能函數(shù):讀取原始數(shù)據(jù)流 [DllImport("YuComm.dll")] public static extern int GetSourceData(ref byte buff, int index = 1); //輔助功能函數(shù):清除緩沖區(qū) //[DllImport("YuComm.dll")] //public static extern void Clear(int index=1); //=================================================================== public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { xktype = comboBox1.Text; szComm = textBox1.Text; szBaud = textBox2.Text; bool re = CreateMeter(xktype, 1, Convert.ToInt32(szComm), Convert.ToInt32(szBaud), 'n', 8, 1); if (re) { label1.Text = "串口狀態(tài):打開成功"; } else { label1.Text = "串口狀態(tài):打開不成功"; } } private void button2_Click(object sender, EventArgs e) { ClosePort(1); label1.Text = "串口狀態(tài):串口關(guān)閉"; } private void timer1_Tick(object sender, EventArgs e) { StringBuilder buff = new StringBuilder(255); GetWeight(buff,1); label2.Text = buff.ToString(); buff = null;//釋放 label5.Text = "緩沖區(qū)中數(shù)據(jù)量:"+ GetBuffLength().ToString(); bool re = GetStatus(); if (re) label4.Text = "通信狀態(tài):通信數(shù)據(jù)流正常"; else label4.Text = "通信狀態(tài):通信數(shù)據(jù)流中斷"; byte[] data = new byte[1024]; int c = GetSourceData(ref data[0],1); richTextBox1.Clear(); for (int i = 0; i < c; i++) { richTextBox1.Text += data[i].ToString("X2") +" "; } data = null; } private void button3_Click(object sender, EventArgs e) { byte[] buff = new byte[1024]; string a = "hello"; BitConverter.ToString(buff); Write(ref buff[0], 1); buff = null; } private void button4_Click(object sender, EventArgs e) { timer1.Enabled = !timer1.Enabled; if (timer1.Enabled) button4.Text = "暫停"; else button4.Text = "開始"; } }
在XP上運(yùn)行,請使用release方式編譯發(fā)布,否則調(diào)用DLL中的函數(shù)時會報錯!