DDE Sample Code

HostExplorer Programming

DDE Sample Code

The following is an example of logging in to a CMS account, written in Word for Windows Basic language. You can use the sample WordBasic macro to copy the current screen image from HostExplorer (configured as EHLLAPI session "A") to your Word document at the current insertion point. The macro uses DDE to connect to the emulator and copy the emulator line by line.

In the DDE sample code, HostExplorer lets you create login scripts to connect to remote hosts.

The first command is the On Error Command. Always include an On Error Command to make sure that the DDE link is terminated when the macro is finished. If you do not use the command, you may use all available DDE sources and be unable to execute the macro properly.

The macro begins by retrieving the number of rows and columns in the current presentation space. Because data returned by DDE is always in string format, you must use the Val() function to convert the data to numeric values. The macro proceeds to copy the screen line by line. The request$ = ... line is where all the work is prepared.

This command builds a string of the format PxxLyy, where xx is the screen position (based from 1) and yy is the line length. Because of this, the macro issues requests for data such as P1L80, P81L80, and P161L80, to copy line by line. The data is then inserted into the current document using the Insert command. The last step of the macro is to close down the DDE connection.

Sub Main
    ChanNum = DDEInitiate( "HOSTEX", "A" )
    DDEPoke ChanNum, "Keystroke", "LOGIN PIERRE@E"
    DDEExecute ChanNum, "[Wait Unlock(6)]"
    DDEPoke ChanNum, "Keystroke", password@E
    DDETerminate ChanNum
    MessageBox "Logged into CMS successful", "Information"
End Sub
Sub Main
    On Error Goto ErrorHandler
    crlf$ = Chr$(13) + Chr$(10)
    iChanNum = DDEInitiate("HOSTEX", "A")
    iNumRows = Val(DDERequest$(iChanNum, "Rows"))
    iNumCols = Val(DDERequest$(iChanNum, "Columns"))

    If iChanNum Then
      For row = 1 To iNumRows
         request$ = "P" + Mid$(Str$(1 + ((row  1) * iNumCols)), 2) +
        "L" + LTrim$(Str$(iNumCols))
        Data$ = Data$ + DDERequest$(iChanNum, request$) + crlf$
      Next row
      Insert Data$
    Else 'could not open session
       MsgBox "Could not open DDE Session with program."
    End If
    ErrorHandler:
    DDETerminate iChanNum
End Sub


Related Topics

DDE Terminology