Click here to Skip to main content
11,634,453 members (75,376 online)
Click here to Skip to main content

Working with TAPI 3.x from .NET

, 6 Aug 2005 CPOL 267K 8.7K 76
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Bishoy Demian
Software Developer (Senior) Readify
Australia Australia
Dreamer, Technology Evangelist, have a passion for software architecture and design patterns an hope everyone does so.

I've been working in this field for many years and still expect to learn more and do more in my career and would love to see others do too.

I live in Cairo, Egypt and work for a leading International Company developing strategic decisions and executing them with many teams to achieve company goals in Software market, and use my favorite programming language C# and .NET framework sometimes to research and create hobby projects.

Wish you enjoyed knowing about me Wink | ;)

You may also be interested in...

Comments and Discussions

 
Questiontapi Pin
Member 112819786-May-15 19:51
memberMember 112819786-May-15 19:51 
QuestionConsole closes Pin
Marcel Walser29-Nov-13 22:20
memberMarcel Walser29-Nov-13 22:20 
QuestionHOWTO ... answer an incomming Call Pin
beckiemkf7-Aug-13 23:55
memberbeckiemkf7-Aug-13 23:55 
QuestionNo event raized when incoming call Pin
Jul67ien30-Jun-13 22:28
memberJul67ien30-Jun-13 22:28 
AnswerRe: No event raized when incoming call Pin
Bishoy Demian1-Jul-13 12:57
memberBishoy Demian1-Jul-13 12:57 
GeneralRe: No event raized when incoming call Pin
Jul67ien2-Jul-13 22:17
memberJul67ien2-Jul-13 22:17 
GeneralRe: No event raized when incoming call Pin
beckiemkf11-Jul-13 11:47
memberbeckiemkf11-Jul-13 11:47 
GeneralMy vote of 4 Pin
Nujramako2-Mar-12 23:59
memberNujramako2-Mar-12 23:59 
GeneralMy vote of 1 Pin
Technorodent9-Feb-11 10:41
memberTechnorodent9-Feb-11 10:41 
GeneralMy vote of 1 Pin
BenJamming2-Jan-11 15:53
memberBenJamming2-Jan-11 15:53 
GeneralHelp me profeor Pin
AshkanJami20-Oct-10 21:50
memberAshkanJami20-Oct-10 21:50 
Generalcall center system develop help needed Pin
ChesterAV12-Oct-10 9:28
memberChesterAV12-Oct-10 9:28 
GeneralTAPI "BlindTransfer" Problem Pin
zendehdel21-Jun-10 23:03
memberzendehdel21-Jun-10 23:03 
GeneralVisual Basic 2008 / .NET 3.5 / TAPI3 - INCOMMING CALL [EXAMPLE CODE] Pin
TomGermany201031-Mar-10 0:14
memberTomGermany201031-Mar-10 0:14 
GeneralRe: Visual Basic 2008 / .NET 3.5 / TAPI3 - INCOMMING CALL [EXAMPLE CODE] Pin
wiswalld9-Dec-10 8:02
memberwiswalld9-Dec-10 8:02 
GeneralRe: Visual Basic 2008 / .NET 3.5 / TAPI3 - INCOMMING CALL [EXAMPLE CODE] Pin
beckiemkf8-Jul-13 22:55
memberbeckiemkf8-Jul-13 22:55 
GeneralMy vote of 1 Pin
TomGermany201018-Mar-10 0:08
memberTomGermany201018-Mar-10 0:08 
GeneralRe: My vote of 1 Pin
Bishoy Ghaly18-Mar-10 6:12
memberBishoy Ghaly18-Mar-10 6:12 
GeneralVisual Basic 2008 - frustration Pin
TomGermany201017-Mar-10 0:01
memberTomGermany201017-Mar-10 0:01 
GeneralRe: Visual Basic 2008 - frustration Pin
Bishoy Ghaly18-Mar-10 6:09
memberBishoy Ghaly18-Mar-10 6:09 
GeneralRe: Visual Basic 2008 - frustration Pin
TomGermany201029-Mar-10 23:31
memberTomGermany201029-Mar-10 23:31 
GeneralVB .NET Pin
OppVen3-Jun-11 4:30
memberOppVen3-Jun-11 4:30 
GeneralQuestion using windows XP built in dialer.exe to initiate calls Pin
Megrim0326-Nov-09 5:33
memberMegrim0326-Nov-09 5:33 
GeneralMy vote of 1 Pin
albert_cook11-Aug-09 21:54
memberalbert_cook11-Aug-09 21:54 
GeneralNeed help Pin
kshirsagar15-Jun-09 19:27
memberkshirsagar15-Jun-09 19:27 
Generalplease explain this code, surely it is an error Pin
edt12311-May-09 9:35
memberedt12311-May-09 9:35 
AnswerRe: please explain this code, surely it is an error Pin
The Code Guru15-May-09 5:53
memberThe Code Guru15-May-09 5:53 
General[Message Deleted] Pin
it.ragester28-Mar-09 5:37
memberit.ragester28-Mar-09 5:37 
Questionhow do i use this Pin
theboytony20-Feb-09 8:50
membertheboytony20-Feb-09 8:50 
GeneralNeed a Code to Receive Calls Using External Fax Model Pin
shirin.105-Dec-08 1:43
membershirin.105-Dec-08 1:43 
Generaltapi events stop reciving events... Pin
altarribage29-Oct-08 6:23
memberaltarribage29-Oct-08 6:23 
GeneralRe: tapi events stop reciving events... Pin
The Code Guru1-Nov-08 19:58
memberThe Code Guru1-Nov-08 19:58 
GeneralTTERMINAL namespace Pin
joshi aniruddha25-Sep-08 19:08
memberjoshi aniruddha25-Sep-08 19:08 
GeneralNo incoming calls are notifed Pin
vbnetskywalker22-Sep-08 9:20
membervbnetskywalker22-Sep-08 9:20 
QuestionTAPI and fax Pin
stefkhel7-Sep-08 5:36
memberstefkhel7-Sep-08 5:36 
GeneralTelephony Application Programming Interface (TAPI) functionality is not supported from managed code Pin
kiener9-Jul-08 22:06
memberkiener9-Jul-08 22:06 
GeneralVirtual Caller ID Pin
ismailwissam25-Jun-08 3:27
memberismailwissam25-Jun-08 3:27 
Newssupport for the article Pin
The Code Guru14-Jun-08 12:01
memberThe Code Guru14-Jun-08 12:01 
QuestionIs there a identifer for each call in TAPI? Pin
Carsten Giesen8-Jun-08 21:16
memberCarsten Giesen8-Jun-08 21:16 
GeneralStart project in VB.Net 2005 [modified] Pin
Carsten Giesen5-Jun-08 6:05
memberCarsten Giesen5-Jun-08 6:05 
GeneralEvent and Disconnection Problem Pin
RoSchVB.NET1-Apr-08 6:18
memberRoSchVB.NET1-Apr-08 6:18 
GeneralError Pin
Loops697228-Mar-08 5:22
memberLoops697228-Mar-08 5:22 
Questionvs 2005 Pin
ameen55521-Mar-08 0:37
memberameen55521-Mar-08 0:37 
Generalplay a wave file after answer the incoming call Pin
dida14-Mar-08 7:01
memberdida14-Mar-08 7:01 
GeneralRe: play a wave file after answer the incoming call Pin
makwana.ashish10-Aug-08 7:59
membermakwana.ashish10-Aug-08 7:59 
GeneralStrange Event Problem Pin
Abulfar27-Feb-08 6:02
memberAbulfar27-Feb-08 6:02 
GeneralRe: Strange Event Problem Pin
Abulfar27-Feb-08 7:29
memberAbulfar27-Feb-08 7:29 
GeneralRe: Strange Event Problem Pin
The Code Guru27-Feb-08 9:57
memberThe Code Guru27-Feb-08 9:57 
GeneralRe: Strange Event Problem Pin
Abulfar28-Feb-08 3:33
memberAbulfar28-Feb-08 3:33 
GeneralRe: Strange Event Problem Pin
The Code Guru28-Feb-08 5:20
memberThe Code Guru28-Feb-08 5:20 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 7 Aug 2005
Article Copyright 2005 by Bishoy Demian
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid