VC2015编写的DLL给其他语言调用
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,(1)用C++builder6调用DLL用VC2015创建包含MFC库的DLL,如果是给C++Builder6使用,步骤如下:1、工程属性==》C++==》高级==》调用约定 选项为:__stdcal
千家信息网最后更新 2025年12月03日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'; implementationend.
将上面文件保存为扩展名为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 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 = "串口打开成功" 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文件,即所这个文件就在运行目录下
在VB中调用,声明函数时应加上返回值,即使DLL中这个函数没有返回值.返回值的类型可任意
(4)在C#中调用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 = "开始"; } }在XP上运行,请使用release方式编译发布,否则调用DLL中的函数时会报错!
函数
串口
通信
数据
状态
文件
缓冲区
缓冲
成功
辅助
仪表
功能
工程
数据流
原始
方式
重量
提示
编译
运行
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
江西省网络安全知识竞答答题
软件开发公司有什么资质
文件服务器流量控制
兰陵县网络安全
为什么要建立网络安全应急预案
福建软件开发加盟商哪个好
软件开发职业介绍
信创数据库技术
大华股份软件开发待遇
批处理运行服务器命令
吴爱芳网络技术服务
天鹅湖租房软件开发
台州市国家网络安全宣传周
杭州烈焰网络技术临海
微处理器软件开发工具
c语言服务器
北京拉手网络技术有限公司赤峰
怎么解除服务器的安全连接
网络安全属于专业硕士吗
重庆电商软件开发定制费用
大专有学软件开发的吗
软件开发公司有什么资质
文件服务器流量控制
服务器完成苹果代码打包
网贷是网络安全
共享自动娃娃机软件开发
广东软件开发条件
如何用网袋做网络安全手工
数据库的加锁机制
TiDB数据库的代表