An example DDE client
In this example, a DDE client exchanges data with the example DDE server created previously. This client connects to the server, sets up a hotlink to the time and date, and prints it in the Transcript window when it changes.
First, create a class called DdeTimeClient that is a subclass of Object. It will have the ddeClient instance variable that represents the instance of the DdeClient class that you will create.
Thus, your new class looks like:
Object subclass: #DdeTimeClient
instanceVariableNames: 'ddeClient '
classVariableNames: ''
poolDictionaries: 'DdeConstants '
Include the pool dictionary DdeConstants because you are going to hook into a callback that needs this pool dictionary.
Now, add a class method new to create a new client. Also send the initialize method to the new object in order to set it up.
"Create a new timer client and initialize it."
^super new initialize
Now you create the initialize method. This method creates the DdeClient object and connects it to the server. Next it hooks up to the hotlink callback so that when the data at the server changes, the client is notified. Then the application attempts to hotlink to the time and date items at the server.
"Initialize the dde client."
ddeClient := DdeClient new.
(ddeClient connect: 'Timer' topic: 'Time') ifFalse: [
^Transcript cr; show: 'Client cannot connect'.
addCallback: DdeNdataCallback
receiver: self
selector: #data:clientData:callData:
clientData: nil.
(ddeClient hotLink: 'time' format: 'Smalltalk Object')
ifFalse: [
show:'Cannot hotlink to time'
(ddeClient hotLink: 'date' format: 'Smalltalk Object')
ifFalse: [
show:'Cannot hotlink to time'
Next, create the hotlink callback method. All callbacks take three parameters: the DDE object calling the method, the client data passed when the callback was hooked, and the DdeCallbackData object containing the information about the callback.
When the callback is run the method unflattens the data in the DdeCallbackData object from a ByteArray into a Smalltalk object (because this is what we hotlinked to in the first place). The unflatten: method takes a ByteArray and uses the VA Smalltalk Swapper to convert it back into a Smalltalk object. The code for doing this is specified in Converting Smalltalk objects to a ByteArray and back.
After converting the data back to a Smalltalk object, the method then prints the object in the Transcript window.
data: aDdeClient clientData: clientData callData: callData
"Hotlink callback - unflatten the data and display it."
| data |
data := self unflatten: callData data.
show: data printString.
The final method that you need to implement is the free method. This method frees up the DdeClient.
"Free up the dde client resource."
ddeClient free.
Last modified date: 01/29/2015