Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

DLL written by VC2015 is called by other languages.

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

(1) call DLL with C++builder6

Create a DLL containing the MFC library with VC2015. If it is for C++Builder6 use, the steps are as follows:

1. Project attribute = = "Cellular clients =" Advanced = = "call convention option is: _ _ stdcall (/ Gd)

2. The function declaration in VC++2015 is as follows:

Extern "C" _ declspec (dllexport) VOID _ stdcall CreateMeter (const char* szTypeName)

3.VC++2015 's def file, EXPORTS does not have to write, because C++Builder6 does not need to use this .lib file

However, I found that to export a function, you need to declare it in the word def, otherwise you can't find the function in other languages and prompt an error.

4. In C++Builder6, you need to re-export DLL's bootstrap library .lib as follows:

Implib my.lib my.dll

Introduce my.lib into the BCB6 project and add the function declaration, that is, the function declaration in VC2015DLL (same as step 2)

(2) call with 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'; / / get the weight string function GetWeight (buff: pchar;index: integer = 1): PChar;stdcall; external' YuComm.dll' / / write serial 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'; / / helper function / / cache buffer data function GetBuffLength (index: integer= 0): integer;stdcall;external' YuComm.dll' / / or take the communication status True serial port is open but communication is interrupted. False communication is normal function GetStatus (index: integer = 0): boolean;stdcall;external 'YuComm.dll'; / / clear buffer data / / procedure Clear (index: integer = 0); stdcall;external' YuComm.dll'; / / get raw data function GetSourceData (buff: pbyte;index: integer = 0): integer;stdcall; external 'YuComm.dll'; implementationend.

Save the above file as a file with the extension pas and add it to the delphi7 project.

(3) call DLL in VB6

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' needs to add a return value declaration, even if Private Declare Function GetWeight Lib "YuComm" (ByVal buff As String) is not required Optional ByVal index As Integer = 1) As LongPrivate Declare Function GetStatus Lib "YuComm" (Optional ByVal index As Integer = 1) As IntegerPrivate 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 SubPrivate Sub Command2_Click () ClosePort (1) End SubPrivate Sub Command3_Click () End SubPrivate Sub Timer1_Timer () Dim re As Boolean re = GetStatus (1) If re = True Then Label2.Caption = "serial port opened successfully" Else Label2.Caption = "serial port failed" End If Dim buff1 As String buff1 = Space (100) Call GetWeight (buff1, 1) Label1.Caption = buff1 End Sub

Under the XP platform, use VB6 to call, please compile in release mode, otherwise VB6 will prompt that the corresponding DLL file cannot be found, that is, the file is in the running directory.

When called in VB, the function should be declared with a return value, even if the function in DLL has no return value. The return value can be of any type.

(4) call DLL in C#

Public partial class Form1: Form {/ / = / / 1, create the instrument, and open the instrument [DllImport ("YuComm.dll")] public static extern bool CreateMeter (string xktype,int no,int comm, int baud,int parity,int databit,int stopbit) / / 2. Read the weight of the instrument. For multiple meters, the parameter is index [DllImport ("YuComm.dll")] public static extern string GetWeight (StringBuilder weight, int index = 1); / / write serial port [DllImport ("YuComm.dll")] public static extern void Write (ref byte buff,int index = 1) / / 3. Close the serial port [DllImport ("YuComm.dll")] public static extern void ClosePort (int index=1); / / ScaleWeight auxiliary function: get the number of buffer data [DllImport ("YuComm.dll")] public static extern int GetBuffLength (int index=1) / / get communication status: false communication is normal, true communication is interrupted [DllImport ("YuComm.dll")] public static extern bool GetStatus (int index=1); / / Auxiliary function: read the original data stream [DllImport ("YuComm.dll")] public static extern int GetSourceData (ref byte buff, int index=1) / / Auxiliary function: clear buffer / / [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), 'nasty, 8,1); if (re) {label1.Text = "serial port status: opened successfully" } else {label1.Text = "serial port status: unsuccessful opening";}} private void button2_Click (object sender, EventArgs e) {ClosePort (1); label1.Text = "serial port status: serial port closed" } private void timer1_Tick (object sender, EventArgs e) {StringBuilder buff = new StringBuilder; GetWeight (buff,1); label2.Text = buff.ToString (); buff = null;// release label5.Text = "amount of data in buffer: + GetBuffLength () .ToString (); bool re = GetStatus () If (re) label4.Text = "Communication status: communication data flow is normal"; else label4.Text = "Communication status: communication data flow is interrupted"; byte [] data = new byte [1024]; int c = GetSourceData (ref data [0], 1); richTextBox1.Clear (); for (int I = 0) I < c; buff +) {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 = "pause"; else button4.Text = "start";}}

To run on XP, please use release to compile and publish, otherwise you will get an error when calling the function in DLL!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report