5,663,486 members and growing! (15,112 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

Working with TAPI 3.x from .NET

By The Code Guru

It's about time to Use Advanced Telephony features in your code? Right!
C#, VBWindows, .NET, .NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, VistaVS.NET2003, Visual Studio, Dev

Posted: 6 Aug 2005
Updated: 6 Aug 2005
Views: 93,344
Bookmarked: 37 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
27 votes for this Article.
Popularity: 3.99 Rating: 2.79 out of 5
8 votes, 29.6%
1
2 votes, 7.4%
2
2 votes, 7.4%
3
4 votes, 14.8%
4
11 votes, 40.7%
5

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.

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)

About the Author

The Code Guru


Over 7 years experience in IT field, majorly in Software Development. My valuable experience has been gained in challenging work environments where I have developed expertise like:

* Using Agile and CMMI methodologies manage software development lifecycle (SDLC).
* Project planning, Scheduling and management skills.
* Excellent leadership, problem solving and troubleshooting skills.
* Enterprise solutions architecture and design (Standalone / Client-Server / n-Tier).
* Application development, deployment and maintenance (Desktop / Web / Mobile / Embedded).
* Database Design, Normalization and Administration skills.
* Code refactoring and reverse engineering skills.
* Requirement gathering and analysis skills.
* Configuration Control planning and execution
Occupation: Team Leader
Location: Egypt Egypt

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 83 (Total in Forum: 83) (Refresh)FirstPrevNext
Generaltapi events stop reciving events...memberaltarribage7:23 29 Oct '08  
GeneralRe: tapi events stop reciving events...memberThe Code Guru20:58 1 Nov '08  
GeneralTTERMINAL namespacememberjoshi aniruddha20:08 25 Sep '08  
GeneralNo incoming calls are notifedmembervbnetskywalker10:20 22 Sep '08  
QuestionTAPI and faxmemberstefkhel6:36 7 Sep '08  
GeneralHow get sound on call pc to phone using h.323memberMd572357:17 19 Aug '08  
GeneralTelephony Application Programming Interface (TAPI) functionality is not supported from managed codememberkiener23:06 9 Jul '08  
GeneralVirtual Caller IDmemberismailwissam4:27 25 Jun '08  
Newssupport for the articlememberThe Code Guru13:01 14 Jun '08  
GeneralIs there a identifer for each call in TAPI?memberCarsten Giesen22:16 8 Jun '08  
GeneralStart project in VB.Net 2005 [modified]memberCarsten Giesen7:05 5 Jun '08  
GeneralEvent and Disconnection ProblemmemberRoSchVB.NET7:18 1 Apr '08  
GeneralErrormemberLoops69726:22 28 Mar '08  
Questionvs 2005memberameen5551:37 21 Mar '08  
Generalplay a wave file after answer the incoming callmemberdida8:01 14 Mar '08  
GeneralRe: play a wave file after answer the incoming callmembermakwana.ashish8:59 10 Aug '08  
GeneralStrange Event ProblemmemberAbulfar7:02 27 Feb '08  
GeneralRe: Strange Event ProblemmemberAbulfar8:29 27 Feb '08  
GeneralRe: Strange Event ProblemmemberThe Code Guru10:57 27 Feb '08  
GeneralRe: Strange Event ProblemmemberAbulfar4:33 28 Feb '08  
GeneralRe: Strange Event ProblemmemberThe Code Guru6:20 28 Feb '08  
GeneralRe: Strange Event ProblemmemberAbulfar14:33 28 Feb '08  
GeneralEquipment?memberAmakakeru19:43 26 Feb '08  
GeneralRe: Equipment?memberThe Code Guru10:59 27 Feb '08