Click here to Skip to main content
15,883,996 members
Articles / Programming Languages / Visual Basic
Article

Working with TAPI 3.x from .NET

Rate me:
Please Sign up or sign in to vote.
3.08/5 (47 votes)
6 Aug 2005CPOL 430.5K   11.7K   79   120
It's about time to Use Advanced Telephony features in your code? Right!

Introduction

Here I will post a class containing the event handling part in TAPI 3.0 and VB.NET.
I will also include the full project as a download.

The code is self explaining and well commented and if you need any further information, please drop me a PM.

All comments are welcome and please vote for the article.

Here is the full commented source code in VB.NET and I will be posting more in-depth tutorials about TAPI.

VB.NET
Imports TAPI3Lib 

Namespace VBCity.TAPI 

    Public Class VBTAPI 

        Private Const MediaAudio As Integer = 8 
        Private Const MediaModem As Integer = 16 
        Private Const MediaFax As Integer = 32 
        Private Const MediaVideo As Integer = 32768 

        Private WithEvents oTAPI As TAPI3Lib.TAPI ' will hold our TAPI object 
        ' will hold our selected address (you can hold many addresses in an array)
        Private oAddress As ITAddress 
        Private RegCookie As Integer 

        Sub New() 
            Try 
                ' creating a new instance to first initialize TAPI 
                ' before attaching the events 
                Dim m_TAPI As New TAPIClass 
                ' a variable to hold supported media types for the address 
                Dim MediaTypes As Integer 
                ' initializing TAPI 
                m_TAPI.Initialize() 
                ' attaching event sink 
                oTAPI = m_TAPI 
                ' getting rid of the private instance as we have another 
                ' global instance (oTAPI) 
                m_TAPI = Nothing 
                Dim AddressCollection As ITCollection = oTAPI.Addresses() 
                ' looping through address collection
                For Each Address As ITAddress In AddressCollection  
                    ' checking if address is working
                    If Address.State = ADDRESS_STATE.AS_INSERVICE Then  
                        ' extracting media support interface from the address 
                        Dim MediaSupport As ITMediaSupport = Address 
                        ' extracting media types supporting 
                        MediaTypes = MediaSupport.MediaTypes 
                        MediaSupport = Nothing ' dispose of the object 
                        If (MediaTypes And MediaModem) = MediaModem Then 
                            ' the address is a data Modem 
                            If (MediaTypes And MediaAudio) = MediaAudio Then 
                                ' Select the address since it supports Audio 
                                ' and is a FAX/Modem 
                                oAddress = Address ' select this address 
                                ' show the selected address name
                                MsgBox("we have selected this address: " + _
                                       oAddress.AddressName)  
                                Exit For 
                            End If 
                        End If 
                    End If 
                Next Address 
                If Not (oAddress Is Nothing) Then 
                    ' registering notifications for the selected address 
                    RegCookie = oTAPI.RegisterCallNotifications_
                                (oAddress, True, False, MediaTypes, 1) 
                    ' Note: this registration can be done on 
                    ' as many addresses as you want 
                    ' we will not receive notifications unless we specify 
                    ' which type of events we are interested in 
                    oTAPI.EventFilter = (TAPI_EVENT.TE_CALLNOTIFICATION Or _
                            TAPI_EVENT.TE_CALLSTATE Or TAPI_EVENT.TE_CALLINFOCHANGE) 
                Else 
                    MsgBox("no address selected") 
                End If 
            Catch ex As Exception 
                MsgBox("Error occurred:" & vbCrLf & ex.Message, _
                        MsgBoxStyle.Critical, "VBCITY.VBTAPI") 
            End Try 
            ' by now we are done for the initialization and registration 
            ' and the events should fire 
            ' Note: you must dispose of TAPI before you destroy the class 
            ' and I will leave this for now 
        End Sub 
        
        Private Sub oTAPI_Event(ByVal TapiEvent As TAPI3Lib.TAPI_EVENT, _
                        ByVal pEvent As Object) Handles oTAPI.Event 
            ' making a thread asynchronously process the event 
            Dim thAsyncCall As System.Threading.Thread 
            Select Case TapiEvent 
            
                Case TAPI_EVENT.TE_CALLNOTIFICATION 'Call Notification Arrived 
                    ' assigning our sub's delegate to the thread 
                    thAsyncCall = New Threading.Thread(AddressOf CallNotificationEvent) 
                    ' passing the variable for the thread 
                    CallNotificationObject = CType(pEvent, ITCallNotificationEvent) 
                    ' starting the thread 
                    thAsyncCall.Start() 
                    
                Case TAPI_EVENT.TE_CALLSTATE 'Call State Changes 
                    ' assigning our sub's delegate to the thread 
                    thAsyncCall = New Threading.Thread(AddressOf CallStateEvent) 
                    ' passing the variable for the thread 
                    CallStateObject = CType(pEvent, ITCallStateEvent) 
                    ' starting the thread 
                    thAsyncCall.Start() 
                    
                Case TAPI_EVENT.TE_CALLINFOCHANGE 'Call Info Changes 
                    ' assigning our sub's delegate to the thread 
                    thAsyncCall = New Threading.Thread(AddressOf CallInfoEvent) 
                    ' passing the variable for the thread 
                    CallInfoObject = CType(pEvent, ITCallInfoChangeEvent) 
                    ' starting the thread 
                    thAsyncCall.Start() 
                    
            End Select 
        End Sub 
        
        Private CallNotificationObject As ITCallNotificationEvent 
        Private Sub CallNotificationEvent() 
            ' here we should check to see various notifications of new and ended calls 
            Select Case CallNotificationObject.Event 
            
                Case CALL_NOTIFICATION_EVENT.CNE_MONITOR 
                    ' the notification is for a monitored call 
                Case CALL_NOTIFICATION_EVENT.CNE_OWNER 
                    ' the notification is for an owned call 
            
            End Select 
        End Sub 

        Private CallStateObject As ITCallStateEvent 
        Private Sub CallStateEvent() 
            ' here we should check to see call state and handle connects and disconnects 
            Select Case CallStateObject.State 
            
                Case CALL_STATE.CS_IDLE 
                Case CALL_STATE.CS_INPROGRESS 
                Case CALL_STATE.CS_OFFERING 
                    ' a call is offering so if you don't want it then pass it 
                    ' The code to pass the call is the following 
                    ' Dim CallControl As ITBasicCallControl = CallStateObject.Call 
                    ' CallControl.HandoffIndirect (CallStateObject.Call.CallInfoLong_
                                            (CALLINFO_LONG.CIL_MEDIATYPESAVAILABLE) 
                Case CALL_STATE.CS_CONNECTED 
                    ' call is connected 
                Case CALL_STATE.CS_QUEUED 
                    ' call is being queued 
                Case CALL_STATE.CS_HOLD 
                    ' call is on hold 
                Case CALL_STATE.CS_DISCONNECTED 
                    ' call is disconnected 
            
            End Select 
        End Sub 

        Private CallInfoObject As ITCallInfoChangeEvent 
        Private Sub CallInfoEvent() 
            ' here you can extract information from the call 
            ' the code to extract the caller ID 
            ' >>> put the following code in a try block and 
            ' swallow the exception if it gives errors 
            Dim CallerID As String 
            CallerID = CallInfoObject.Call.CallInfoString_
                    (CALLINFO_STRING.CIS_CALLERIDNAME) 
        End Sub 

    End Class 
