How "talk" between applications?

V

VB Programmer

If I have a VB6 and a VB.NET application, or 2 VB.NET applications what is
the best way to "talk" between them? They both run on the same PC.

Right now I'm just using text files to share information, etc...

(Not database applications.)

Thanks.
 
M

Mike Bulava

Well that kind of depends on what data is being sent back an forth.

My personal favorite is remoting, but remoting can be complex it you're not
firmilar with it because it can run either at the remote source or the
remote client.
 
V

VB Programmer

Both applications are running on the SAME PC.

The data is very simple. Just a few variables and their values. That's it.
No recordsets, etc...
 
H

Herfried K. Wagner [MVP]

* "VB Programmer said:
If I have a VB6 and a VB.NET application, or 2 VB.NET applications what is
the best way to "talk" between them? They both run on the same PC.

Keyword: Remoting.
 
V

VB Programmer

Any links to "beginners guides", etc...?

Does this work well between VB6 and VB.NET too?
 
T

Tom Leylan

Beyond the buzzwords :)

You might have mentioned it but I didn't notice... what is your definition
of "talk"? I believe the methodology of choice is influenced by two major
factors (three or four if we include cross-language and cross-platform) but
they are the quantity of information and the rapidity of the exchange.

Tons of data can be exchanged through files. Consider the dBASE .dbf file
(or any ISAM file.) This is a perfectly good mechanism for exchanging data
and it can handle a little data or a lot and the apps can be on the same
computer or different ones. It has worked for decades.

You don't have to actually persist the data however. You can set up a
server app in your machine and have both apps talk to the server. This
would tend to be faster (probably) and gives you considerably more control.

You can also set up a peer-to-peer connection. Using TCP/IP you can
designate a port and have two apps chat back and forth through it.

What kind of data are you transmitting and what is the nature of the
exchange?

Tom Leylan


VB Programmer said:
Any links to "beginners guides", etc...?

Does this work well between VB6 and VB.NET too?

what
 
A

Adrian Forbes [ASP MVP]

You could look into talking over sockets, or possible store your data in
storage somewhere (a database) and have one poll it. Or maybe have them
send messages to each other via MSMQ/the messaging system. The latter
allows "fire and forget" and gives you options such as the whole thing not
breaking if one app isn't running. Depends what kind of communication you
are after though. Maybe sockets would be quickest but sometimes it is a bit
tricky to do.

You can, for example, serialise objects and send them over sockets and have
your listening app deserialise so the recipient gets a copy of the object
sent and you do minimal programming in between.
 
C

Chad Z. Hower aka Kudzu

Adrian Forbes said:
communication you are after though. Maybe sockets would be quickest but
sometimes it is a bit tricky to do.

Sockets are often the easiest as they are not subject to as many security
policies.
 
A

Adrian Forbes [ASP MVP]

Here is an example. Create a new windows app, add a checkbox a listbox and
a button. Start the app and check the checkbox to start the server (clear
it to shut the server down). Once the server has started click the button
to serialise/deseleralise the object through the socket. Obviously this
works standalone, but start two instances of the app and click the server
checkbox on one and press the button on the other and you'll see the exact
same code sending the object to a different app. This app could even be on
another machine.

Error handling etc is incredibly thin on the ground, this is just a demo.
Any comments welcome cos I'm still a .net noob myself :) If you plan on
using this kind of idea it is better to create a self-contained listener
object rather than hacking it the way I have here so that everything runs
from the one form. This is .net 1.1 and seems a bit different to how things
worked in 1.0 IIRC.

Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Xml.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Net.Sockets
Imports System.Net

Public Class Form1
Inherits System.Windows.Forms.Form

Private Delegate Sub UpdateListBoxDel(ByVal Data As String)
Private m_ListenerThread As Threading.Thread
Private server As New TcpListener(IPAddress.Parse("127.0.0.1"), 5000)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' Create a new object that is populated with data
Dim obj As New MyObject(1, "Adrian", "Forbes")
' Create an XmlSerializer for the MyObject type
Dim formatter As New XmlSerializer(GetType(MyObject))

' Create new TcpClient
Dim client As TcpClient = New TcpClient

' Connect to port 5000 on the local machine
UpdateDisplay("Connecting to client", True)
client.Connect("127.0.0.1", 5000)

' Get the underlying stream
Dim stream As Stream = client.GetStream

' Seralise out instance of MyObject over the TcpClient's stream
using the XmlSerializer
UpdateDisplay("Sending..." & obj.ToString, True)
formatter.Serialize(stream, obj)

' Close the stream
stream.Close()

End Sub

Private Sub StartServer()

' This function is going to run until the server closes
' so set up a var that lets us know we want to run
Dim bRunning As Boolean = True

' Start the server
UpdateDisplay("Server starting")
server.Start()

While bRunning
Try
' This method will block until a connection is accepted
UpdateDisplay("Server waiting for connection...")
Dim client As TcpClient = server.AcceptTcpClient()

