T3XDOC.DOB

VERSION 5.00 
Begin VB.UserDocument Tapi3XDoc
ClientHeight = 3600
ClientLeft = 0
ClientTop = 0
ClientWidth = 5085
HScrollSmallChange= 225
ScaleHeight = 3600
ScaleWidth = 5085
VScrollSmallChange= 225
Begin VB.CommandButton Using
Caption = "What To Use for Connect"
Height = 375
Left = 1320
TabIndex = 4
Top = 480
Width = 2295
End
Begin VB.CommandButton Disconnect
Caption = "Disconnect"
Height = 375
Left = 1320
TabIndex = 3
Top = 2880
Width = 2295
End
Begin VB.CommandButton Connect
Caption = "Connect Sync"
Default = -1 'True
Height = 375
Left = 1320
TabIndex = 2
Top = 2160
Width = 2295
End
Begin VB.TextBox txtDestAddress
Height = 285
Left = 1320
TabIndex = 0
Top = 1320
Width = 2295
End
Begin VB.Label lblDestAddress
Caption = "Destination Address"
Height = 375
Left = 240
TabIndex = 1
Top = 1320
Width = 855
End
End
Attribute VB_Name = "Tapi3XDoc"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'
'T3XDoc.DLL
'
'This is a sample that shows how to write an ActiveX Document
'that uses Tapi3.0 for making a synchronous outgoing call.
'
'The ActiveX document must be loaded in Internet Explorer.
'It allows the user to select the address on which to make the call,
'and indicate one terminal and one media type to be used on that call.
'After the call is connected, the user can disconnect the call.
'
'In order to obtain the ActiveX Document, the Application Setup Wizard
'must be used. It will create the dll, the cab file as
'well as the htm file that points to the ActiveXDocument.
'Internet Explorer must be pointed to the HTM file, and this
'will automatically load the ActiveX Document.
'

Option Explicit

Private mbFirstShow As Boolean
Private mobjCallControl As ITBasicCallControl

Private Sub UserDocument_Initialize()
On Error GoTo ErrHandle

Debug.Print ("UserDocument_Initialize")

'set initial value in global variables
Set mobjCallControl = Nothing
mbFirstShow = True

'init user interface
Load frmUsing

'init tapi objects
Call frmUsing.InitializeTapiObjects

cleanup:
Exit Sub

ErrHandle:
Debug.Print ("Error: " & Err.Number & " " & Err.Description)
Call PrintError("UserDocument_Initialize", Err)

GoTo cleanup

End Sub

Private Sub UserDocument_Terminate()
On Error Resume Next

Debug.Print ("UserDocument_Terminate")

'release tapi objects
Set mobjCallControl = Nothing
frmUsing.ReleaseTapiObjects 'this releases also objTapi itself

'release user interface
Unload frmUsing

cleanup:
Exit Sub

End Sub

Private Sub Using_Click()
If Not (mobjCallControl Is Nothing) Then
'a previous call exists;
Dim strMsg As String
strMsg = "Current call is not yet disconnected!"
Call MessageBox(0, strMsg, "T3XDoc", MB_OK Or MB_USERICON)
Else
frmUsing.Show vbModal
End If
End Sub

'
'Connect_Click: check if connect is allowed, then make call
'
Private Sub Connect_Click()
On Error GoTo ErrHandle

Dim strMsg As String
Dim objAddress As Address
Dim MediaTerminals
Dim bSync As Boolean
Dim objCallInfo As ITCallInfo
Dim bConnectReturned As Boolean, bCallStateDisplayed As Boolean


bConnectReturned = False
bCallStateDisplayed = False

'
'Connect is allowed only if there is no previous call or
'the previous call is in disconnected state
'
If Not (mobjCallControl Is Nothing) Then
'
'a previous call exists; query for its state
'
If CallIsConnected(mobjCallControl) Then
strMsg = "Cannot make new call while previous one is still connected."
Call MessageBox(0, strMsg, "T3XDoc", MB_OK Or MB_USERICON)

Exit Sub

Else
'
'Release previous call, which is not needed anymore
'
Set mobjCallControl = Nothing
End If
End If

'
' Go on to create and connect a call, using the tapi objects
' selected by the user in the secondary form "frmUsing":
' user must have selected the origination address + the
' mediaterminal
'

