问题描述
我们正在将一套数据采集和分析例程从 VB6 程序升级为 VB.NET、VB6 和 Matlab 程序的混合体.我们希望保持系统模块化(单独的 EXE),以便我们可以轻松创建专门的独立分析程序,而无需不断升级一个大型应用程序.当所有程序都用 VB6 编写时,我们使用 MBInterProcess 在 EXE 之间发送消息,这对我们来说非常有效(例如,让数据采集 EXE 将最新的文件名发送到独立的数据显示程序).不幸的是,这个 ActiveX 不能在 Matlab 或 VB.NET 中用于接收消息.我们想知道我们可以采用的最简单的字符串消息传递系统(管道、注册消息等)是什么.现在我们只是轮询看看新文件是否写入特定文件夹,这不是最好的解决方案.我们理想的解决方案不需要花费大量时间来学习 Windows 的细微差别(我们是生物学家,而不是全职程序员),并且可以在 WinXP 和 64 位版本的 Windows 中运行.
We are upgrading a suite of data acquisition and analysis routines from VB6 programs to a mixture of VB.NET, VB6, and Matlab programs. We want to keep the system modular (separate EXEs) so we can easily create specialized stand-alone analysis programs without having to constantly upgrade one massive application. We have used MBInterProcess to send messages between EXEs when all the programs were written in VB6 and this worked perfectly for us (e.g., to have the data acquisition EXE send the latest file name to a stand-alone data display program). Unfortunately, this ActiveX cannot be used within Matlab or VB.NET to receive messages. We are wondering what is the simplest string message passing system (pipes, registered messages, etc) that we could adopt. Right now we are just polling to see if new file was written in a specific folder, which can't be the best solution. Our ideal solution would not require a huge investment in time learning nuances of Windows (we are biologists, not full-time programmers) and would work in both WinXP and 64-bit versions of Windows.
为了响应这些查询,我们将整个 Matlab 会话包装在一个具有 MBInterProcess ActiveX 控件的 VB6 程序中.这可行,但对我们来说不是一个很好的解决方案,因为它可能会将我们永远锁定在 WinXP 中(并且肯定会阻止我们使用 64 位版本的 Matlab).最新版本的 Matlab (2009a) 可以直接访问 .NET 函数,因此我们假设一种解决方案可能是使用 .NET 库来实现跨程序的管道(或类似的东西).我们想重新创建 MBInterProcess ActiveX 的优雅简单的语法,并拥有一段代码来侦听具有该程序的顶级 Windows 名称的消息,然后调用特定的 Matlab m 文件或 VB.NET 函数,字符串数据(例如,文件名)作为参数.
In response to the queries, we have wrapped the entire Matlab session within a VB6 program that has the MBInterProcess ActiveX control. That works but is not a great solution for us since it will probably lock us into WinXP forever (and certainly will prevent us from using the 64-bit version of Matlab). The latest version of Matlab (2009a) can access .NET functions directly, so we assume one solution might be to use the .NET library to implement pipes (or something similar) across programs. We would like to recreate the elegantly simple syntax of the MBInterProcess ActiveX and have a piece of code that listens for a message with that program's top-level Windows name, and then call a specific Matlab m-file, or VB.NET function, with the string data (e.g., file name) as an argument.
推荐答案
能否在VB6 中创建一个ActiveX EXE 来简单地在各方之间转发消息?当有人调用它时,它会使用传递给调用的参数引发一个事件.您的 VB6 和 VB.NET 代码可以建立对 ActiveX exe 的引用来调用它并接收它的事件.我不熟悉 Matlab,所以我不知道它是否可以在那里访问.
Could you create an ActiveX EXE in VB6 to simply forward messages between the different parties? When anyone called it, it would raise an event with the parameters passed to the call. Your VB6 and VB.NET code could establish a reference to the ActiveX exe to call it and sink its events. I'm not familiar with Matlab so I don't know whether it would be accessible there.
您已经写到 Matlab 2009a 可以直接访问 .NET.如果它可以接收 .NET 事件,您还可以在 VB6 ActiveX EXE 上使用 .NET 包装器.
you've written that Matlab 2009a can access .NET directly. If it can sink .NET events, you could also have a .NET wrapper on the VB6 ActiveX EXE.
这是我快速敲出的一些示例代码.
Here's some sample code I knocked up quickly.
项目名称为 VB6MatlabMessenger 的 VB6 ActiveX EXE 项目.每条消息都有一个文本字符串 Destination(以某种方式标识预期的收件人)和一个带有消息的字符串.
VB6 ActiveX EXE project with project name VB6MatlabMessenger. Each message has a text string Destination (that somehow identifies the intended recipient) and a string with the message.
'MultiUse class VB6Messenger
Option Explicit
Public Event MessageReceived(ByVal Destination As String, ByVal Message As String)
Public Sub SendMessage(ByVal Destination As String, ByVal Message As String)
Call Manager.RaiseEvents(Destination, Message)
End Sub
Private Sub Class_Initialize()
Call Manager.AddMessenger(Me)
End Sub
Friend Sub RaiseTheEvent(ByVal Destination As String, ByVal Message As String)
RaiseEvent MessageReceived(Destination, Message)
End Sub
'BAS module called Manager
Option Explicit
Private colMessengers As New Collection
Sub AddMessenger(obj As VB6Messenger)
colMessengers.Add obj
End Sub
Sub RaiseEvents(ByVal Destination As String, ByVal Message As String)
Dim obj As VB6Messenger
For Each obj In colMessengers
Call obj.RaiseTheEvent(Destination, Message)
Next obj
End Sub
还有一个测试VB6正常的exe,参考了VB6MatlabMessenger.这是整个frm文件.将其构建为 exe,运行几个副本.填写目标和消息文本字段并单击按钮 - 您将看到消息已在 所有 exe(在列表框中报告)中收到.
And a test VB6 normal exe, with a reference to the VB6MatlabMessenger. Here is the whole frm file. Build this as an exe, run a few copies. Fill in the destination and message text fields and click the button - you will see that the messages are received in all the exes (reported in the listboxes).
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.ListBox lstEvents
Height = 1620
Left = 120
TabIndex = 3
Top = 1320
Width = 4455
End
Begin VB.TextBox txtMessage
Height = 375
Left = 120
TabIndex = 2
Text = "Message"
Top = 840
Width = 2295
End
Begin VB.TextBox txtDestination
Height = 375
Left = 120
TabIndex = 1
Text = "Destination"
Top = 240
Width = 2295
End
Begin VB.CommandButton cmdSendMessage
Caption = "Send Message"
Height = 495
Left = 2640
TabIndex = 0
Top = 360
Width = 1575
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private WithEvents objMessenger As VB6MatlabMessenger.VB6Messenger
Private Sub cmdSendMessage_Click()
objMessenger.SendMessage txtDestination, txtMessage.Text
End Sub
Private Sub Form_Load()
Set objMessenger = New VB6MatlabMessenger.VB6Messenger
End Sub
Private Sub objMessenger_MessageReceived(ByVal Destination As String, ByVal Message As String)
lstEvents.AddItem Now() & " RECEIVED - " & Destination & ", " & Message
End Sub
我开始编写一个 VB.NET 类库,它封装了 VB6 以使其可被 .NET 访问.我没有测试过这个.它引用了 VB6MatLabMessenger.
I started writing a VB.NET class library that wraps the VB6 to make it accessible to .NET. I haven't tested this one. It has a reference to the VB6MatLabMessenger.
Public Class VBNETMatlabMessenger
Private WithEvents objVB6Messenger As VB6MatlabMessenger.VB6Messenger
Public Event MessageReceived(ByVal Destination As String, ByVal Message As String)
Public Sub SendMessage(ByVal Destination As String, ByVal Message As String)
objVB6Messenger.SendMessage(Destination, Message)
End Sub
Public Sub New()
objVB6Messenger = New VB6MatlabMessenger.VB6Messenger
End Sub
Private Sub objVB6Messenger_MessageReceived(ByVal Destination As String, ByVal Message As String) Handles objVB6Messenger.MessageReceived
RaiseEvent MessageReceived(Destination, Message)
End Sub
End Class
这可能会让您入门.请注意,VB6 messenger 对象将永远存在,因为 messenger 在内部保留对它们的引用,因此 COM 永远不会整理它们.如果这成为一个问题(如果 许多 消息在没有重新启动 PC 的情况下发送),您可以向 VB6 messenger 添加一个方法,指示它从其集合中删除 messenger 对象,
This might get you started. Note that the VB6 messenger objects will live forever because the messenger keeps a reference to them internally, so COM will never tidy them up. If this becomes a problem (if many messages are sent without rebooting the PC) you could add a method to the VB6 messenger which instructs it to removed the messenger object from its collection,
这篇关于在 Matlab、VB6 和 VB.NET 程序之间发送消息的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!