Delphi7通过VB6之COM对象调用PowerBASIC写的DLL功能

Delphi7通过VB6之COM对象调用PowerBASIC写的DLL功能。标题挺长,其实目标很简单,就是在Delphi7中使用PowerBASIC的MKI/CVI, MKS/CVS, MKD/CVD,并顺便加入CRC16检验函数,再进行16进制高低字节调整,方便在VB6、Delphi、Lazarus等环境下利用Modbus协议传送指令和数据时,进行十进制数的浮点转换和数据接收校验。我写的只是一个方法,其实用算法实现也并不十分复杂,但总觉得应该让曾经精典的老古懂们能做点事情,不希望职场上那样只要年龄大了就弃了不招不用的做法。

 分三步走:

  1.  用PowerBASIC写基本DLL
  2.  用VB6写COM组件
  3.  用Delphi7写界面验证程序

一、用PowerBASIC写基本DLL

PowerBASIC兼容VB6最好,甚至许多功能完胜VB6,而且QBASIC有的功能它基本上都保留了,只是随着VB6的淡出而停止了前行。如果用现在语言的功能衡量它们,它们确实老了,但在工控领域里还是有许多用武之地的,比如工厂一般使用的总线方面,Modbus在国内比较普及,即使有了TCP也只是从Modbus ASCII或Modbus RTU变成了Modbus TCP,所以小而精的东西在这方面比大而复杂的东西更受青睐。PowerBASIC写DLL很简单,DLL入口出口不用管,写自己的功能函数并EXPORT即可。

下面的MBFIEEE32PD.BAS是用PowerBASIC写的(由代码时都找不到选哪个了,就选VB.NET吧)

