Introduction to DDE

 

Foreword

 

Definition of Dynamic data exchange: Dynamic data exchange (DDE) is a form of interprocess communication that uses shared memory to exchange data between applications. Applications can use DDE for one-time data transfers and for ongoing exchanges and updating of data.

In this report, the use of DDE functions will be made clear and a detailed description will be given too. You need to use a lot of functions to let two or more applications work together, as one function needs another one.

This report also describe all the functions in detail. The syntax, return values and the arguments will be explained with a little description with each argument.

 

1. Introduction to DDE

 

DDE is an interprocess communication method that allows two or more applications running simultaneously to exchange data and commands. This means that two or more applications can work together to create a new application One way to enable DDE is by use of the function DDEAdvise ( )

1.1 DDEAdvise ( )

DDEAdvise( ) is used to create a notify link or an automatic link to an item name in a server application. When a notify link is created with DDEAdvise( ), the server application notifies that the item name has been modified. If an automatic link is created, the server application notifies that the item name has been modified and passes the new data to Visual C++. You can also use DDEAdvise( ) to turn off notification from the server. Before you can create a link, you must establish a channel to the server application with DDEInitiate( ).

1.2 DDEInitiate( )

DDEInitiate( ) establishes a DDE channel between Visual C+++ and a DDE server application. Once a channel is established, Visual C++ can request data from the server by referring to the channel in subsequent DDE functions. Visual C++ acts as the client, requesting data from the server application through the channel. If the channel is successfully established, DDEInitiate( ) returns the channel number. Channel numbers are non-negative, and the number of channels you can establish is limited only by your system resources. DDEInitiate( ) returns –1 if the channel cannot be established. If the server application isn't open, Visual C++ asks if you would like to open it. If you choose Yes, Visual C++ attempts to open the application. (You can use DDELastError( ) to determine why a channel cannot be established.) To avoid being asked whether you want to open the application, set the DDESetOption( ) SAFETY option. You can also use RUN with the /N option to start the application. A channel can be closed with DDETerminate( ).

1.3 DDETerminate( )

If the channel is successfully closed, DDETerminate( ) returns true (.T.). If the channel cannot be closed, DDETerminate( ) returns false (.F.). Be sure to close channels as soon as they are no longer needed to conserve system resources. All channels are automatically closed if you exit Visual C++ by choosing Exit from the File menu

1.4 DDERequest ( )

Another way to enable DDE is by using DDERequest. Before you can request data using DDERequest( ), you must establish a channel to the server application with DDEInitiate( ). If the request for data is successful, DDERequest( ) returns the data as a character string. If the request fails, DDERequest( ) returns an empty string and DDELastError( ) returns a nonzero value. If you include the asynchronous user-defined function cUDFName, DDERequest( ) returns a transaction number if successful, or –1 if an error occurs.

1.5 DDEExecute ( )

A third method to enable DDE is DDEExecute. The command sent with DDEExecute( ) must be understood by the application. Before you can execute the command, you must establish a channel to the server application with DDEInitiate( ). For example, Microsoft Excel has an extensive set of macro commands, including DDE commands that let you request data from Visual C++ from within Microsoft Excel. If you establish a channel to Excel, you can then use DDEExecute( ) to send macro commands to Excel from within Visual C++ If the receiving application successfully executes the command, DDEExecute( ) returns true (.T.). If the receiving application cannot successfully execute the command or if the channel number you include is not valid, DDEExecute( ) returns false (.F.). If the optional asynchronous user-defined function cUDFName is included, a transaction number is returned. If an error occurs, DDEExecute( ) returns –1.

1.6 DDELastError ( )

You can use DDELastError( ) to help determine the cause of an error when a DDE function doesn't execute successfully. DDELastError( ) returns 0 if the last DDE function executed successfully. It returns a nonzero value if the last DDE function was unsuccessful. The following table lists the error numbers and their descriptions (figure number 1).

Error number

Description

1

Service busy

2

Topic busy

3

Channel busy

4

No such service

5

No such topic

6

Bad channel

7

Insufficient memory

8

Acknowledge timeout

9

Request timeout

10

No DDEInitiate( )

11

Client attempted server transaction

12

Execute timeout

13

Bad parameter

14

Low memory

15

Memory error

16

Connect failure

17

Request failure

18

Poke timeout

19

Could not display message

20

Multiple synchronous transactions

21

Server died

22

Internal DDE error

23

Advise timeout

