DDE with VB.Net

Introduction

DDE appears to be the first solution Microsoft offered to let two applications communicate. It's an awkward and slow solution, which explains why it's not supported by default in VB.Net. Apparently, the only open-source add-on is NDde, last updated in 2006.

To read

http://www.angelfire.com/biz/rhaminisys/ddeinfo.html#DDEoverview

http://msdn.microsoft.com/en-us/library/ms648774(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/ms649053.aspx

Setup

Download and unzip NDde

Launch VB 2008 Express, and create a new Windows Form project

Project > Add Reference > Browse, and choose NDde.dll

Coding

DDE mechanism overview

DDE server = service + topic + item

service + topic = channel or phone number

To establish a conversation a DDE client specifies the service/topic name pair (channel) it wishes to connect to. Windows broadcasts the request to all top level windows. The first server to accept is connected to the client and so a conversation is established.

Sample

  Try
      Using client As DdeClient = New DdeClient("myapp", "mytopic")
          AddHandler client.Disconnected, AddressOf OnDisconnected
 
          client.Connect()
 
          client.Execute("mycommand", 60000)
 
          client.Poke("myitem", DateTime.Now.ToString(), 60000)
          Console.WriteLine("Request: " + client.Request("myitem", 60000))
 
          client.BeginExecute("mycommand", AddressOf OnExecuteComplete, client)
 
          client.BeginPoke("myitem", Encoding.ASCII.GetBytes(DateTime.Now.ToString() + Convert.ToChar(0)), 1, AddressOf OnPokeComplete, client)
 
          client.BeginRequest("myitem", 1, AddressOf OnRequestComplete, client)
 
          client.StartAdvise("myitem", 1, True, 60000)
          AddHandler client.Advise, AddressOf OnAdvise
 
          Console.WriteLine("Press ENTER to quit...")
          Console.ReadLine()
 
      End Using
 
  Catch e As Exception
 
      Console.WriteLine(e.ToString())
      Console.WriteLine("Press ENTER to quit...")
      Console.ReadLine()
 
  End Try
 
End Sub
 
Private Sub OnExecuteComplete(ByVal ar As IAsyncResult)
  Try
      Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
      client.EndExecute(ar)
      Console.WriteLine("OnExecuteComplete")
  Catch e As Exception
      Console.WriteLine("OnExecuteComplete: " + e.Message)
  End Try
End Sub
 
Private Sub OnPokeComplete(ByVal ar As IAsyncResult)
  Try
      Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
      client.EndPoke(ar)
      Console.WriteLine("OnPokeComplete")
  Catch e As Exception
      Console.WriteLine("OnPokeComplete: " + e.Message)
  End Try
End Sub
 
Private Sub OnRequestComplete(ByVal ar As IAsyncResult)
  Try
      Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
      Dim data() As Byte = client.EndRequest(ar)
      Console.WriteLine("OnRequestComplete: " + Encoding.ASCII.GetString(data))
  Catch e As Exception
      Console.WriteLine("OnRequestComplete: " + e.Message)
  End Try
End Sub
 
Private Sub OnStartAdviseComplete(ByVal ar As IAsyncResult)
  Try
      Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
      client.EndStartAdvise(ar)
      Console.WriteLine("OnStartAdviseComplete")
  Catch e As Exception
      Console.WriteLine("OnStartAdviseComplete: " + e.Message)
  End Try
End Sub
 
Private Sub OnStopAdviseComplete(ByVal ar As IAsyncResult)
  Try
      Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
      client.EndStopAdvise(ar)
      Console.WriteLine("OnStopAdviseComplete")
  Catch e As Exception
      Console.WriteLine("OnStopAdviseComplete: " + e.Message)
  End Try
End Sub
 
Private Sub OnAdvise(ByVal sender As Object, ByVal args As DdeAdviseEventArgs)
  Console.WriteLine("OnAdvise: " + args.Text)
End Sub
 
Private Sub OnDisconnected(ByVal sender As Object, ByVal args As DdeDisconnectedEventArgs)
  Console.WriteLine( _
   "OnDisconnected: " + _
   "IsServerInitiated=" + args.IsServerInitiated.ToString() + " " + _
   "IsDisposed=" + args.IsDisposed.ToString())
End Sub

Q&A

How to use DDE in VB.Net?

NDde 2.01.0563 source code uploaded Jun 7 2007

Mark Hurd's VB.Net DDE sample

DDEML

What is DDEML?

"The Dynamic Data Exchange Management Library (DDEML) provides an interface that simplifies the task of adding DDE capability to an application. Instead of sending, posting, and processing DDE messages directly, an application uses the functions provided by the DDEML to manage DDE conversations. [...] To use the DDEML, you must include the DDEML.H header file in your source files, link with the USER32.LIB file, and ensure that the DDEML.DLL file resides in the system's path." (source)

What is ROT (Running Object Table)?

Resources