Virtual Developer Workshop: Containerized Development with Docker

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.

About the Author

Richard Newcombe

Richard Newcombe has been involved in computers since the time of the Commodore 64. Today, he has excelled in programming, and designs. Richard is in his mid 30's and, if or when you looking for him look no further than his computer. Always willing to help and give advice where he can in regard to computer related subjects. At present he is working as a .NET 2008 Software Developer for Syncrony Web Services, South Africa.



  • DDE

    Posted by Keithuk on 12/08/2009 08:21pm

    You can transfer data from different VB apps easily. I know its old but it still works a treat.
    [url=http://www.gab2001uk.com/index.html]DDE (Dynamic Data Exchange) And Network DDE[/url]

  • dde help

    Posted by ctonge on 03/09/2007 09:14pm

    I've written a little app that opens and excel workbook with a few links to a dde server. If the dde server isn't running it puts up a message asking if I want to start the dde application. I'd like to suppress this message and command it to run the dde server. I've tried setting all the excel options for updating links automatically and not displaying prompts, but those have no effect when the server is not running. Is it possible to accomplish this? I'm a fairly new VB programmer and can't seem to find any examples of this sort. regards, Clint.

    • PDF navigation using visual basic 2005

      Posted by georgestef on 10/20/2007 08:33pm

      I am interested in geting started to build an app which can open a PDF file and automatically navigate to a page number. Can You help? Thanks

    • DDE Help..

      Posted by GremlinSA on 04/17/2007 03:33pm

      One little problem here is that this msgbox is hardcoded into excel.. What you could do is try the link to the DDE server within your App before openning excel.. And if the link fails, ShellExecute the Server App...

  • dde dll or exe

    Posted by joemerchant on 12/07/2006 10:32pm

    I use dde in vb6 to get the url from web browsers such as firefox. I'd like to migrate my application to vb.net for a few reasons. Problem is dde isn't available in vb.net. Therefore, I thought I'd create a activex exe in vb6 just for the dde. Only problem is that activeX exe shows up in the windows task manager program list everytime the aX is called. I looked into making it a dde dll but I need a form for the textbox dde links to work. Ideas?

    • getting close

      Posted by GremlinSA on 12/21/2006 08:49pm

      While working on the article i've come across the same problem ... I'm looking into how to sort this out....

    • getting close but problem.

      Posted by joemerchant on 12/08/2006 07:10pm

      WHen I run my code in the activex dll and set the linkmode = 2 then my program returns "Operation not valid in an ActiveX DLL" ideas?

    • dde in ActiveX

      Posted by GremlinSA on 12/08/2006 09:41am

      With an ActiveX Dll you can still add a form and text box, In your Class activation you create and load the form and hide it. Now you have a DDE Aware App in VB.Net I may write another article on how to get DDE links in VB.net , using a VB6 dll...

  • Network DDE / DSDM

    Posted by TooMuchFun on 10/11/2006 02:57am

    Nice to see information on a somewhat forgotten interface. What role do these (normally off) services play in the functionality of DDE?

  • Is DDE always available ?

    Posted by RobCrombie on 06/14/2006 04:56am

    Nice simple article.
    Is there a catch ?
    Is DDE always available on clients machinnes ?

    • DDE in Vista ?

      Posted by GremlinSA on 04/17/2007 03:39pm

      AFIK it works.. Havn't tested it personally, but i see no reaso for it not too work....

    • DDE in Vista ?

      Posted by RobCrombie on 04/17/2007 01:27pm

      Richard, Thanks for your reply. I didn't know it was there (I obviously need a follow-up system). I was just googling, and came across your article(again), and noticed my previous question. Will DDE work ok in VB6 apps running in Vista ? Thanks again, Rob

    • Is DDE Always available ???

      Posted by GremlinSA on 06/17/2006 10:45am

      Yes... DDE is always availavble.. MS Office uses DDE extensively to pass data back and forth.. The thing withh DDE is it is application dependant.. IE. If the application is written to accept DDE links... you can use DDE links...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date