End Namespace

History

  • 7th August, 2005: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Dot Labs Pty Ltd
Australia Australia
A trusted technology adviser and a seasoned consultant with 15 years of experience in IT industry focused on helping organisations build modern IT solutions that drive innovation and increase efficiency and end-user value.

With a strong business acumen, an entrepreneur character, and vast experience in software engineering, I started my own consulting business (Dot Labs Pty Ltd) with a mission to drive adoption of modern solution architecture, public cloud infrastructure, and industry best practices.

During my career, I gained an extensive hands-on experience of software engineering through architecture standards, patterns, and best practice through my work with various enterprise organisations and startups, while keeping track of emerging technology and industry trends.

Comments and Discussions

 
GeneralEvent and Disconnection Problem Pin
RoSchVB.NET1-Apr-08 6:18
RoSchVB.NET1-Apr-08 6:18 
GeneralError Pin
Loops697228-Mar-08 5:22
Loops697228-Mar-08 5:22 
Questionvs 2005 Pin
ameen55521-Mar-08 0:37
ameen55521-Mar-08 0:37 
Generalplay a wave file after answer the incoming call Pin
dida14-Mar-08 7:01
dida14-Mar-08 7:01 
GeneralRe: play a wave file after answer the incoming call Pin
makwana.ashish10-Aug-08 7:59
makwana.ashish10-Aug-08 7:59 
GeneralStrange Event Problem Pin
Abulfar27-Feb-08 6:02
Abulfar27-Feb-08 6:02 
GeneralRe: Strange Event Problem Pin
Abulfar27-Feb-08 7:29
Abulfar27-Feb-08 7:29 
GeneralRe: Strange Event Problem Pin
Bishoy Demian27-Feb-08 9:57
Bishoy Demian27-Feb-08 9:57 

