VC2015編寫的DLL給其他語(yǔ)言調(diào)用-創(chuàng)新互聯(lián)

(1)用C++builder6調(diào)用DLL

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到望城網(wǎng)站設(shè)計(jì)與望城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)頁(yè)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋望城地區(qū)。

用VC2015創(chuàng)建包含MFC庫(kù)的DLL,如果是給C++Builder6使用,步驟如下:
1、工程屬性==》C++==》高級(jí)==》調(diào)用約定  選項(xiàng)為:__stdcall (/Gd)

2、VC++2015中的函數(shù)聲明如下:
   extern "C" __declspec(dllexport)  VOID __stdcall CreateMeter(const char* szTypeName);

3.VC++2015的def文件,EXPORTS可以不用寫,因?yàn)镃++Builder6不用到這個(gè).lib文件
  不過(guò),我發(fā)現(xiàn)要導(dǎo)出函數(shù),需要在def字聲明,否則在其他語(yǔ)言中找不到函數(shù),提示錯(cuò)誤.

4.在C++Builder6中,需要重新導(dǎo)出DLL的引導(dǎo)庫(kù).lib,方法如下:
  implib my.lib my.dll

在BCB6工程中引入my.lib,同時(shí)添加函數(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'需要添加一個(gè)返回值聲明,即使不需要
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平臺(tái)下,用VB6調(diào)用,請(qǐng)用release方式編譯,否則VB6會(huì)提示找不到相應(yīng)的DLL文件,即所這個(gè)文件就在運(yùn)行目錄下

在VB中調(diào)用,聲明函數(shù)時(shí)應(yīng)加上返回值,即使DLL中這個(gè)函數(shù)沒(méi)有返回值.返回值的類型可任意

(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、讀取儀表重量,多臺(tái)儀表時(shí),參數(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ù)個(gè)數(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)行,請(qǐng)使用release方式編譯發(fā)布,否則調(diào)用DLL中的函數(shù)時(shí)會(huì)報(bào)錯(cuò)!

文章名稱:VC2015編寫的DLL給其他語(yǔ)言調(diào)用-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://www.muchs.cn/article14/dpjjde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、App開發(fā)網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)