Using DDE

Dynamic Data Exchange (DDE) sends data to and receives data from other applications. With Delphi, you can use this data to exchange text with other applications. You can also send commands and macros to other applications, so your application can control other applications.

Here's a typical way to use DDE: a link between two applications is established, either by your application or the other application. Once this link (called a conversation) is established, the two applications can continuously and automatically send data back and forth. A typical use for DDE is to exchange text data. When the text changes in one application, DDE automatically updates the text in the other. For example, in the following figure, text changes in the Delphi application are automatically reflected in Word.

Figure 14.1 A DDE conversation between a Delphi application and Microsoft Word 6.0.

Figure 14.1 A DDE conversation between a Delphi application and Microsoft Word 6.0.

Delphi Microsoft Word
— Linked text —

Note Not all applications support DDE. To determine whether an application supports DDE, refer to its documentation.

To understand DDE applications, you need to become familiar with the concept of DDE conversations.

DDE conversations

In order to have a DDE conversation, there must be a DDE client application and a DDE server application. The application that requests data is the client. The application that provides the requested data, and updates the DDE client, is the DDE server. With Delphi, you can create both DDE clients and DDE servers. In fact, a single Delphi application can be both a DDE client and a DDE server at the same time. A DDE conversation is defined by its service, topic, and item.

Note Sometimes DDE clients are called destinations, and DDE servers are called sources. The terminology is interchangeable.

This section explains

  • DDE services
  • DDE topics
  • DDE items

DDE services

The service of a conversation is the name of the DDE server. Typically, this is the server application's main executable file without the .EXE extension. For example, if you want your application to establish a conversation with Novell Quattro Pro 6.0 (QPW.EXE), the conversation service is QPW.

Sometimes, however, the service differs from the main executable file name. For example, if you want your application to establish a conversation with Borland ReportSmith 2.0 (RPTSMITH.EXE), the DDE service would be ReportSmith.

If the server is a Delphi application, the service is the project name without the .DPR or .EXE extension. For example, to establish a conversation with PROJ1.DPR the service is PROJ1.

Note The DDE service depends on the DDE server application. Refer to the documentation of the DDE server for specific information about specifying the DDE service.

Note Sometimes DDE services are called application names. The terminology is interchangeable.

DDE topics

The topic of a DDE conversation is a unit of data, identifiable to the server, containing the linked information. Typically, the topic is a file. For example, if the data you want to share is in the Quattro Pro notebook file NOTEBK1.WB2, the topic would be NOTEBK1.WB2; the full file name is used, including the extension.

If the server is a Delphi application, the topic is (by default) the caption of the form containing the data you want to link. For example, if you want to link to text in a form with a Caption of MyApp, the topic would be MyApp. (Optionally, you could use the name of a DDEServerConv component as explained in "Creating DDE server applications" on page 388.)

DDE items

The item of a DDE conversation identifies the actual piece of data to link. The syntax used for specifying the DDE item depends on the DDE server application. Examples of possible DDE items are spreadsheet cells or database fields. For example, to link to cell A1 on page A in a Quattro Pro notebook, the item would be $A:$A$1..$A$1.

If the server is a Delphi application, the item is the name of the linked DDEServerItem component. For example, to establish a conversation with the DDE server component named DDEServer1, the item would be DDEServerl. DDEServerItems are discussed in "Creating DDE server applications" on page 388.

Note See the documentation for the DDE server for specific information about specifying the application, topic, and item of a conversation.