'MBFIEEE32PD.BAS
'===============================================================================
'
'  Generic DLL Template for PowerBASIC for Windows
'  Copyright (c) 1997-2011 PowerBASIC, Inc.
'  All Rights Reserved.
'
'  LIBMAIN function Purpose:
'
'    User-defined function called by Windows each time a DLL is loaded into,
'    and unloaded from, memory. In 32-bit Windows, LibMain is called each
'    time a DLL is loaded by an application or process.  Your code should
'    never call LibMain explicitly.
'
'    hInstance is the DLL instance handle.  This handle is used by the
'    calling application to identify the DLL being called.  To access
'    resources in the DLL, this handle will need to be stored in a global
'    variable.  Use the GetModuleHandle(BYVAL 0&) to get the instance
'    handle of the calling EXE.
'
'    fdwReason specifies a flag indicating why the DLL entry-point
'    (LibMain) is being called by Windows.
'
'    lpvReserved specifies further aspects of the DLL initialization
'    and cleanup.  If fdwReason is %DLL_PROCESS_ATTACH, lpvReserved is
'    NULL (zero) for dynamic loads and non-NULL for static loads.  If
'    fdwReason is %DLL_PROCESS_DETACH, lpvReserved is NULL if LibMain
'    has been called by using the FreeLibrary API call and non-NULL if
'    LibMain has been called during process termination.
'
' Return
'
'    If LibMain is called with %DLL_PROCESS_ATTACH, your LibMain function
'    should return a zero (0) if any part of your initialization process
'    fails or a one (1) if no errors were encountered.  If a zero is
'    returned, Windows will abort and unload the DLL from memory. When
'    LibMain is called with any other value than %DLL_PROCESS_ATTACH, the
'    return value is ignored.
'
'===============================================================================#COMPILER PBWIN 10
#COMPILE DLL#INCLUDE ONCE "Win32api.inc"GLOBAL ghInstance AS DWORD'-------------------------------------------------------------------------------
' Main DLL entry point called by Windows...
'
FUNCTION LIBMAIN (BYVAL hInstance   AS LONG, _BYVAL fwdReason   AS LONG, _BYVAL lpvReserved AS LONG) AS LONGSELECT CASE fwdReasonCASE %DLL_PROCESS_ATTACH'Indicates that the DLL is being loaded by another process (a DLL'or EXE is loading the DLL).  DLLs can use this opportunity to'initialize any instance or global data, such as arrays.ghInstance = hInstanceFUNCTION = 1   'success!'FUNCTION = 0   'failure!  This will prevent the EXE from running.CASE %DLL_PROCESS_DETACH'Indicates that the DLL is being unloaded or detached from the'calling application.  DLLs can take this opportunity to clean'up all resources for all threads attached and known to the DLL.FUNCTION = 1   'success!'FUNCTION = 0   'failure!CASE %DLL_THREAD_ATTACH'Indicates that the DLL is being loaded by a new thread in the'calling application.  DLLs can use this opportunity to'initialize any thread local storage (TLS).FUNCTION = 1   'success!'FUNCTION = 0   'failure!CASE %DLL_THREAD_DETACH'Indicates that the thread is exiting cleanly.  If the DLL has'allocated any thread local storage, it should be released.FUNCTION = 1   'success!'FUNCTION = 0   'failure!END SELECTEND FUNCTIONFUNCTION myMKI ALIAS "myMKI" (BYVAL Param1 AS INTEGER) EXPORT AS STRINGDIM I AS INTEGERDIM TString AS STRINGI=0: TString=""' code goes hereFOR I = 2 TO 1 STEP -1TString=TString+RIGHT$(("0"+LTRIM$(HEX$(ASC(MID$(MKI$(Param1),I,1))))),2)NEXT IFUNCTION = TString
END FUNCTIONFUNCTION myCVI ALIAS "myCVI" (BYVAL Param1 AS STRING) EXPORT AS INTEGERDIM I AS INTEGERDIM TString AS STRINGI=0: TString=""' code goes hereFOR I = 3 TO 1 STEP -2TString=TString+CHR$(VAL("&H"+MID$(Param1,I,2)))NEXT IFUNCTION = CVI(TString)
END FUNCTIONFUNCTION myMKL ALIAS "myMKL" (BYVAL Param1 AS LONG) EXPORT AS STRINGDIM I AS INTEGERDIM TString AS STRINGI=0: TString=""' code goes hereFOR I = 4 TO 1 STEP -1TString=TString+RIGHT$(("0"+LTRIM$(HEX$(ASC(MID$(MKL$(Param1),I,1))))),2)NEXT IFUNCTION = TString
END FUNCTIONFUNCTION myCVL ALIAS "myCVL" (BYVAL Param1 AS STRING) EXPORT AS LONGDIM I AS INTEGERDIM TString AS STRINGI=0: TString=""' code goes hereFOR I = 7 TO 1 STEP -2TString=TString+CHR$(VAL("&H"+MID$(Param1,I,2)))NEXT IFUNCTION = CVL(TString)
END FUNCTIONFUNCTION myMKS ALIAS "myMKS" (BYVAL Param1 AS SINGLE) EXPORT AS STRINGDIM I AS INTEGERDIM TString AS STRINGI=0: TString=""' code goes hereFOR I = 4 TO 1 STEP -1TString=TString+RIGHT$(("0"+LTRIM$(HEX$(ASC(MID$(MKS$(Param1),I,1))))),2)NEXT IFUNCTION = TString
END FUNCTIONFUNCTION myCVS ALIAS "myCVS" (BYVAL Param1 AS STRING) EXPORT AS SINGLEDIM I AS INTEGERDIM TString AS STRINGI=0: TString=""' code goes hereFOR I = 7 TO 1 STEP -2TString=TString+CHR$(VAL("&H"+MID$(Param1,I,2)))NEXT IFUNCTION = CVS(TString)
END FUNCTIONFUNCTION myMKD ALIAS "myMKD" (BYVAL Param2 AS DOUBLE) EXPORT AS STRINGDIM I AS INTEGERDIM TString AS STRINGI=0: TString=""' code goes hereFOR I = 8 TO 1 STEP -1TString=TString+RIGHT$(("0"+LTRIM$(HEX$(ASC(MID$(MKD$(Param2),I,1))))),2)NEXT IFUNCTION = TString
END FUNCTIONFUNCTION myCVD ALIAS "myCVD" (BYVAL Param1 AS STRING) EXPORT AS DOUBLEDIM I AS INTEGERDIM TString AS STRINGI=0: TString=""' code goes hereFOR I = 15 TO 1 STEP -2TString=TString+CHR$(VAL("&H"+MID$(Param1,I,2)))NEXT IFUNCTION = CVD(TString)
END FUNCTIONFUNCTION myCRC16 ALIAS "myCRC16" (BYVAL Param1 AS STRING) EXPORT AS STRING'An input string converted to a 4-byte HEX stringDIM DataA() AS BYTEDIM CRC16Lo AS BYTE, CRC16Hi AS BYTE        'CRC寄存器DIM CL     AS BYTE, CH       AS BYTE                  '多项式码&HA001DIM SaveHi     AS BYTE, SaveLo       AS BYTEDIM I     AS INTEGERDIM Flag     AS INTEGERDIM strMsg AS STRINGDIM intLen AS INTEGERstrMsg = Param1REPLACE " " WITH "" IN StrMsgintLen = LEN(strMsg) / 2 - 1REDIM DataA(0 TO intLen) AS BYTEFOR I = 0 TO intLenDataA(I) = VAL("&H" & MID$(strMsg, I * 2 + 1, 2))NEXTCRC16Lo = &HFFCRC16Hi = &HFFCL = &H1CH = &HA0FOR I = 0 TO UBOUND(DataA, 1)CRC16Lo = CRC16Lo XOR DataA(I)FOR Flag = 0 TO 7SaveHi = CRC16HiSaveLo = CRC16Lo'CRC16Hi = CRC16Hi \ 2SHIFT RIGHT CRC16Hi, 1'CRC16Lo = CRC16Lo \ 2SHIFT RIGHT CRC16Lo, 1IF ((SaveHi AND &H1) = &H1) THENCRC16Lo = CRC16Lo OR &H80END IFIF ((SaveLo AND &H1) = &H1) THENCRC16Hi = CRC16Hi XOR CHCRC16Lo = CRC16Lo XOR CLEND IFNEXT FlagNEXTERASE DataAFUNCTION = RIGHT$("0" & HEX$(CRC16Lo), 2) & RIGHT$("0" & HEX$(CRC16Hi), 2)
END FUNCTIONFUNCTION myINSTRU ALIAS "myINSTRU" (BYVAL Param1 AS STRING) EXPORT AS STRINGDIM LParam1 AS STRINGDIM RETURNSTR AS STRINGRETURNSTR = "UNKNOWN"LParam1 = TRIM$(Param1)SELECT CASE LParam1CASE "VERSION"RETURNSTR = "VERSION 1.00 9AUG2023"CASE "AUTHOR"RETURNSTR = "Mongnewer"END SELECTFUNCTION = RETURNSTR
END FUNCTION