Set objAddress = frmUsing.Address
If objAddress Is Nothing Then
strMsg = "No address is selected, go in What To Use for Connect, "
strMsg = strMsg & "select something, then press OK."
Call MessageBox(0, strMsg, "T3XDoc", MB_OK Or MB_USERICON)
GoTo cleanup
End If
Set mobjCallControl = objAddress.CreateCall(txtDestAddress.Text)

MediaTerminals = frmUsing.MediaTerminals
mobjCallControl.SelectMediaTerminals (MediaTerminals)

'
'Calling Connect with bSync = True, means that the
'call is synchronous; the function Connect will not
'return until it has a result, i.e. until the call reaches
'either the connected or the disconnected state.
'This might take a while, depending on the Tapi service provider,
'and meanwhile the calling app will appear as "hanging"
'

bSync = True
mobjCallControl.Connect (bSync)
bConnectReturned = True

'query the ITCallInfo interface, which can tell us the state
Set objCallInfo = mobjCallControl
DisplayCallState (objCallInfo.CallState)
bCallStateDisplayed = True

cleanup:
On Error Resume Next 'otherwise there would be a loop
Set objCallInfo = Nothing

If Not IsEmpty(MediaTerminals) Then
Dim nIterator As Long
For nIterator = LBound(MediaTerminals) To UBound(MediaTerminals)
Set MediaTerminals(nIterator) = Nothing
Next
End If

Set objAddress = Nothing
Exit Sub

ErrHandle:

Call PrintError("Connect_Click", Err)

'display a message
If bConnectReturned = False Then
strMsg = "Connect didn't succeed, there was an error."
Call MessageBox(0, strMsg, "T3XDoc", MB_OK Or MB_USERICON)
ElseIf bCallStateDisplayed = False Then
strMsg = "Can't find out the state of the call, there was an error."
Call MessageBox(0, strMsg, "T3XDoc", MB_OK Or MB_USERICON)
End If

'finish
GoTo cleanup
End Sub

Private Sub Disconnect_Click()
On Error GoTo ErrHandle

Dim strMsg As String

If mobjCallControl Is Nothing Then
strMsg = "There is no call to be disconnected."
Call MessageBox(0, strMsg, "T3XDoc", MB_OK Or MB_USERICON)
Exit Sub
End If

'disconnect the call by calling Disconnect, method
'of ITBasicCallControl
If CallIsConnected(mobjCallControl) Then
mobjCallControl.Disconnect (DC_NORMAL)
End If

Set mobjCallControl = Nothing

cleanup:
Exit Sub

ErrHandle:
Call PrintError("Disconnect_Click", Err)
GoTo cleanup
End Sub

Private Function CallIsConnected(objCall As ITBasicCallControl) As Boolean
On Error GoTo ErrHandle

Dim objCallInfo As ITCallInfo
Dim State As CALL_STATE

CallIsConnected = False

'to find out the state, we must query the ITCallInfo interface
Set objCallInfo = objCall
State = objCallInfo.CallState

If State = CS_CONNECTED Then
CallIsConnected = True
End If

cleanup:
On Error Resume Next 'otherwise there would be a loop
Set objCallInfo = Nothing
Exit Function

ErrHandle:
Call PrintError("CallIsConnected", Err)
GoTo cleanup
End Function

'I don't want the message boxes to beep, so instead of using
'MsgBox, I call the Win32 function MessageBox with MB_USERICON
'See Tools.bas for declarations
Private Sub DisplayCallState(State As CALL_STATE)
Select Case State
Case CS_CONNECTED
Call MessageBox(0, "CS_CONNECTED", "Call State", _
MB_OK Or MB_USERICON)
Case CS_DISCONNECTED
Call MessageBox(0, "CS_DISCONNECTED", "Call State", _
MB_OK Or MB_USERICON)
Case CS_HOLD
Call MessageBox(0, "CS_HOLD", "Call State", _
MB_OK Or MB_USERICON)
Case CS_IDLE
Call MessageBox(0, "CS_IDLE", "Call State", _
MB_OK Or MB_USERICON)
Case CS_INPROGRESS
Call MessageBox(0, "CS_INPROGRESS", "Call State", _
MB_OK Or MB_USERICON)
Case CS_OFFERING
Call MessageBox(0, "CS_OFFERING", "Call State", _
MB_OK Or MB_USERICON)
Case CS_QUEUED
Call MessageBox(0, "CS_QUEUED", "Call State", _
MB_OK Or MB_USERICON)
Case Else
Call MessageBox(0, "Call state unknown!!", "Call State", _
MB_OK Or MB_USERICON)
End Select
End Sub