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

Comments
DDE
Posted by Keithuk on 12/08/2009 12:21pmdde help
Posted by ctonge on 03/09/2007 01:14pmI'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.
-
-
ReplyPDF navigation using visual basic 2005
Posted by georgestef on 10/20/2007 01:33pmI 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
ReplyDDE Help..
Posted by GremlinSA on 04/17/2007 08:33amOne 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...
Replydde dll or exe
Posted by joemerchant on 12/07/2006 02:32pmI 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?
-
-
-
Replygetting close
Posted by GremlinSA on 12/21/2006 12:49pmWhile working on the article i've come across the same problem ... I'm looking into how to sort this out....
Replygetting close but problem.
Posted by joemerchant on 12/08/2006 11:10amWHen 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?
Replydde in ActiveX
Posted by GremlinSA on 12/08/2006 01:41amWith 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...
ReplyNetwork DDE / DSDM
Posted by TooMuchFun on 10/10/2006 07:57pmNice to see information on a somewhat forgotten interface. What role do these (normally off) services play in the functionality of DDE?
ReplyIs DDE always available ?
Posted by RobCrombie on 06/13/2006 09:56pm-
-
-
ReplyDDE in Vista ?
Posted by GremlinSA on 04/17/2007 08:39amAFIK it works.. Havn't tested it personally, but i see no reaso for it not too work....
ReplyDDE in Vista ?
Posted by RobCrombie on 04/17/2007 06:27amRichard, 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
ReplyIs DDE Always available ???
Posted by GremlinSA on 06/17/2006 03:45amYes... 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