不难看出,MKI/CVI MKS/CVS MKD/CVD这些函数在PowerBASIC里是保留的关键字,CRC16计算是我从CSDN上载了贴上去的,在这里感谢那位CSDN朋友的贡献。Modbus RTU一般使用十六进制浮点传送,因此程序里做了变换处理。

二、用VB6写COM组件

用VB6调用刚才编译后的MBFIEEE32PD.DLL非常容易,不需要做任何字符串处理,两者是100%一致的。做声明定义时完全按VB6的原则来即可,PowerBASIC是无条件遵从的。如果是写VB6应用程序,直接调用DLL中的函数,直接应用就可以了,这里路过就不多说了,还是接着往下写COM组件。

Private toSingle As Single
Private toDouble As DoublePrivate Declare Function myMKI Lib "MBFIEEE32PD" (ByVal a As Integer) As String
Private Declare Function myCVI Lib "MBFIEEE32PD" (ByVal b As String) As Integer
Private Declare Function myMKL Lib "MBFIEEE32PD" (ByVal a As Long) As String
Private Declare Function myCVL Lib "MBFIEEE32PD" (ByVal b As String) As Long
Private Declare Function myMKS Lib "MBFIEEE32PD" (ByVal a As Single) As String
Private Declare Function myCVS Lib "MBFIEEE32PD" (ByVal b As String) As Single
Private Declare Function myMKD Lib "MBFIEEE32PD" (ByVal a As Double) As String
Private Declare Function myCVD Lib "MBFIEEE32PD" (ByVal b As String) As Double
Private Declare Function myCRC16 Lib "MBFIEEE32PD" (ByVal a As String) As String
Private Declare Function myINSTRU Lib "MBFIEEE32PD" (ByVal a As String) As StringSelect Case commandnoCase 1'MKIModbusRoutines = setMKI(Val(commandval))Case 2'MKLModbusRoutines = setMKL(Val(commandval))Case 3'MKSModbusRoutines = setMKS(Val(commandval))Case 4'MKDModbusRoutines = setMKD(Val(commandval))Case 5'CVIModbusRoutines = Str$(getCVI(commandval))Case 6'CVLModbusRoutines = Str$(getCVL(commandval))Case 7'CVStoSingle = getCVS(commandval)toDouble = toSingleModbusRoutines = Str$(toDouble)Case 8'CVDModbusRoutines = Str$(getCVD(commandval))Case 9'CRC16ModbusRoutines = getCRC16(commandval)Case 10'VersionModbusRoutines = getINSTRU(commandval)End Select
End Function
Private Function setMKI(ByVal a As Integer) As StringM2I3HiddenWND.Text1.Text = myMKI(a)setMKI = M2I3HiddenWND.Text1.Text
End Function
Private Function getCVI(ByVal a As String) As IntegerM2I3HiddenWND.Text2.Text = agetCVI = myCVI(M2I3HiddenWND.Text2.Text)
End Function
Private Function setMKL(ByVal a As Long) As StringM2I3HiddenWND.Text3.Text = myMKL(a)setMKL = M2I3HiddenWND.Text3.Text
End Function
Private Function getCVL(ByVal a As String) As LongM2I3HiddenWND.Text4.Text = agetCVL = myCVL(M2I3HiddenWND.Text4.Text)
End Function
Private Function setMKS(ByVal a As Single) As StringM2I3HiddenWND.Text5.Text = myMKS(a)setMKS = M2I3HiddenWND.Text5.Text
End Function
Private Function getCVS(ByVal a As String) As SingleM2I3HiddenWND.Text6.Text = agetCVS = myCVS(M2I3HiddenWND.Text6.Text)
End Function
Private Function setMKD(ByVal a As Double) As StringM2I3HiddenWND.Text7.Text = myMKD(a)setMKD = M2I3HiddenWND.Text7.Text
End Function
Private Function getCVD(ByVal a As String) As DoubleM2I3HiddenWND.Text8.Text = agetCVD = myCVD(M2I3HiddenWND.Text8.Text)
End Function
Private Function getCRC16(ByVal a As String) As StringgetCRC16 = myCRC16(a)
End Function
Private Function getINSTRU(ByVal a As String) As StringgetINSTRU = myINSTRU(a)
End Function

