Using DDE on Your VB Application

When writing a application that uses command lines, you are left with a small dilemma of ‘What if the User reruns the app with a new command-line?’ By using a DDE link, you can pass the new commands through to the Previous instance of the application.

OR… You have written an application that runs as a service, and want to use a separate application to configure the services options. Again, you could use a DDE to transfer the info and data to the application.

So, for this task, you are looking at making an application that will pass the new command line to the previous instance.

First, you need to make some changes on your form so that it will accept the DDE link. In the project explorer, select the form that will accept the connection. Change the Linkmode property to 1 – Source. Although you set it as the source, the form really is now the target for DDE links. You also set the LinkTopic to some relevant name rather than using the default Form1 topic. In the example, you will use “DDELink“.

Now that the Application is ready to accept DDE links, you now can set up the Link Action. You will now notice that the form has a new event, Form_LinkExecute. Whenever a successful DDE link takes place, this event is triggered, giving you the data that the DDE link requester has sent.

Private Sub Form_LinkExecute(CmdStr As String, Cancel As Integer)
   List1.AddItem CmdStr
   Cancel = 0
End Sub

In the above code, I simply add the Link data to a listbox, but you can store or set off all sorts of data or triggers from this Event.

There are two things to take care of and to remember:

  1. The app is running and may even be in the middle of processing. (This event can be called during a Doevents from another sub in the application.) Take care not to alter any variables that are critical to these subs. Rather, set a flag to inform the application that new data is available. Store the data in temporary variables and update when the process is completed.
  2. Many of your classes and controls will be loaded and initialised already. Check to see if they are. Reloading and re-initialising them may cause unpredictable results.

Now, how do you initiate the DDE link and send data through it? I found that, for ease of use and minimal interference with the rest of the application, it’s best to add a Textbox to the form, set its Visible property to False, and initialize the connection from it.

So, now you add a Textbox to the form, calling it HiddenText. Change the Visible property to False. No other properties need to be set. You now are ready to set up the DDE link.

Because in this task you looking at linking to a previous instance, your DDE link will be initiated from the ‘orm’s Form_Load event. In this event, the first thing you do is check to see whether there is a previous instance.

Private Sub Form_Load()
If App.PrevInstance Then
   'Previous instance of App found
   If Command$ <> vbNullString Then
      'New commandLine parameters to send
      HiddenText.Text = Command$
      HiddenText.LinkTopic = "Project1|DDELink"
      'Set the link topic
      HiddenText.LinkMode = vbLinkManual
      'Initiate the DDE link
      HiddenText.LinkExecute HiddenText.Text
      'Send data
      HiddenText.LinkMode = vbLinkNone
      'Close Link
    End If
   Unload Me
   'Close the copy of the app
   Exit Sub
End If
If Command$ <> vbNullString Then
   List1.AddItem Command$
End If
End Sub

Note: Testing the DDE link will not work in the IDE. You need to compile the application and run the executable.

Passing data to a second application is the principle. The Form_LinkExecute code is in the application that needs to receive the data. Instead of using Form_load, you use a command button in the sending application.

Private Sub Command1_Click()
   Text1.LinkTopic = "DDE_Main|DDElink"
   'App name and Link topic of destination program.
   Text1.LinkMode = vbLinkManual
   Text1.LinkExecute Text1.Text
   Text1.LinkMode = vbLinkNone
   Text1.Text = ""
End Sub

That’s about all there is to know about DDE links. Enjoy.

More by Author

Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Must Read