Abulfar wrote:

tapi_obj.EventFilter = (TAPI_EVENT.TE_DIGITEVENT Or TAPI_EVENT.TE_TONEEVENT Or TAPI_EVENT.TE_CALLSTATE)


that line should come before you register for call notification in order to receive your events.

"Imagination is more important than knowledge.."
{Albert Einstein}

GeneralRe: Strange Event Problem Pin
Abulfar28-Feb-08 3:33
Abulfar28-Feb-08 3:33 
GeneralRe: Strange Event Problem Pin
Bishoy Demian28-Feb-08 5:20
Bishoy Demian28-Feb-08 5:20 
GeneralRe: Strange Event Problem Pin
Abulfar28-Feb-08 13:33
Abulfar28-Feb-08 13:33 
QuestionEquipment? Pin
Amakakeru26-Feb-08 18:43
Amakakeru26-Feb-08 18:43 
AnswerRe: Equipment? Pin
Bishoy Demian27-Feb-08 9:59
Bishoy Demian27-Feb-08 9:59 
GeneralRe: Equipment? Pin
Amakakeru28-Feb-08 18:07
Amakakeru28-Feb-08 18:07 
Questionneed sme help...its urgent Pin
dida17-Jan-08 4:10
dida17-Jan-08 4:10 
GeneralRe: need sme help...its urgent Pin
Bishoy Demian19-Jan-08 8:28
Bishoy Demian19-Jan-08 8:28 
QuestionTAPI PLAYBACK Pin
atogwe3-Nov-07 19:56
atogwe3-Nov-07 19:56 
AnswerRe: TAPI PLAYBACK Pin
mr_Engineer16-Nov-07 20:04
mr_Engineer16-Nov-07 20:04 
GeneralRe: TAPI PLAYBACK Pin
aliyasserkhan27-Jan-09 4:51
aliyasserkhan27-Jan-09 4:51 
Questiondetecting remote hang up with voice modems Pin
clayga1-Nov-07 14:40
clayga1-Nov-07 14:40 
QuestionTAPI WITH SOUND Pin
atogwe28-Oct-07 4:26
atogwe28-Oct-07 4:26 
QuestionHow can i setup hardwares for phone call? Pin
Roathvb11-Oct-07 6:47
Roathvb11-Oct-07 6:47 
GeneralNo call appearance available. Pin
Andisheh_K9-Jul-07 11:27
Andisheh_K9-Jul-07 11:27 
GeneralNo call appearance available. Pin
Andisheh_K9-Jul-07 12:16
Andisheh_K9-Jul-07 12:16 
AnswerRe: No call appearance available. Pin
Bishoy Demian11-Jul-07 23:46
Bishoy Demian11-Jul-07 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.