Creating DDE client applications n [

DDE client applications initiate conversations and typically request to receive data from DDE servers. Client applications can also send data to servers, which is called poking data. They can also execute macros of servers with macro capabilities.

■ To create a DDE client to be automatically updated by a DDE server,

1 Add a DDEClientConv and a DDEClientItem component to a form.

2 Assign the name of the DDEClientConv component to the DDEConv property of the DDEClientItem component. To establish a link at design time, choose this value from a list of possible conversations for DDEConv in the Object Inspector. To establish a link at run time, your application should execute code that assigns the value to the DDEConv property.

► For example, the following code links an DDEClientItem component named DDEClientItem1 to a DDEClientConv component named DDEClientConv1:

DDEClientlteml.DDEConv := 'DDEClientConvl';

■ To create a DDE client to be manually updated by a DDE server, add only a DDEClientConv component to a form. To be updated, your client application must execute code that calls the RequestData method of the DDEClientConv component. See "Requesting data" on page 386 for more information.

Of course, no DDE conversation is yet taking place, because the DDEClientConv component has not yet been connected to a DDE server. Nonetheless, you have "set the stage" for this conversation to take place from your Delphi application. The next sections explain

  • Establishing a link with a DDE server
  • Requesting data
  • Poking data
  • Controlling other applications

Establishing a link with a DDE server

If you have access to the DDE server application and data, you can establish a DDE link by pasting it from the Clipboard at design time.

■ To paste a DDE link from the Clipboard at design time,

1 Activate the server application and select the data to link to your client application.

► For example, to link to a cell in a Quattro Pro notebook called C:\NOTEBK1.WB2, run Quattro Pro, load the notebook, and select the cell.

2 Copy the data and DDE link information to the Clipboard from the server application. Typically, you do this by choosing Copy from the Edit menu of the server.

► For example, in Quattro Pro choose EditlCopy.

3 Activate Delphi and select the DDEClientConv component.

4 Click either the DDEService or the DDETopic property in the Object Inspector, then click the ellipsis (...) button.

The DDE Info dialog box appears. Figure 14.2 The DDE Info dialog box

— DDE Info

1

Add: i

Topic:

Paste Link |

\<S OK J 1 X Cancel |

5 Choose Paste Link.

The App and Topic boxes are filled in with the correct values automatically. If the Paste Link button is disabled, then the application you intended to be the server does not support DDE, or the DDE information was not successfully copied to the Clipboard.

► For example, if you copy from the Quattro Pro notebook C:\NOTEBK1.WB2, App is QPW and Topic is C:\NOTEBK1.WB2.

6 Choose OK.

The DDEService and DDETopic properties now contain the appropriate values to establish a DDE link.

7 Select the DDEClientItem component and choose the name of the linked DDEClientConv component for the DDEConv property from the list in the Object Inspector.

8 If the contents of the Clipboard haven't changed since you completed step 2, then the Clipboard contains a value for the DDE item of the conversation. Choose this value for the DDEItem property from the drop-down list in the Object Inspector. If the contents of the Clipboard do not contain a value for the DDE item, type the appropriate value for DDEItem in the Object Inspector.

  • For example, if you copy cell A1 from page A in the Quattro Pro notebook, select or type $A:$A$1..$A$1 for DDEItem. Remember that you also need to provide your application with an area and a means to display the data being exchanged. The DDEItem component is nonvisual, so it serves only as a means of access, not a means of display.
  • To establish a DDE link at run time, specify the service and topic of the conversation, using the SetLink method of the DDEClientConv component.

The first parameter of SetLink is a string specifying the service, and the second parameter is a string specifying the topic. SetLink assigns these strings to the DDEService and DDETopic properties of the DDEClientConv component, respectively. For more information about this method, search online Help for SetLink.

Note Simply assigning values to the DDEService and DDETopic properties of a

DDEClientConv component does not establish a link at run time. You need to use the SetLink method to property initiate a DDE conversation.

► For example, execute the following line of code to link to a Quattro Pro notebook named C:\NOTEBK1.WB2:

DDEClientConv1.SetLink('QPW', 'C:\NOTEBK1.WB2');

  • After using the SetLink method, you need to assign the DDE item of the conversation to the DDEItem property of the DDEClientItem component.
  • For example, execute the following line of code to link to cell A1 on page A of the Quattro Pro notebook:

When a DDE link has been established, the linked data appears in the Text and Lines properties of the DDEClientItem component. Text specifies a string, and Lines specifies a

TStrings object. Text can contain up to 255 characters of linked text data. If the text data from the DDE server is longer than 255 characters, use Lines to access the data. The data is continuously updated by the DDE server, and an OnChange event of the DDEClientItem component occurs whenever the data changes.

  • One way to process linked text data is to display it in an edit box (Edit component). To do this, you assign the value of the Text property of the DDEClientItem to the Text property of the edit box (or, for larger bodies of text, you could assign the Lines property of the DDEClientItem to the Lines property of a memo). Attach the assignment statement to the OnChange event handler of the DDEClientItem.
  • For example, if the DDEClientItem component is named DDEClientItem1 and the edit box is named Edit1, execute the following code:

Editl.Text := DDEClientlteml.Text;

Requesting data

Some DDE items aren't passed automatically in a DDE conversation. Instead, the client must explicitly request a specific item to be updated by the DDE server. Also you might want your client application to obtain data from a DDE server once, and not be continually updated by the DDE server. In these cases, your DDE client should request to be updated with the RequestData method.

Note To determine if your DDE client must explicitly request to be updated, see the documentation of the DDE server application.

■ To request data, use the RequestData method of a DDEClientConv component. Specify the item to request as a parameter of RequestData. The item is the simply a DDE item. RequestData returns a PChar null-terminated string containing the requested text.

The requested data is given to the DDE client only in the return value of RequestData. Even if the DDEClientConv component is connected to a DDEClientItem component, RequestData won't update the values of the Text or Lines properties of the DDEClientItem. Search online Help for RequestData for more information about this method.

► For example, to request the data from cell A1 on page A in a Quattro Pro notebook, and store the data in a PChar variable named TheData, execute the following code:

TheData := DDEClientConv1.RequestData('$A:$A$1..$A$1');

Note The memory for the PChar string returned by RequestData must be freed after your application is finished with it. Use the StrDispose function to do this.

► For example, after your application has processed TheData, you should deallocate it with the following code:

StrDispose(TheData);

Poking data

Poking data means sending data from your DDE client application to the DDE server application, directly opposite the usual data flow direction for DDE.

■ To poke data, use the PokeData or PokeDataLines methods of a DDEClientConv component. PokeData pokes a PChar string, while PokeDataLines pokes a TStrings object.

The first parameter of PokeData specifies the item of the DDE conversation (usually specified in the DDEItem property of the associated DDEClientltem component). The second parameter of PokeData is a PChar containing the text to send. PokeDataLines functions similarly, except its second parameter is of type TStrings. Search online Help for PokeData or PokeDataLines for more information about these methods.

► For example, to send the text 'Hello' from a DDEClientConv component named DDEClientConv1 to a linked DDE server, execute the following code, assuming that TheText is of type PChar:

StrPCopy(TheText, 'Hello');

DDEClientConvl.PokeData(DDEClientItem1.DDEItem, TheText);

The text string "Hello" is inserted into the DDE item specified in the DDEItem property of DDEClientItem1.

Note StrPCopy simply copies a Pascal-style string into a null-terminated PChar string.

Just as you need to provide an area and a means to display DDE data in your application, so you also need to provide a source for the data you want your application to poke. In the previous example, the text string itself was passed as a parameter, but in most of your "real world" applications, the DDEClientConv control probably obtains this data from another control.

Controlling other applications

All DDE client applications can control server applications in one particular way: clients attempt to run servers, when necessary. When your DDE client tries to establish a link with a DDE server that isn't running, the client attempts to activate the server and load the conversation topic (specified in the DDETopic property of the DDEClientConv component).

■ To activate a DDE server when the client form is created, set the value of the ConnectMode property of a DDEClientConv component to dmAutomatic.

If the value of ConnectMode is dmManual, your application must execute the OpenLink method of the DDEClientConv component.

Another way to control other applications is to execute macro commands.

  • To execute a macro in the DDE server from within the DDE client, use the ExecuteMacro or ExecuteMacroLines methods of the DDEClientConv component to send text containing one or more macro commands to the server. The server then processes the macro. ExecuteMacro sends a PChar string, while ExecuteMacroLines sends a TStrings object.
  • For example, you could tell Microsoft Word 6.0 to close its active worksheet by executing the following code in your client application, assuming that TheMacro is of type PChar:

StrPCopy(TheMacro, '[FileClose(2)]'); DDEClientConv1.ExecuteMacro(TheMacro, False);

In this example, the second parameter is a "wait flag." It specifies whether your client application should wait for the server application to indicate that it has finished processing the macro before sending any more DDE data to the server. If False, your application sends more data to the server if you execute another ExecuteMacro, ExecuteMacroLines, PokeData, or PokeDataLines method. If True, subsequent calls to these methods before the server has indicated the completion of the first macro do not send data. Search online Help for ExecuteMacro or ExecuteMacroLines for more information about these methods.

Note Not all DDE servers can process macros. See the documentation for the server application to determine whether it supports macros and for its macro syntax.

Creating DDE server applications

DDE server applications respond to DDE clients. Typically, they contain data to which the client application needs access. Servers simply update clients.

■ To create a DDE server, add a DDEServerItem component to a form.

The Text or Lines properties of the DDEServerItem component contain the data to link. Your server application continuously updates all clients with the value of the Text or Lines properties.

As with the DDEClientItem component, Text specifies a string, while Lines specifies a TStrings object. If the text to exchange is longer than 255 characters, use the Lines property to specify the data. When either Text or Lines is modified, the other is automatically updated to reflect the change. The value of Text always equals the value of the first line of Lines.

Optionally, you can add a DDEServerConv component and link the DDEServerItem and DDEServerConv together. When you add a DDEServerConv component, the DDE topic becomes the name of the DDEServerConv component (instead of theCaption of the form containing the DDEServerItem component). You should use a DDEServerConv component in the following situations:

  • The value of the Caption property of the form containing the DDEClientItem component might change, or might not be unique at run time. If this value is not unique or constant, the link might not be able to be established.
  • The DDE client might send a macro to your server application for processing. If this happens, an OnMacroExecute event of the DDEServerConv component occurs. The OnMacroExecute event contains a string parameter that specifies the macro sent from the DDE client. Search online Help for OnMacroExecute for more information about this event.
  • To create a DDE server using a DDEServerConv component,

1 Add DDEServerItem component and a DDEServerConv component to a form.

2 Assign the name of the DDEServerConv component to the ServerConv property of the DDEServerItem component.

At design time, choose this value from a list of possible conversations for ServerConv in the Object Inspector. To establish this link at run time, your application should execute code that assigns the value to the ServerConv property.

► For example, the following code links a DDEServerItem component named DDEServerItem1 to a DDEServerConv component named DDEServerConv1:

DDEServerItem1.ServerConv := 'DDEServerConv1';

Establishing a link with a DDE client application

Typically, it is up to the DDE client to establish the link with a DDE server application. If you want to test a link from your DDE server, you can use the Clipboard to paste a link.

1 Use the CopyToClipboard method of the DDEServerItem component to copy the value of the Text (or Lines) property, along with DDE link information, to the Clipboard.

2 Insert the linked data into the DDE client application. Typically, do this by choosing the appropriate command (such as EditlPaste Special or EditlPaste Link) of the client application.

Search online Help for CopyToClipboard for more information about this event.

► For example, to create a link from a DDEServerItem component named DDEServerItem1 to a WordPerfect 6.0 document, perform the following steps. (If you don't have WordPerfect installed, this example is worth examining because the steps required are similar for any other DDE client application that can paste links.)

1 At run time, your DDE server application should execute the following code: DDEServerItem1.CopyToClipboard;

2 Activate WordPerfect and choose Edit l Paste Special. The WordPerfect Paste Special dialog box appears. Figure 14.3 The WordPerfect Paste Special dialog box

=

Paste Special

I

Source; DDESERV DdeServerConvl DdeServerl

1 Paste Link || Paste 1

Data Type:

I Cancel 1

1 1

Help

The Paste Special dialog box closes and the linked text from the Text (or Lines) property of DDEServerItem1 appears at the insertion point in the WordPerfect document. When the value of Text (or Lines) changes, the text in the WordPerfect document is updated automatically.

Note The method for establishing a DDE link depends on the DDE client application. See the documentation for the client for specific information about establishing DDE links. If the DDE client is another Delphi application, refer to "Establishing a link with a DDE server" on page 384.

  • One way to process linked text data is to keep the client updated to changes in an edit box (Edit component) in your DDE server application. Assign the value of the Text property of an edit box to the Text property of the DDEServerItem (or, for larger bodies of text, you could assign the Lines property of a memo to the Lines property of the DDEClientItem). Attach the assignment statement to the OnChange event of the edit box or memo.
  • For example, if the edit box is named Editl and the DDEServerItem component is named DDEServerItem1, execute the following code:

DDEServerlteml.Text := Editl.Text;

Was this article helpful?

+3 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Responses

  • TOLMAN
    How to use DDEServerConv delphi?
    10 years ago
  • Flora
    How use SetLink delphi?
    9 years ago
  • maia
    How to check if the dde client is connected?
    9 years ago
  • welde
    How to create an application using dde?
    9 years ago
  • Phillipp
    Do all applications support dde?
    9 years ago

Post a comment