' Now we have a TcpClient with an underlying stream
' so we want to deserialise our object. This is
' pretty much the sending code in reverse.
UpdateDisplay("Server connected")
Dim Formatter As XmlSerializer = New
XmlSerializer(GetType(MyObject))

Dim obj As MyObject =
CType(Formatter.Deserialize(client.GetStream), MyObject)

UpdateDisplay("Received " & obj.ToString)
Catch
' If we get an error such as the connection getting
' closed from under us then signal to stop running
bRunning = False
End Try

End While

' Stop the server
Debug.WriteLine("Server stopping")
server.Stop()

End Sub

Private Sub UpdateDisplay(ByVal Data As String)
' Invoke a call to update the listbox
ListBox1.Invoke(New UpdateListBoxDel(AddressOf UpdateListBox), New
Object() {Data})
End Sub

Private Sub UpdateDisplay(ByVal Data As String, ByVal Direct As Boolean)
If Direct Then
' This method will update the listbox on this thread
UpdateListBox(Data)
Else
' Otherwise use this method that will use the listbox's thread
to
' do the update
UpdateDisplay(Data)
End If
End Sub

Public Sub UpdateListBox(ByVal Data As String)
ListBox1.Items.Add(Data)
Application.DoEvents()
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

If Not m_ListenerThread Is Nothing Then
' If the TcpListener is listening then shut it down
StopServer()
End If

End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
' The server checkbox has been clicked so kick off a new thread
' and run our StartServer method on it
UpdateDisplay("Starting server")
m_ListenerThread = New Threading.Thread(AddressOf StartServer)
m_ListenerThread.Start()
UpdateDisplay("Started")
Else
' The checkbox has been unchecked so shut the server down
UpdateDisplay("Stopping server", True)
StopServer()
UpdateDisplay("Stopped", True)
End If
End Sub

Private Sub StopServer()

' Stop the TcpListener
server.Stop()
' and wait here for the thread to exit. The above call will cause
an
' exception in the listener thread so lets just wait until it is all
' closed up
m_ListenerThread.Join()

End Sub
End Class

<Serializable()> _
Public Class MyObject
Public EmployeeID As Long
Public Forename As String
Public Surname As String

Public Sub New(ByVal EmployeeID As Long, ByVal Forename As String, ByVal
Surname As String)
Me.EmployeeID = EmployeeID
Me.Forename = Forename
Me.Surname = Surname
End Sub

Public Sub New()
Me.EmployeeID = 0
Me.Forename = ""
Me.Surname = ""
End Sub

Public Overrides Function ToString() As String
Return EmployeeID.ToString & ": " & Surname & ", " & Forename
End Function

End Class
 
T

Tom Leylan

The OP might be able to get some valuable information from the code you
posted but what does he run on the VB6 side?
 
T

Tom Scales

The OP said either VB6+VB.NET OR two VB.NET

Tom
Tom Leylan said:
The OP might be able to get some valuable information from the code you
posted but what does he run on the VB6 side?
 
T

Tom Leylan

Yup... So given those two scenarios a .Net only solution won't work for one
of them :)

I'll leave it to the OP to decide if .Net object serialization is the type
of "talk" he meant, perhaps it is.
 
V

VB Programmer

My first scenario includes a VB6 app and a VB.NET app. The VB6 app sends
some variable/value sets over to the VB.NET app for further processing.
It's minimal data.

I will keep all of this in mind for future development though. It sounds
like sharing data between 2 .NET apps can be done by remoting and other
ways. I just I have to educate myself more.

Thanks everyone!
 
A

Adrian Forbes [ASP MVP]

The shallow XML Serialisation is incredibly simple and you can easily make
VB6 emulate it using basic XML such as the DOMDocument. The class I used to
test reduces to

<MyObject>
<EmployeeID>1</EmployeeID>
<Forename>Adrian</Forename>
<Surname>Forbes</Surname>
</MyObject>

Just to prove the concept I wrote a VB version of my test app and they can
both talk to their own app or talk to each other, so are totally
interchangeable. It is only XML over sockets, simple.
 
T

Tom Leylan

Adrian: That's great!

Adrian Forbes said:
The shallow XML Serialisation is incredibly simple and you can easily make
VB6 emulate it using basic XML such as the DOMDocument. The class I used to
test reduces to

<MyObject>
<EmployeeID>1</EmployeeID>
<Forename>Adrian</Forename>
<Surname>Forbes</Surname>
</MyObject>

Just to prove the concept I wrote a VB version of my test app and they can
both talk to their own app or talk to each other, so are totally
interchangeable. It is only XML over sockets, simple.
 
V

Volker Jobst

Hi Adrian,

Could you please write which code you modified in order to communicate with
xml?

thanx volker
 
V

VB Programmer

Exactly my thoughts. A quick example of the "talking" portion would be most
helpful. Thanks so much for the great responses!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top