home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / dde2 / userdde.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1993-05-16  |  24.2 KB  |  759 lines

  1. VERSION 2.00
  2. Begin Form USERDDE 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Interactive DDE"
  5.    ClientHeight    =   5070
  6.    ClientLeft      =   630
  7.    ClientTop       =   1455
  8.    ClientWidth     =   5775
  9.    Height          =   5760
  10.    Icon            =   USERDDE.FRX:0000
  11.    Left            =   570
  12.    LinkMode        =   1  'Source
  13.    LinkTopic       =   "UserDDE"
  14.    ScaleHeight     =   5070
  15.    ScaleWidth      =   5775
  16.    Top             =   825
  17.    Width           =   5895
  18.    Begin Timer timNotify 
  19.       Enabled         =   0   'False
  20.       Left            =   5880
  21.       Top             =   4560
  22.    End
  23.    Begin SSFrame frmLinkMode 
  24.       Caption         =   "Link Mode"
  25.       Font3D          =   0  'None
  26.       Height          =   1935
  27.       Left            =   2760
  28.       TabIndex        =   17
  29.       Top             =   120
  30.       Width           =   1455
  31.       Begin SSOption optLinkMode 
  32.          Caption         =   "Noti&fy"
  33.          Font3D          =   0  'None
  34.          Height          =   255
  35.          HelpContextID   =   1130
  36.          Index           =   3
  37.          Left            =   120
  38.          TabIndex        =   21
  39.          Top             =   1440
  40.          Width           =   1215
  41.       End
  42.       Begin SSOption optLinkMode 
  43.          Caption         =   "&Manual"
  44.          Font3D          =   0  'None
  45.          Height          =   255
  46.          HelpContextID   =   1110
  47.          Index           =   2
  48.          Left            =   120
  49.          TabIndex        =   20
  50.          Top             =   1080
  51.          Width           =   1215
  52.       End
  53.       Begin SSOption optLinkMode 
  54.          Caption         =   "&Automatic"
  55.          Font3D          =   0  'None
  56.          Height          =   255
  57.          HelpContextID   =   1120
  58.          Index           =   1
  59.          Left            =   120
  60.          TabIndex        =   19
  61.          Top             =   720
  62.          Width           =   1215
  63.       End
  64.       Begin SSOption optLinkMode 
  65.          Caption         =   "&None"
  66.          Font3D          =   0  'None
  67.          Height          =   255
  68.          Index           =   0
  69.          Left            =   120
  70.          TabIndex        =   18
  71.          Top             =   360
  72.          Width           =   1215
  73.       End
  74.    End
  75.    Begin TextBox txtData 
  76.       Height          =   1935
  77.       HelpContextID   =   1010
  78.       Left            =   240
  79.       MultiLine       =   -1  'True
  80.       ScrollBars      =   2  'Vertical
  81.       TabIndex        =   9
  82.       Top             =   2640
  83.       Width           =   5295
  84.    End
  85.    Begin CommandButton cmdAction 
  86.       Caption         =   "&Exit"
  87.       Height          =   375
  88.       Index           =   3
  89.       Left            =   4440
  90.       TabIndex        =   14
  91.       Top             =   1680
  92.       Width           =   1095
  93.    End
  94.    Begin TextBox Text1 
  95.       Height          =   375
  96.       HelpContextID   =   1010
  97.       Index           =   3
  98.       Left            =   1200
  99.       TabIndex        =   7
  100.       Top             =   1680
  101.       Width           =   855
  102.    End
  103.    Begin CommandButton cmdAction 
  104.       Caption         =   "E&xecute"
  105.       Height          =   375
  106.       HelpContextID   =   1230
  107.       Index           =   2
  108.       Left            =   4440
  109.       TabIndex        =   12
  110.       Top             =   1200
  111.       Width           =   1095
  112.    End
  113.    Begin TextBox Text1 
  114.       Height          =   375
  115.       HelpContextID   =   1010
  116.       Index           =   2
  117.       Left            =   1200
  118.       TabIndex        =   5
  119.       Top             =   1200
  120.       Width           =   1335
  121.    End
  122.    Begin CommandButton cmdAction 
  123.       Caption         =   "&Poke"
  124.       Height          =   375
  125.       HelpContextID   =   1220
  126.       Index           =   1
  127.       Left            =   4440
  128.       TabIndex        =   11
  129.       Top             =   720
  130.       Width           =   1095
  131.    End
  132.    Begin TextBox Text1 
  133.       Height          =   375
  134.       HelpContextID   =   1010
  135.       Index           =   1
  136.       Left            =   1200
  137.       TabIndex        =   3
  138.       Top             =   720
  139.       Width           =   1335
  140.    End
  141.    Begin CommandButton cmdAction 
  142.       Caption         =   "&Request"
  143.       Default         =   -1  'True
  144.       Height          =   375
  145.       HelpContextID   =   1210
  146.       Index           =   0
  147.       Left            =   4440
  148.       TabIndex        =   10
  149.       Top             =   240
  150.       Width           =   1095
  151.    End
  152.    Begin TextBox Text1 
  153.       Height          =   375
  154.       HelpContextID   =   1010
  155.       Index           =   0
  156.       Left            =   1200
  157.       TabIndex        =   1
  158.       Top             =   240
  159.       Width           =   1335
  160.    End
  161.    Begin Label lblStatus 
  162.       BackColor       =   &H00C0C0C0&
  163.       BorderStyle     =   1  'Fixed Single
  164.       Height          =   255
  165.       Left            =   1200
  166.       TabIndex        =   15
  167.       Top             =   4680
  168.       Width           =   4335
  169.    End
  170.    Begin Label lblStatusLabel 
  171.       BackColor       =   &H00C0C0C0&
  172.       Caption         =   "Status:"
  173.       Height          =   255
  174.       Left            =   240
  175.       TabIndex        =   16
  176.       Top             =   4680
  177.       Width           =   735
  178.    End
  179.    Begin Label Label2 
  180.       BackColor       =   &H00C0C0C0&
  181.       Caption         =   "Server | Topic | Item"
  182.       Height          =   255
  183.       Left            =   960
  184.       TabIndex        =   13
  185.       Top             =   2280
  186.       Width           =   4575
  187.    End
  188.    Begin Label Label3 
  189.       BackColor       =   &H00C0C0C0&
  190.       Caption         =   "&Data:"
  191.       Height          =   255
  192.       Left            =   240
  193.       TabIndex        =   8
  194.       Top             =   2280
  195.       Width           =   615
  196.    End
  197.    Begin Label Label1 
  198.       BackColor       =   &H00C0C0C0&
  199.       Caption         =   "Time&Out:"
  200.       Height          =   255
  201.       Index           =   3
  202.       Left            =   240
  203.       TabIndex        =   6
  204.       Top             =   1800
  205.       Width           =   735
  206.    End
  207.    Begin Label Label1 
  208.       BackColor       =   &H00C0C0C0&
  209.       Caption         =   "&Item:"
  210.       Height          =   255
  211.       Index           =   2
  212.       Left            =   240
  213.       TabIndex        =   4
  214.       Top             =   1320
  215.       Width           =   735
  216.    End
  217.    Begin Label Label1 
  218.       BackColor       =   &H00C0C0C0&
  219.       Caption         =   "&Topic:"
  220.       Height          =   255
  221.       Index           =   1
  222.       Left            =   240
  223.       TabIndex        =   2
  224.       Top             =   840
  225.       Width           =   735
  226.    End
  227.    Begin Label Label1 
  228.       BackColor       =   &H00C0C0C0&
  229.       Caption         =   "&Server:"
  230.       Height          =   255
  231.       Index           =   0
  232.       Left            =   240
  233.       TabIndex        =   0
  234.       Top             =   360
  235.       Width           =   735
  236.    End
  237.    Begin Menu mnuSourcePopup 
  238.       Caption         =   "Sour&ce"
  239.       HelpContextID   =   2000
  240.       Begin Menu mnuSource 
  241.          Caption         =   "&None"
  242.          Checked         =   -1  'True
  243.          HelpContextID   =   2000
  244.          Index           =   0
  245.       End
  246.       Begin Menu mnuSource 
  247.          Caption         =   "&Server"
  248.          HelpContextID   =   2000
  249.          Index           =   1
  250.       End
  251.    End
  252.    Begin Menu mnuFeaturePopup 
  253.       Caption         =   "Feat&ures"
  254.       Begin Menu mnuFeature 
  255.          Caption         =   "&Always on Top"
  256.          HelpContextID   =   3010
  257.          Index           =   0
  258.       End
  259.    End
  260. ' Define LinkExecute attributes. NUMEXECUTECMDS
  261. ' is the number of execution commands MINUS 1.
  262. ' The EC_ constants define the id value of each
  263. ' valid execution command. These must be sequential
  264. ' numbering from 0 to NUMEXECUTECMDS.
  265. ' To add new commands:
  266. ' 1) increment NUMEXECUTECMDS
  267. ' 2) add a new EC_ constant equal to the new
  268. '     NUMEXECUTECMDS value.
  269. ' 3) Add to the DoLinkExecute procedure a Case
  270. '     statement to the Select Case code for
  271. '     handling the new command.
  272. Const NUMEXECUTECMDS = 1   ' Number of commands minus 1.
  273. Const EC_DISPLAYABOUT = 0
  274. Const EC_SHELLAPP = 1
  275. Dim ExecuteCmd(NUMEXECUTECMDS) As String
  276. Const IDX_SERVER = 0
  277. Const IDX_TOPIC = 1
  278. Const IDX_ITEM = 2
  279. Const IDX_TIMEOUT = 3
  280. Const IDX_TEXTDATA = 4
  281. Const LAST_ITEM = 4
  282. Const IDX_REQUEST = 0
  283. Const IDX_POKE = 1
  284. Const IDX_EXECUTE = 2
  285. Const IDX_DONE = 3
  286. Const IDM_TOPMOST = 0
  287. Const NONE = 0
  288. Const AUTOMATIC = 1
  289. Const SERVER = 1
  290. Const MANUAL = 2
  291. Const NOTIFY = 3
  292. ' Set the default LinkTimeout period
  293. Const DEFTIMEOUT = "50"
  294. Dim bStatus(LAST_ITEM) As Integer
  295. Function Cmd_ShellApp (Params As String)
  296.  Dim rtn As Integer
  297.  Dim sRtn As String
  298.  Dim appname As String
  299.  Dim state As Integer
  300.    ' Shell the application defined by Params
  301.    ' First, extract the application name
  302.    If DoExtractParam(Params, appname) Then
  303.       ' Next extract the show state, if specified
  304.       state = 1
  305.       If DoExtractParam(Params, sRtn) Then
  306.          state = Val(sRtn)
  307.       End If
  308.       ' Now, shell the application
  309.       Cmd_ShellApp = Shell(appname, state)
  310.       Exit Function
  311.    Else
  312.       ' No app name found
  313.       Cmd_ShellApp = False
  314.       Exit Function
  315.    End If
  316. End Function
  317. Sub cmdAction_Click (Index As Integer)
  318.   Dim sTimeOut As String
  319.     On Error GoTo cmdAction_Err
  320.     ' Unload form if Index = IDX_DONE
  321.     If Index = IDX_DONE Then Unload USERDDE
  322.    ' Set the link timeout period for this operation.
  323.    ' If no timeout period is set, initialize it to
  324.    ' the global constant DEFTIMEOUT, specified in
  325.    ' tenths of a second.
  326.    sTimeOut = Text1(IDX_TIMEOUT).Text
  327.    If sTimeOut = "" Then
  328.       sTimeOut = DEFTIMEOUT:
  329.       Text1(IDX_TIMEOUT).Text = DEFTIMEOUT
  330.    End If
  331.    txtData.LinkTimeout = Val(sTimeOut)
  332.    Select Case Index
  333.     Case IDX_REQUEST
  334.         DdeRequest
  335.     Case IDX_POKE
  336.         DdePoke
  337.     Case IDX_EXECUTE
  338.         DdeExecute
  339.     End Select
  340.     Label2.Caption = Text1(IDX_SERVER).Text + " | " + Text1(IDX_TOPIC).Text + " | " + txtData.LinkItem
  341.     Text1(IDX_ITEM).SetFocus
  342. cmdAction_Terminate:
  343.     ' Reset Display Status just in case an error got bumped
  344.     ' up to this routine without cleaning up properly.
  345.     DisplayStatus ("")
  346.     Exit Sub
  347. cmdAction_Err:
  348.     Select Case Err
  349.     Case FILE_NOT_FOUND
  350.         ' If attempt to Shell start a server fails in the
  351.         ' DdeRequest, DdePoke, or DdeExecute function
  352.         ' because of an invalid file name, the
  353.         ' FILE_NOT_FOUND error will be received by this
  354.         ' calling routine. Handle accordingly.
  355.         MsgBox "Error: Invalid Server Name"
  356.         Text1(IDX_SERVER).SelStart = 0
  357.         Text1(IDX_SERVER).SelLength = Len(Text1(IDX_SERVER).Text)
  358.         Text1(IDX_SERVER).SetFocus
  359.         Resume cmdAction_Terminate
  360.     Case Else
  361.         MsgBox "Error: " + Str$(Err) + ":" + Error$
  362.         Resume cmdAction_Terminate
  363.     End Select
  364. End Sub
  365. Sub DdeExecute ()
  366.   Dim ii As Integer
  367.     ' This routine sends an execution command stream to
  368.     ' the server on behalf of the client application.
  369.     On Error GoTo Err_DdeExecute
  370.     Screen.MousePointer = 11    ' Set cursor to hourglass
  371.     DisplayStatus ("Executing Commands at Server")
  372.     txtData.LinkExecute txtData.Text
  373.     ii = DoEvents()
  374. DdeExecute_Terminate:
  375.     Screen.MousePointer = 1     ' Set cursor to arrow
  376.     DisplayStatus ("")
  377.     Exit Sub
  378. Err_DdeExecute:
  379.    MsgBox "Error: " + Str$(Err) + ":" + Error$
  380.    Resume DdeExecute_Terminate
  381. End Sub
  382. Sub DdePoke ()
  383.   Dim ii As Integer
  384.     On Error GoTo Err_DdePoke
  385.     txtData.LinkItem = Text1(IDX_ITEM).Text
  386. DdePoke_Restart:
  387.     DisplayStatus ("Poking Data back to Server")
  388.     txtData.LinkPoke
  389.     ii = DoEvents()
  390. DdePoke_Terminate:
  391.     DisplayStatus ("")
  392.     Exit Sub
  393. Err_DdePoke:
  394.     MsgBox "Error: " + Str$(Err) + ":" + Error$
  395.     Resume DdePoke_Terminate
  396. End Sub
  397. Sub DdeRequest ()
  398.   Dim ii As Integer
  399.     On Error GoTo Err_DdeRequest
  400.     ' Check if satisfying notification
  401.     If timNotify.Enabled Then
  402.         timNotify.Enabled = False
  403.         lblStatusLabel.ForeColor = &H0&
  404.         DisplayStatus ("")
  405.     End If
  406.     txtData.LinkItem = Text1(IDX_ITEM).Text
  407. DdeRequest_Restart:
  408.     DisplayStatus ("Making one-time request")
  409.     txtData.LinkRequest
  410.     ii = DoEvents()
  411. DdeRequest_Terminate:
  412.     DisplayStatus ("")
  413.     Exit Sub
  414. Err_DdeRequest:
  415.     MsgBox "Error: " + Str$(Err) + ":" + Error$
  416.     Error Err
  417.     Resume DdeRequest_Terminate
  418. End Sub
  419. Sub DisplayStatus (sParam As String)
  420.     lblStatus.Caption = sParam
  421.     lblStatus.Refresh
  422. End Sub
  423. Function DoExtractParam (Params As String, sRtn As String)
  424.  Dim pStart, pEnd As Integer
  425.  Dim rtn As Integer
  426.    DoExtractParam = True
  427.    ' Extract next parameter
  428.    If Len(Params) = 0 Then
  429.       DoExtractParam = False
  430.       Exit Function
  431.    End If
  432.    ' First, extract the next parameter and update the
  433.    ' Params string.
  434.    rtn = InStr(1, Params, ",") ' look next for commas
  435.    If rtn > 0 Then
  436.       ' More parameters follow. Extract the first into
  437.       ' 'sRtn' and update the Params string
  438.       sRtn = LTrim$(RTrim$(Left$(Params, rtn - 1)))
  439.       Params = Right$(Params, Len(Params) - rtn)
  440.    Else
  441.       ' No parameters follow.
  442.       sRtn = LTrim$(RTrim$(Params))
  443.       Params = ""
  444.    End If
  445.    ' Clean up sRtn. Eliminate any leading or trailing
  446.    ' parenthesis and blanks
  447.    If Left$(sRtn, 1) = Chr$(34) Then
  448.       sRtn = LTrim$(Right$(sRtn, Len(sRtn) - 1))
  449.    End If
  450.    If Right$(sRtn, 1) = Chr$(34) Then
  451.       sRtn = RTrim$(Left$(sRtn, Len(sRtn) - 1))
  452.    End If
  453. End Function
  454. Function DoLinkExecute (CmdStr As String)
  455.  Dim CommandStr As String
  456.  Dim CmdNumber As Integer
  457.  Dim Params As String
  458.  Dim rtn, ii As Integer
  459.    ' Provide for simple execution commands.
  460.    ' Return TRUE if successful, FALSE otherwise.
  461.    ' Make local copy of command string. This local
  462.    ' copy will be modified as commands and parameters
  463.    ' are parsed from it.
  464.    CommandStr = CmdStr
  465.    ' Parse the first command from the command stream
  466.    rtn = ParseCommand(CommandStr, CmdNumber, Params)
  467.    Do While rtn <> -1
  468.       ' Allow for multitasking after parsing each command.
  469.       ii = DoEvents()
  470.       ' This code provides processing for each of the
  471.       ' valid commands known to this application. As new
  472.       ' commands are added, a new Case statement must be
  473.       ' added for each.
  474.       Select Case CmdNumber
  475.       Case EC_DISPLAYABOUT
  476.          MsgBox "Display About..." + Params
  477.       Case EC_SHELLAPP
  478.          If Cmd_ShellApp(Params) = 0 Then GoTo ExecuteError
  479.       Case Else
  480.       End Select
  481.       If rtn = 0 Then
  482.          DoLinkExecute = False
  483.          Exit Function
  484.       End If
  485.       ' Extract the next command from the command stream.
  486.       rtn = ParseCommand(CommandStr, CmdNumber, Params)
  487.    Loop
  488. ExecuteError:
  489.    ' Error has occurred. Return TRUE. If no errors occur,
  490.    ' this code should never be reached.
  491.    DoLinkExecute = True
  492. End Function
  493. Sub Form_LinkClose ()
  494.    DisplayStatus "Link Closed"
  495. End Sub
  496. Sub Form_LinkError (LinkErr As Integer)
  497.  Dim sError, sTitle As String
  498.    ' When USERDDE is acting as a server, it may receive
  499.    ' run-time link errors. Process errors here.
  500.    sError = "A Link Error Has Occurred. " + Chr$(13) + Chr$(10)
  501.    sTitle = "DDE Link Error"
  502.    Select Case LinkErr
  503.    Case WRONG_FORMAT
  504.       sError = sError + "Wrong Data Format."
  505.       MsgBox sError, 0, sTitle
  506.    'Case REQUEST_WITHOUT_INIT
  507.    'Case DDE_WITHOUT_INIT
  508.    'Case ADVISE_WITHOUT_INIT
  509.    'Case POKE_WITHOUT_INIT
  510.    Case DDE_SERVER_CLOSED
  511.       sError = sError + "Requested DDE Server is closed."
  512.       MsgBox sError, 0, sTitle
  513.    Case TOO_MANY_LINKS
  514.       sError = sError + "Too many links(128 max.)."
  515.       MsgBox sError, 0, sTitle
  516.    Case STRING_TOO_LONG
  517.       sError = sError + "String too long."
  518.       MsgBox sError, 0, sTitle
  519.    'Case INVALID_CONTROL_ARRAY_REFERENCE
  520.    'Case UNEXPECTED_DDE
  521.    Case OUT_OF_MEMORY
  522.       sError = sError + "Insufficient memory to complete operation."
  523.       MsgBox sError, 0, sTitle
  524.    'Case SERVER_ATTEMPTED_CLIENT_OPERATION
  525.    Case Else
  526.       sError = sError + "Link Error Number: " + Str$(LinkErr)
  527.       MsgBox sError, 0, sTitle
  528.    End Select
  529. End Sub
  530. Sub Form_LinkExecute (CmdStr As String, Cancel As Integer)
  531.    DisplayStatus "Link Execute Attempted"
  532.    Cancel = DoLinkExecute(CmdStr)
  533. End Sub
  534. Sub Form_LinkOpen (Cancel As Integer)
  535.    DisplayStatus "Link Opened"
  536. End Sub
  537. Sub Form_Load ()
  538.    ' Initialize application
  539.    optLinkMode(NONE).Value = True
  540.    LoadExecuteCmds
  541.    ' Set initial Source capability to NONE
  542.    mnuSource_Click (NONE)
  543.    ValidateLinkModes
  544.    ValidateActions
  545. End Sub
  546. Sub Form_Unload (Cancel As Integer)
  547.     ' Terminate the application
  548.     End
  549. End Sub
  550. Sub LoadExecuteCmds ()
  551.    ' Load Execution commands into array. To add new
  552.    ' commands, be certain to update the NUMEXECUTECMDS
  553.    ' constant in the forms general declarations section.
  554.    ExecuteCmd(EC_DISPLAYABOUT) = "DisplayAbout"
  555.    ExecuteCmd(EC_SHELLAPP) = "ShellApp"
  556. End Sub
  557. Sub mnuFeature_Click (Index As Integer)
  558.    If mnuFeature(Index).Checked Then
  559.       SetWindowPos hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
  560.    Else
  561.       SetWindowPos hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
  562.    End If
  563.    ' Toggle menu checkmark
  564.    mnuFeature(Index).Checked = Not mnuFeature(Index).Checked
  565. End Sub
  566. Sub mnuSource_Click (Index As Integer)
  567.    Static nChecked As Integer
  568.    ' Set menu check mark
  569.    mnuSource(nChecked).Checked = False
  570.    mnuSource(Index).Checked = True
  571.    nChecked = Index
  572.    ' Set the LinkMode
  573.    LinkMode = Index
  574. End Sub
  575. Sub optLinkMode_Click (Index As Integer, Value As Integer)
  576.  Dim sMsg As String
  577.    On Error GoTo optLinkMode_Err
  578.    ' Set the LINKMODE. Index will be defined as follows:
  579.    ' Index  LinkMode
  580.    ' -----  --------
  581.    '   0    NONE
  582.    '   1    AUTOMATIC
  583.    '   2    MANUAL
  584.    '   3    NOTIFY
  585. optLinkMode_Restart:
  586.    ' Reset LinkMode and display status
  587.    lblStatus.Caption = ""
  588.    txtData.LinkMode = NONE
  589.    If Index = NONE Then
  590.       Label2.Caption = ""
  591.    Else
  592.       ' Setup initial link info
  593.       txtData.LinkTopic = Text1(IDX_SERVER).Text + "|" + Text1(IDX_TOPIC).Text
  594.       txtData.LinkItem = Text1(IDX_ITEM).Text
  595.    ' Set the link timeout period for this operation.
  596.    ' If no timeout period is set, initialize it to
  597.    ' the global constant DEFTIMEOUT, specified in
  598.    ' tenths of a second.
  599.    sTimeOut = Text1(IDX_TIMEOUT).Text
  600.    If sTimeOut = "" Then
  601.       sTimeOut = DEFTIMEOUT:
  602.       Text1(IDX_TIMEOUT).Text = DEFTIMEOUT
  603.    End If
  604.    txtData.LinkTimeout = Val(sTimeOut)
  605.    ' Set the LinkMode
  606.    txtData.LinkMode = Index
  607.       ' Set Server | Topic label field
  608.       Label2.Caption = Text1(IDX_SERVER).Text + " | " + Text1(IDX_TOPIC).Text + " | " + Text1(IDX_ITEM).Text
  609.    End If
  610.    ValidateActions
  611. optLinkMode_Terminate:
  612.    Exit Sub
  613. optLinkMode_Err:
  614.    If Err = DDE_NO_APP Then
  615.       ' Server is not loaded. Attempt to start.
  616.       If StartServer() = 0 Then
  617.          sMsg = "Unable to start server: "
  618.          sMsg = sMsg + Text1(IDX_SERVER).Text
  619.          MsgBox sMsg, 1, "DDE Error"
  620.          Resume optLinkMode_Terminate
  621.       Else
  622.          Resume optLinkMode_Restart
  623.       End If
  624.    Else
  625.       MsgBox "Error: " + Str$(Err) + ":" + Error$
  626.       optLinkMode(NONE).Value = True
  627.       lblStatus.Caption = Error$
  628.       Resume optLinkMode_Terminate
  629.    End If
  630. End Sub
  631. Function ParseCommand (CmdStr As String, CmdNumber As Integer, Params As String)
  632.  Dim CmdStart, CmdEnd, NextCmd As Integer
  633.  Dim pStart, pEnd As Integer
  634.  Dim Cmd As String
  635.  Dim ii As Integer
  636.    ' Parse LinkExecute command and return the command number
  637.    ' and the parameter string. Return 1 if a valid command
  638.    ' is found, -1 if an invalid command is found, else
  639.    ' return 0 if end of command string.
  640.    ' Find first left square bracket. If CmdStart = 1, no bracket
  641.    ' was found and we can assume no more commands exist so
  642.    ' we return a 0.
  643.    CmdStart = InStr(CmdStr, "[") + 1
  644.    If CmdStart = 1 Then ParseCommand = 0: Exit Function
  645.    ' If CmdEnd is -1, no following left parenthesis was found.
  646.    ' Hence, an error was found.
  647.    CmdEnd = InStr(CmdStart, CmdStr, "(") - 1
  648.    If CmdEnd = -1 Then ParseCommand = -1: Exit Function
  649.    Cmd = UCase$(LTrim$(RTrim$(Mid$(CmdStr, CmdStart, CmdEnd - CmdStart + 1))))
  650.    pStart = InStr(CmdStart, CmdStr, "(") + 1
  651.    pEnd = InStr(pStart, CmdStr, ")") - 1
  652.    NextCmd = InStr(pEnd, CmdStr, "[")
  653.    ' Find Cmd in ExecuteCmd array
  654.    For ii = 0 To NUMEXECUTECMDS
  655.       If UCase$(ExecuteCmd(ii)) = Cmd Then
  656.          ' Return the command number and parameters
  657.          Params = Mid$(CmdStr, pStart, pEnd - pStart + 1)
  658.          CmdNumber = ii
  659.          If NextCmd = 0 Then
  660.             ' No following command; return 0
  661.             ParseCommand = 0
  662.          Else
  663.             ' Additional commands follow. Remove this
  664.             ' command from CmdStr and return 1.
  665.             CmdStr = Right$(CmdStr, Len(CmdStr) - NextCmd + 1)
  666.             ' Set the return value
  667.             ParseCommand = 1
  668.          End If
  669.          Exit Function
  670.       End If
  671.    Next ii
  672.    ParseCommand = -1
  673. End Function
  674. Function StartServer ()
  675.   Dim ii As Integer
  676.     ' Attempt to start server minimized without focus
  677.     DisplayStatus ("Attempt to start server")
  678.     Screen.MousePointer = 11    ' set cursor to hourglass
  679.     StartServer = Shell(Text1(IDX_SERVER).Text + ".exe", 7)
  680.     Screen.MousePointer = 1     ' set cursor to arrow
  681.     ' Shelling takes time. Offer to share the processor.
  682.     ii = DoEvents()
  683.     DisplayStatus ("")
  684. End Function
  685. Sub Text1_Change (Index As Integer)
  686.     ' Set the associated text controls status to
  687.     ' TRUE if it contains text.
  688.     bStatus(Index) = Len(Text1(Index).Text) > 0
  689.     ValidateLinkModes
  690.     ValidateActions
  691. End Sub
  692. Sub Text1_GotFocus (Index As Integer)
  693.     ' Upon getting focus, select the entire text if
  694.     ' some exists.
  695.     Text1(Index).SelStart = 0
  696.     Text1(Index).SelLength = Len(Text1(Index).Text)
  697. End Sub
  698. Sub Text1_KeyPress (Index As Integer, KeyAscii As Integer)
  699.     ' Validate timeout entry
  700.     If Index = IDX_TIMEOUT Then
  701.         If KeyAscii < Asc("0") And KeyAscii > Asc("9") And KeyAscii <> Asc("-") Then
  702.             KeyPress = 0
  703.         End If
  704.     End If
  705. End Sub
  706. Sub Timer1_Timer ()
  707. End Sub
  708. Sub timNotify_Timer ()
  709.    Const RED = &HFF&
  710.    Const GREEN = &HFF00&
  711.    If lblStatusLabel.ForeColor = RED Then
  712.       lblStatusLabel.ForeColor = GREEN
  713.    Else
  714.       lblStatusLabel.ForeColor = RED
  715.    End If
  716. End Sub
  717. Sub txtData_Change ()
  718.     ' Set the main text control status to
  719.     ' TRUE if it contains text. This allows enabling
  720.     ' of the EXECUTE action.
  721.     bStatus(IDX_TEXTDATA) = Len(txtData.Text) > 0
  722.     ValidateActions
  723. End Sub
  724. Sub txtData_LinkClose ()
  725.    optLinkMode(NONE).Value = True
  726.    ValidateActions
  727.    DisplayStatus ("Dde Session Closed")
  728. End Sub
  729. Sub txtData_LinkError (LinkErr As Integer)
  730.     Screen.MousePointer = 1     ' Reset mouse pointer
  731.     MsgBox "Link Error " + Str$(LinkErr) + " has occurred"
  732. End Sub
  733. Sub txtData_LinkNotify ()
  734.    DisplayStatus "DDE Notification from Server. 'REQUEST' updated data."
  735.    timNotify.Interval = 500
  736.    timNotify.Enabled = True
  737. End Sub
  738. Sub ValidateActions ()
  739.   Dim bEnable As Integer
  740.     ' Validate the action buttons.
  741.     bEnable = txtData.LinkMode <> 0 And bStatus(IDX_ITEM)
  742.     cmdAction(IDX_REQUEST).Enabled = bEnable
  743.     cmdAction(IDX_POKE).Enabled = bEnable
  744.     cmdAction(IDX_EXECUTE).Enabled = txtData.LinkMode <> 0 And bStatus(IDX_TEXTDATA)
  745.     ' Set the default button.
  746.     cmdAction(IDX_EXECUTE).Default = cmdAction(IDX_EXECUTE).Enabled
  747.     cmdAction(IDX_REQUEST).Default = bEnable
  748. End Sub
  749. Sub ValidateLinkModes ()
  750.    Dim bEnable As Integer
  751.    bEnable = Len(Text1(IDX_SERVER)) > 0 And Len(Text1(IDX_TOPIC)) > 0
  752.    ' Enable MANUAL LinkMode if the server and topic are specified
  753.    optLinkMode(MANUAL).Enabled = bEnable
  754.    ' Enable AUTOMATIC and NOTIFY only if the server, topic, AND item
  755.    ' are specified
  756.    optLinkMode(AUTOMATIC).Enabled = bEnable And Len(Text1(IDX_ITEM)) > 0
  757.    optLinkMode(NOTIFY).Enabled = bEnable And Len(Text1(IDX_ITEM)) > 0
  758. End Sub
  759.