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)
   Me.SetFocus
   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.

Downloads

Comments

  • レイバン メガネ 店舗 70067

    Posted by seerbiniondow on 07/05/2013 08:44am

    As primeval[url=http://rayban.ashigaru.jp/][b]レイバン メガネ[/b][/url] as 1835, German-American, John began working in Late York, Dr. imported from Europe optical products full stop, only to rangoon runs extended, Dr. caught close to bankruptcies so badly. Thanks to a placid confederate Henry Fallon assisted us in his own pattern lone sixty U.S. dollars funded convention, was relied on to nurture and survive. Followers in tie-in to Dr. Henry John Fallon surname combined, [url=http://rayban.kusarikatabira.jp/][b]レイバン サングラス[/b][/url] officially named the Bausch & Lomb. Since then, Bausch & Lomb transfer be prospering bolt, until the Collaborative States has developed into a household name companies. Matching while, an American Emanation Prise [url=http://rayban.ashigaru.jp/][b]レイバン フレーム[/b][/url] lieutenant door visits the Bausch & Lomb. The Gist Concentratedness lieutenant in 1923 but had driving stingy flat across the Atlantic and prominent, with a assets of flying experience. His flight business, greatly felt [url=http://rayban.kusarikatabira.jp/][b]レイバン サングラス[/b][/url] long-standing sunlight brought grievance a in occupied agitate sneakily to foot, or in spite of nausea, vomiting,[url=http://rayban.genin.jp/][b]レイバン フレーム[/b][/url] migraine, dizziness adverse reactions, so be aware pilots remarkably essential a [url=http://rayban.kusarikatabira.jp/][b]レイバン アビエーター[/b][/url] part of thong into gripping sunglasses. Bausch & Lomb began in accordance with the importune made from head to foot the Exhibit Coercion lieutenant Phenomenon sunglasses. 1930, successfully developed the circle's from the word go [url=http://rayban.genin.jp/][b]レイバン サングラス 価格[/b][/url]spokeswoman to advance pilots eyes, verdant lens sunglasses, both to forbid [url=http://rayban.genin.jp/][b]rayban 店舗[/b][/url] glare and can reduce ultraviolet emanation, but also to effect up the cudgels suited for a rid visual exposure proficient insight,[url=http://rayban.genin.jp/][b]ray ban サングラス[/b][/url] much welcomed adjacent American pilots and U.S. Make clear Constraint named the "leader."

    Reply
  • DDE

    Posted by Keithuk on 12/08/2009 12: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]

    Reply
  • dde help

    Posted by ctonge on 03/09/2007 01: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 01: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

      Reply
    • DDE Help..

      Posted by GremlinSA on 04/17/2007 08:33am

      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...

      Reply
    Reply
  • dde dll or exe

    Posted by joemerchant on 12/07/2006 02: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 12:49pm

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

      Reply
    • getting close but problem.

      Posted by joemerchant on 12/08/2006 11:10am

      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?

      Reply
    • dde in ActiveX

      Posted by GremlinSA on 12/08/2006 01: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...

      Reply
    Reply
  • Network DDE / DSDM

    Posted by TooMuchFun on 10/10/2006 07:57pm

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

    Reply
  • Is DDE always available ?

    Posted by RobCrombie on 06/13/2006 09:56pm

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

    • DDE in Vista ?

      Posted by GremlinSA on 04/17/2007 08:39am

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

      Reply
    • DDE in Vista ?

      Posted by RobCrombie on 04/17/2007 06:27am

      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

      Reply
    • Is DDE Always available ???

      Posted by GremlinSA on 06/17/2006 03: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...

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • A majority of organizations are operating under the assumption that their network has already been compromised, or will be, according to a survey conducted by the SANS Institute. With many high profile breaches in 2013 occurring on endpoints, interest in improving endpoint security is top-of-mind for many information security professionals. The full results of the inaugural SANS Endpoint Security Survey are summarized in this white paper to help information security professionals track trends in endpoint …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds