etCallWaiting Sample Program Source Code

Private Declare Function lineSwapHold Lib "TAPI32.DLL" (ByVal hCall1 As Long, _
                                                   ByVal hCall2 As Long) As Long

' This sample program supports up to 2 calls.
' Any more calls will be ignored.
' TeleTools can handle as many calls as the telephony device.
' This program could easily be modified to handle more calls.
' The following variables hold the Call Handles for up to two calls.
Private CallHandles(2) As Long

' The following constants are used to display the current call state
' and to consolidate a number of possible call states into a few
Private Const CALLSTATE_NOCALL = "No call"
Private Const CALLSTATE_NEWCALL = "New call"
Private Const CALLSTATE_CONNECTED = "Connected"
Private Const CALLSTATE_ONHOLD = "On hold"
Private Const CALLSTATE_DISCONNECTED = "Disconnected"

Private Function IsDataModem() As Boolean
Dim ModemMediaModes As Integer

    ' These are the minimal media modes expected for standard data modems
    ModemMediaModes = LINEMEDIAMODE_INTERACTIVEVOICE + LINEMEDIAMODE_DATAMODEM

    ' Check to see if the selected device is a modem and it is a voice modem
    If (InStr(1, etLine1.TAPITSP, "Modem", 1) > 0) And _
        ((etLine1.DeviceMediaModesAvailable And ModemMediaModes) >= _
            ModemMediaModes) Then
        IsDataModem = True
    Else
        IsDataModem = False
    End If
End Function

Private Function IsVoiceModem() As Boolean
Dim ModemMediaModes As Integer
Dim VoiceModemMediaModes As Integer

    ' These are the minimal media modes expected for standard data modems
    ModemMediaModes = LINEMEDIAMODE_INTERACTIVEVOICE + LINEMEDIAMODE_DATAMODEM
    ' These are the minimal media modes expected for voice modems
    VoiceModemMediaModes = ModemMediaModes + LINEMEDIAMODE_UNKNOWN + _
                            LINEMEDIAMODE_AUTOMATEDVOICE

    ' Check to see if the selected device is a modem and it is a voice modem
    If (InStr(1, etLine1.TAPITSP, "Modem", 1) > 0) And _
        ((etLine1.DeviceMediaModesAvailable And VoiceModemMediaModes) >= _
            VoiceModemMediaModes) Then
        IsVoiceModem = True
    Else
        IsVoiceModem = False
    End If
End Function

Private Sub Hangup()
    ' Make sure that it is safe to hangup
    If (etLine1.CallCount > 0) And (etLine1.CallHandle <> 0) And _
        (etLine1.CallState <> LINECALLSTATE_IDLE) Then
        ' Hangup the call
        etLine1.CallHangup
    End If
End Sub

Private Function GetCallIndex(ByVal CallHandle As Long) As Integer
    ' Find the CallHandle# variable by using the actual call handle
    If CallHandle = CallHandles(0) Then
        GetCallIndex = 0
    Else
        If CallHandle = CallHandles(1) Then
            GetCallIndex = 1
        Else
            ' Ignore the call if there are more than 2
            GetCallIndex = -1
        End If
    End If
End Function

Private Sub UpdateCallDisplay(ByVal Index As Integer, ByVal CallHandle As Long)
    If Index > 1 Then
        ' Ignore the call if there are more than 2
        Exit Sub
    End If

    If CallHandle = 0 Then
        ' No call exists for this index
        ' Clear the call information
        CommandCall(Index).Caption = "Call"
        CommandCall(Index).Enabled = False
        TextCallerIDName(Index).Text = ""
        TextCallerIDNumber(Index).Text = ""
        TextState(Index).Text = CALLSTATE_NOCALL
    Else
        ' Make sure that the etLine control is using the correct Call Handle
        etLine1.CallHandle = CallHandle

        Select Case etLine1.CallState
            Case LINECALLSTATE_IDLE
                ' The call has gone to the idle state
                ' This routine will now call itself to clear the call information
                Call UpdateCallDisplay(Index, 0)
            Case LINECALLSTATE_OFFERING
                ' This is a new call
                TextState(Index).Text = CALLSTATE_NEWCALL
                CommandCall(Index).Caption = "Answer"
                CommandCall(Index).Enabled = True
            Case LINECALLSTATE_CONNECTED
                ' This call is now the connected call
                TextState(Index).Text = CALLSTATE_CONNECTED
                CommandCall(Index).Caption = "Hangup"
                CommandCall(Index).Enabled = True
            Case LINECALLSTATE_ONHOLD, _
                LINECALLSTATE_ONHOLDPENDTRANSF, _
                LINECALLSTATE_ONHOLDPENDCONF
                ' This call is now oh hold.
                ' Need to check all On Hold states because some telephony
                ' devices report on hold calls differently
                TextState(Index).Text = CALLSTATE_ONHOLD
                CommandCall(Index).Caption = "Swap Hold"
                CommandCall(Index).Enabled = True
            Case LINECALLSTATE_DISCONNECTED
                ' The call has been disconnected and needs to be hung up
                TextState(Index).Text = CALLSTATE_DISCONNECTED
                CommandCall(Index).Enabled = False
                Hangup
        End Select
    End If
End Sub


Private Sub ComboDevice_Click()
    TextCallCount.Text = "0"

    Call UpdateCallDisplay(0, 0)
    Call UpdateCallDisplay(1, 0)

    ' Set the TAPI Line device
    etLine1.DeviceName = ComboDevice.Text

    ' Check to see if the selected device is a modem
    If IsDataModem Then
        ' Check to see if the selected device is a Voice modem
        If IsVoiceModem Then
            MsgBox ("This device is a Voice Modem!" & vbCrLf & _
                    "Use the etVoiceModemCallWaiting sample program.")
        Else
            MsgBox ("This device is a Data Modem!" & vbCrLf & _
                    "Data modems do not support Call Waiting or CallerID.")
        End If
    Else
        ' Check to see if this telephony device supports placing calls on hold
        If etLine1.AddressCapabilitiesMaxNumOnHoldCalls = 0 Then
            MsgBox("This telephony device is not configured support Call Waiting")
        Else
            ' Active the TAPI Line device
            etLine1.DeviceActive = True

            If Not etLine1.DeviceActive Then
                MsgBox ("Error opening line device: " & etLine1.ErrorText)
            End If
        End If
    End If
End Sub

Private Sub CommandCall_Click(Index As Integer)
Dim lResult As Long

    If Index > 1 Then
        ' Ignore the call if there are more than 2
        Exit Sub
    End If

    If TextState(Index).Text = CALLSTATE_NEWCALL Then
        ' Answer the call
        If Not etLine1.CallAnswer Then
            MsgBox ("Error Answering: " & etLine1.ErrorText)
        End If
    Else
        If TextState(Index).Text = CALLSTATE_CONNECTED Then
            ' Hangup the call
            If Not etLine1.CallHangup Then
                MsgBox ("Error Hanging up: " & etLine1.ErrorText)
            End If
        Else
            If TextState(Index) = CALLSTATE_ONHOLD Then
                ' SwapHold the calls
                Select Case Index
                    Case 0
                        lResult = lineSwapHold(CallHandles(1), CallHandles(0))
                    Case 1
                        lResult = lineSwapHold(CallHandles(0), CallHandles(1))
                End Select
            End If
            If lResult < 0 Then
                MsgBox ("Error Swapping Hold: " & etLine1.StringLINEERR(lResult))
            End If
        End If
    End If
End Sub

Private Sub etLine1_OnCallBegin(ByVal CallHandle As Long)
' A new call!
Dim Index As Integer

    TextCallCount.Text = Str(etLine1.CallCount)

    ' Search for a free CallHandle index variable
    If CallHandles(0) = 0 Then
        Index = 0
    Else
        If CallHandles(1) = 0 Then
            Index = 1
        Else
            ' Ignore the call if there are more than 2
            Exit Sub
        End If
    End If

    ' Set the CallHandle index variable
    CallHandles(Index) = CallHandle

    ' Clear the call information
    Call UpdateCallDisplay(Index, 0)

    ' Update the call controls
    Call UpdateCallDisplay(Index, CallHandle)

    ' Just in case the application was started and the call already
    ' existed, update the CallerID information.
    etLine1_OnCallerID (CallHandle)
End Sub

Private Sub etLine1_OnCallerID(ByVal CallHandle As Long)
    ' Make sure that etLine is using the correct call
    etLine1.CallHandle = CallHandle

    ' Check the flags to see if the CallerID Name is available
    If (etLine1.CallCallerIDFlags And LINECALLPARTYID_NAME) <> 0 Then
        TextCallerIDName(GetCallIndex(CallHandle)).Text = etLine1.CallCallerIDName
    End If

    ' Check the flags to see if the CallerID Number is available
    If (etLine1.CallCallerIDFlags And LINECALLPARTYID_ADDRESS) <> 0 Then
        TextCallerIDNumber(GetCallIndex(CallHandle)).Text = _
                                                        etLine1.CallCallerIDNumber
    End If

    If etLine1.CallCallerIDBlocked Then
        TextCallerIDName(GetCallIndex(CallHandle)).Text = "Blocked"
    Else
        If etLine1.CallCallerIDOutOfArea Then
            TextCallerIDName(GetCallIndex(CallHandle)).Text = "Out of area"
        End If
    End If
End Sub

Private Sub etLine1_OnConnected(ByVal CallHandle As Long)
    ' Update the call controls
    Call UpdateCallDisplay(GetCallIndex(CallHandle), CallHandle)
End Sub

Private Sub etLine1_OnDisconnected(ByVal CallHandle As Long)
    ' Update the call controls
    Call UpdateCallDisplay(GetCallIndex(CallHandle), CallHandle)
    Hangup
End Sub

Private Sub etLine1_OnHold(ByVal CallHandle As Long)
    ' Update the call controls
    Call UpdateCallDisplay(GetCallIndex(CallHandle), CallHandle)
End Sub

Private Sub etLine1_OnHoldPendingConference(ByVal CallHandle As Long)
    ' Update the call controls
    Call UpdateCallDisplay(GetCallIndex(CallHandle), CallHandle)
End Sub

Private Sub etLine1_OnHoldPendingTransfer(ByVal CallHandle As Long)
    ' Update the call controls
    Call UpdateCallDisplay(GetCallIndex(CallHandle), CallHandle)
End Sub

Private Sub etLine1_OnIdle(ByVal CallHandle As Long)
' The call is now idle
Dim Index As Integer

    ' Set the call count display,
    ' Need to subtract one because TeleTools has not updated this value yet
    ' because you still have access to the call information
    TextCallCount.Text = Str(etLine1.CallCount - 1)

    ' Search for the CallHandle index variable
    If CallHandles(0) = CallHandle Then
        Index = 0
    Else
        If CallHandles(1) = CallHandle Then
            Index = 1
        Else
            ' Ignore the call if there are more than 2
            Exit Sub
        End If
    End If

    ' Clear the CallHandle index variable
    CallHandles(Index) = 0
    ' Clear the call information
    Call UpdateCallDisplay(Index, 0)
End Sub

Private Sub etLine1_OnOffering(ByVal CallHandle As Long)
    ' Update the call controls
    Call UpdateCallDisplay(GetCallIndex(CallHandle), CallHandle)
End Sub

Private Sub Form_Load()
    'Enabled all ExceleTel TeleTools controls before using them
    etLine1.Enabled = True

    ' Fill in the list box with all available telephony devices
    ComboDevice.Clear
    etLine1.DeviceID = 0
    For L = 0 To etLine1.DeviceCount - 1
        ComboDevice.AddItem etLine1.DeviceList(L)
    Next L

    ' Select the first device in the list
    If ComboDevice.ListCount > 0 Then
        ComboDevice.Text = ComboDevice.List(0)
    End If
    ComboDevice.ListIndex = 0
End Sub


Private Sub Form_Unload(Cancel As Integer)
    'Close the etLine control when closing the form
    etLine1.DeviceActive = False
End Sub