天天看點

VC2015編寫的DLL給其他語言調用

(1)用C++builder6調用DLL

用VC2015建立包含MFC庫的DLL,如果是給C++Builder6使用,步驟如下:

1、工程屬性==》C++==》進階==》調用約定  選項為:__stdcall (/Gd)

2、VC++2015中的函數聲明如下:

   extern "C" __declspec(dllexport)  VOID __stdcall CreateMeter(const char* szTypeName);

3.VC++2015的def檔案,EXPORTS可以不用寫,因為C++Builder6不用到這個.lib檔案

  不過,我發現要導出函數,需要在def字聲明,否則在其他語言中找不到函數,提示錯誤.

4.在C++Builder6中,需要重新導出DLL的引導庫.lib,方法如下:

  implib my.lib my.dll

  在BCB6工程中引入my.lib,同時添加函數聲明,也就是VC2015DLL中的函數聲明(與第2步相同)

(2)用Delphi7調用

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';

       //輔助函數
       //緩取緩沖區資料量
       function GetBuffLength(index :integer =0):integer;stdcall;
       external 'YuComm.dll';
       //或取通信狀态;true序列槽打開但通信中斷,false通信正常
       function GetStatus(index :integer =0):boolean;stdcall;external 'YuComm.dll';
       //清除緩沖區資料
       //procedure Clear(index :integer =0);stdcall;external 'YuComm.dll';
       //擷取原始資料
       function  GetSourceData( buff :pbyte;index :integer = 0):integer;stdcall;
       external 'YuComm.dll';
       
implementation

end.      

将上面檔案儲存為擴充名為pas的檔案,添加到delphi7工程中.

(3)在VB6中調用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調用,請用release方式編譯,否則VB6會提示找不到相應的DLL檔案,即所這個檔案就在運作目錄下

public partial class Form1 : Form
    {
        //========================================================================
        //1、建立儀表,并打開儀
        [DllImport("YuComm.dll")]
        public static extern bool CreateMeter(string xktype,int no,int comm,
        int baud,int parity,int databit,int stopbit);
        //2、讀取儀表重量,多台儀表時,參數為索引
        [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、關閉序列槽
        [DllImport("YuComm.dll")]
        public static extern void ClosePort( int index=1);
    


        //ScaleWeight輔助功能函數:擷取緩沖區資料個數
        [DllImport("YuComm.dll")]
        public static extern int GetBuffLength(int index = 1);
        //擷取通信狀态:false通信正常 ,true通信中斷
        [DllImport("YuComm.dll")]
        public static extern bool GetStatus(int index=1);
        //輔助功能函數:讀取原始資料流
        [DllImport("YuComm.dll")]
        public static extern int GetSourceData(ref byte buff, int index = 1);
        //輔助功能函數:清除緩沖區
        //[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 = "序列槽狀态:打開成功";
           }
           else
           {
               label1.Text = "序列槽狀态:打開不成功";
           }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ClosePort(1);
            label1.Text = "序列槽狀态:序列槽關閉";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            StringBuilder buff = new StringBuilder(255); 
            GetWeight(buff,1);
            label2.Text = buff.ToString();
            buff = null;//釋放

            label5.Text = "緩沖區中資料量:"+ GetBuffLength().ToString();

            bool re = GetStatus();
            if (re)
                label4.Text = "通信狀态:通信資料流正常";
            else
                label4.Text = "通信狀态:通信資料流中斷";

            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 = "開始";
        }

    }