前面说的Delphi通过Activex DLL同时调用PowerBasic和FreeBasic写的DLL,是在WINDOWS基础平台上完成的。 而 .NET平台是架在WINDOWS基础平台之上的,它的上面VB.NET或C#等开发的APP程序,下面写一下用VB.NET,通过VB6注册的Activex DLL公共对象COM,调用PowerBasic和FreeBasic写的DLL实现,核心功能是MKI/CVI MKL/CVL MKS/CVS MKD/CVD的IEEE浮点计算。它们在一些传统产业上还在应用,而且一时半时也不会弃之不用。

先创建新项目

往下翻,找到VB窗体项目,中途大多是C#移动开发。

在项目菜单中选填加引用

确认后加到依赖项下

在窗体上摆一些控件,和VB6设计时的界面内容差不多。

在程序的最上部把注册的COM引用进来

在Command1下写代码
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As MBFIEEECRC = New MBFIEEECRC
TextBox11.Text = "VERSION" : TextBox1.Text = s.ModbusRoutines(PBFB:=1, commandno:=10, commandval:=TextBox11.Text)
TextBox11.Text = "AUTHOR" : TextBox10.Text = s.ModbusRoutines(PBFB:=1, commandno:=10, commandval:=TextBox11.Text)
TextBox2.Text = s.ModbusRoutines(PBFB:=1, commandno:=1, commandval:="1123.21") 'MKI
TextBox9.Text = s.ModbusRoutines(PBFB:=1, commandno:=5, commandval:=TextBox2.Text) 'CVI
TextBox3.Text = s.ModbusRoutines(PBFB:=1, commandno:=2, commandval:="1123.21") 'MKL
TextBox8.Text = s.ModbusRoutines(PBFB:=1, commandno:=6, commandval:=TextBox3.Text) 'CVL
TextBox4.Text = s.ModbusRoutines(PBFB:=1, commandno:=3, commandval:="1123.21") 'MKS
TextBox7.Text = s.ModbusRoutines(PBFB:=1, commandno:=7, commandval:=TextBox4.Text) 'CVS
TextBox5.Text = s.ModbusRoutines(PBFB:=1, commandno:=4, commandval:="1123.21") 'MKD
TextBox6.Text = s.ModbusRoutines(PBFB:=1, commandno:=8, commandval:=TextBox5.Text) 'CVD
End Sub
MBFIEEECRC是COM的对外接口,把它NEW一个出来就可以用了,用法和VB6开发差不多一样的,调用函数ModbusRoutines时参数写至里面,有一个地方留心一点。

入口参数是字符串时,先所它放到文本框中再将文本框中的TEXT作为参数使用,不宜直接将字符串作为参数传给COM,因为表面看上去无差别,但存储方式是有差异的,DLL间传送字符串时比较好的解决办法是先放到文本框,再送出文本框内容,在写COM时也如些处理一下,能免去调用时莫明其妙地异常退出。

编译时选用x86模式,因为COM是线程内x32的。如果选了x64模式,编译通不能运行,找不到COM中的函数。正确编译后在x86的debug文件夹下生成可执行文件,同时把VB6的创建的那些DLL考贝进来。

双击WinFormsApp1.exe即可运行,点Command1执行,结果就出来了。

VS2022版的项目文件于下面的链接中