24

Invalid transaction identifier

25

Unknown

Figure 1: Error numbers

2. Explanation of the DDE functions

This paragraph will explain the different functions by giving an example of the use of the functions. For each function will the syntax, return value and the arguments be given.

2.1 DDEAdvise ( ) details Syntax

DDEAdvise(nChannelNumber, cItemName, cUDFName, nLinkType)

Return values

Logical

Arguments

nChannelNumber

Specifies the channel number.

cItemName

Specifies the item name. For example, Microsoft Excel uses row and column notation to refer to cells in a worksheet. The item name R1C1 designates the cell in the first row and first column of the worksheet.

cUDFName

Specifies the user-defined function that is executed when a notify link or an automatic link is established and the item cItemName is modified. When the user-defined function is executed, it is passed the following six parameters in the order given below:

2.2 DDEInitiate ( ) details Syntax

DDEInitiate(cServiceName, cTopicName)

Return values

Numeric

Arguments

cServiceName

Specifies the service name of the server application which, in most cases, is the name of the executable file without its extension. If you are establishing a channel to Microsoft Excel, cServiceName is Excel.

cTopicName

Specifies the topic name. The topic is application-specific and must be understood by the application. For example, one topic supplied by most DDE servers is the System topic. See the application documentation for the service and topic names supported by the application.

2.3 DDETerminate details Syntax

DDETerminate(nChannelNumber | cServiceName)

Return values

Logical

Arguments

nChannelNumber

Specifies the channel number to close.

cServiceName

Specifies the service name to close.

2.4 DDERequest ( ) details Syntax

DDERequest(nChannelNumber, cItemName [, cDataFormat [, cUDFName]])

Return values

Character

Arguments

nChannelNumber

Specifies the channel number of the server application.

cItemName

Specifies the item name. The item name is application-specific and must be understood by the application. For example, Microsoft Excel uses row and column notation to refer to cells in a worksheet. The item name R1C1 designates the cell in the first row and first column of the worksheet.

cDataFormat

Specifies a format for the data requested. The default format is CF_TEXT. In this format, fields are delimited with tabs and records are delimited with a carriage return and a line feed.

cUDFNameAllows an asynchronous data transfer. If you omit cUDFName, Visual C++ waits for the data from the server for the period specified with DDESetOption( ). If you specify the name of a user-defined function with cUDFName, Visual C++ continues program execution immediately after the request is made. When the data is available from the server application, the user-defined function specified with cUDFName is executed. The user-defined function is passed six parameters in this order: (see figure number 2)

 

Parameter

Contents

Channel Number

The channel number of the server application.

Action

XACTCOMPLETE (successful transaction). XACTFAIL (failed transaction).

Item

The item name; for example, R1C1 for a Microsoft Excel worksheet cell.

Data

The new data (REQUEST) or data passed (POKE or EXECUTED).

Format

The data format; for example, CF_TEXT.

Transaction Number

The transaction number returned by DDERequest( ).

Figure 2: cUDFName parameters

If the transaction fails, you can use DDELastError( ) to determine why it failed. When you include cUDFName, DDERequest( ) returns a transaction number equal to or greater than 0 if successful, or –1 if an error occurs.

2.5 DDEExecute ( ) details Syntax

DDEExecute(nChannelNumber, cCommand [, cUDFName])

Return values

Logical

Arguments

nChannelNumber

Specifies the channel number.

cCommand

Specifies the command you want to send to the other application. The format of the command is determined by the application you are sending it to. Consult the application's documentation for the correct syntax.

cUDFNameAllows asynchronous command execution requests. If you omit cUDFName, a client application waits for the period specified with DDESetOption( ). If you specify a user-defined function with cUDFName, client program execution continues immediately after the command execution request is made. When the server application finishes executing the command, the user-defined function you specify with cUDFName is executed. The user-defined function is passed six parameters in the order shown in the above table (see figure number 2).

2.6 DDELastError ( ) details Syntax

DDELastError( )

Return values

Numeric

Afterword

As you could read in this report, the use of DDE has several advantages and disadvantages. One of the main advantages is that the use of DDE results in applications using DDE for one-time data transfers and for ongoing exchanges and updating of data.

When an application is not responding, the function DDELastError will give an error number, of which a description can be found in the following table: (see figure number 1). One minor problem is that not al error descriptions are clear. For example: error number 13 / bad parameter. Usually no further description is given so you have to find out for yourself what to change.