打开VB6,选Active X,把上面的码贴进去,添加个无边的小窗体,放上Text1到Text7共7个文本框,Form的名字 M2I3HiddenWND,属性是 Hidden 隐藏的。文件名 MBFMODIEEE,类名 MBFIEEECRC,存盘、生成 MBFMODIEEE.DLL,即为其它开发环境使用的COM了。

加这个Hidden窗口是这么想的,VB6和PowerBASIC变量和字符串完全兼容,但Delphi7就不一定了,尤其是字符串存储方式的转换。从Delphi来的字符串显示在VB6的文本框可以,但直接传送给PowerBASIC或许有问题,于是就想让文本框做个过渡,或许直接传也不是问题,我没做验证。

因为这个DLL是COM,需要将 MBFMODIEEE.DLL和MBFIEEE32PD.DLL放在同一目录下,并在目录中放入Delphi7应用程序。为了让程序能互访,在CMD窗口里,转到它们所在的目录下,用regsvr32将MBFMODIEEE.DLL注册到系统中。regsvr32 MBFMODIEEE.DLL 回车即可。

三、用Delphi7写界面验证程序

在Delphi下引用刚才注册的MBFMODIEEE.DLL

 在弹出的列表中选中刚才注册的MBFMODIEEE,并点击 Create Unit生成 MBFMODIEEE_TLB声明文件,刚才注册的DLL中要调用的类和接口就都有了。

 在USE中引用生成的PAS,然后为接口声明个handle

 在Form产生时创建对象

 然后在需要的地方就可以通过接口使用对象中的功能函数了

 然后就是正常的开发应用程序,编译后运行(有时开发环境下可能出现异常,但编译后运行是比较好的方法。都是老顽固,稳定可靠,但要就着它们的性子,不能太勉强了)。

