在Matlab,VB6和VB.NET程序之间发送消息的最简单方法 [英] Simplest way to send messages between Matlab, VB6 and VB.NET programs

查看:76
本文介绍了在Matlab,VB6和VB.NET程序之间发送消息的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将一套数据采集和分析例程从VB6程序升级到VB.NET,VB6和Matlab程序的混合体.我们希望保持系统模块化(独立的EXE),以便我们可以轻松创建专门的独立分析程序,而不必不断升级一个大型应用程序.当所有程序都用VB6编写时,我们已经使用MBInterProcess在EXE之间发送消息,这对我们来说非常理想(例如,让数据采集EXE将最新的文件名发送到独立的数据显示程序).不幸的是,不能在Matlab或VB.NET中使用此ActiveX来接收消息.我们想知道什么是我们可以采用的最简单的字符串消息传递系统(管道,已注册消息等).现在,我们只是在轮询以查看是否在特定文件夹中写入了新文件,这不是最佳解决方案.我们理想的解决方案不需要花费大量时间在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程序之间发送消息的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