BTW:这些功能除PowerBASIC外,FreeBASIC里更齐全,甚至包括了QBASIC的全部关键字,但它的字符串不同于VB和Delphi,需要另外处理。不过它可以写COM,除32位编译器,它还有64位编译器。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/60435.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux配置QT Creator环境:ubuntu中安装QT Creator环境

一、前景 目前市面上很多公司使用QT Creator进行界面开发,基本都会选择在Linux环境进行,优点不仅是市场所需,更是方便后期代码的移植,相较于Windows系统,Linux系统移植性非常好。故此篇文章,介绍如何在Linu…

【EI/SCOPUS检索】第三届新媒体发展与现代化教育国际学术会议(NMDME 2023)

第三届新媒体发展与现代化教育国际学术会议(NMDME 2023) The 3rd International Conference on New Media Development and Modernized Education 第三届新媒体发展与现代化教育国际学术会议(NMDME 2023)将于2023年10月13-15日于西安召开。会议旨在为新…

FPGA_时钟显示(时钟可调)

1. 实验说明 在数码管显示数据的基础上,让六位数码管显示数字时钟,并且通过按键可以对时间进行修改。 实验目标:六位数码管分别显示时间的时分秒,且通过按键可实现加减调整时间及清零功能。 key1: 切换键:选择待…

lancet: 【推荐】--源码学习

一个全面、高效、可复用的go语言工具函数库; 可以学习源码的好的地方,这个是个工具库,建议最好的办法是 在项目中导入后,然后查看他的各个源代码进行学习使用 golangd中,查看导入包以及他的源代码; 中文…

Telerik UI for ASP.NET Core Crack

Telerik UI for ASP.NET Core Crack Telerik ASP.NET Core还包括MVC和Kendo UI捆绑包(用于JavaScript)、Figma的设计工具包以及文档处理库、用于ASP.NET Core的Telerik REPL、RTL支持、辅助功能和键盘导航、主题化、虚拟课堂培训、详细文档、演示、KBs和世界级支持。使用一整套…

接口幂等性实现方式

优质博文:IT-BLOG-CN 幂等 操作的特点是一次和多次请求某一个资源对于资源本身应该具有同样的结果(网络超时等问题除外)。幂等函数或幂等方法是指可以使用相同参数重复执行,并能获得相同结果的函数。这些函数不会影响系统状态&am…

【云原生】Docker 详解(一):从虚拟机到容器

Docker 详解(一):从虚拟机到容器 1.虚拟化 要解释清楚 Docker,首先要解释清楚 容器(Container)的概念。要解释容器的话,就需要从操作系统说起。操作系统太底层,细说的话一两本书都说…

科技引领,教育革新|EasyV助力数字孪生智慧教育建设!

数字孪生校园是以物联网、大数据、云计算、人工智能、三维可视化等新型数字化技术为基础,构建的数智校园的“大脑”。对校园的人、车、资产设施、各业务系统进行全联接,实现数据全融合、状态全可视、业务全可管、事件全可控,使校园更安全、更…

Observable设计模式简介

Observable设计模式存在于许多Java API和响应式编程中。下面介绍Java中永恒的Observable模式。 Observable设计模式用于许多重要的Java API。一个众所周知的示例是使用ActionListenerAPI执行操作的JButton。在这个例子中,我们ActionListener在按钮上进行了监听或…

【大数据】Flink 详解(二):核心篇 Ⅱ

Flink 详解(二):核心篇 Ⅱ 22、刚才提到 State,那你简单说一下什么是 State。 在 Flink 中,状态 被称作 state,是用来保存中间的计算结果或者缓存数据。根据状态是否需要保存中间结果,分为 无状…

2023年上海国际车展,英信翻译提供中日英同传服务

在2023年4月上海车展期间,日产汽车展示了一系列搭载智能网联技术和电驱动技术的车型,包括首次亮相的Max-Out概念车和专为中国消费者设计的纯电动SUV概念车——日产Arizon。备受全球汽车行业瞩目。 日产是日本第二大汽车公司,也是世界十大汽车…

【JavaSE】接口的语法知识和使用方法总结

目录 1. 接口的概念 2. 语法规则 3. 接口特性 4. 接口使用 5. 实现多个接口 6. 接口间的继承 1. 接口的概念 在现实生活中,接口的例子比比皆是,比如:笔记本上的USB口,电源插座等。 电脑的USB口上,可以插&#x…