Click here to Skip to main content
15,890,690 members
Articles / Programming Languages / Visual Basic
Tip/Trick

VB.NET ITAPI/TAPI Incoming call and caller ID

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
14 Aug 2013CPOL 72.3K   8.8K   12   17
ITAPI, TAPI Incoming call and caller ID/Caller Number show

Introduction

This is a complete example how to use ITAPI library in VB.NET, showing the code for incoming call through an audio modem to answer and show the caller ID.

Background

Take a look at the website and download the package from http://itapi3.codeplex.com/downloads/get/104950. Inside you will find a chm file with a guide.

Using the code

Before you use this class you have to add a project reference. In this case I've used Visual Studio 2008. Project -> Add Reference -> Browse and choose ITapi3.dll. Choose the correct one (x32 or x64) and then instantiate the class. The code will work listening to the hardware events. If you have x64 platform use the attached x64 file. x32 file on x64 systems will not work. Once you have instantiated the class the code will run alone and will listen to the events. Before you run it, ensure your TAPI service on your machine is running. The code has been tested on Windows7 and works fine.

VB
Imports JulMar.Tapi3

Public Class VBITAPI

    Private hwAddress As TAddress = Nothing
    Private mediaTypes As Integer

    Private WithEvents tapiCls As TTapi
    Private Const mediaAudio = JulMar.Tapi3.TAPIMEDIATYPES.AUDIO
    Private Const mediaData = JulMar.Tapi3.TAPIMEDIATYPES.DATAMODEM
    Private Const mediaVideo = JulMar.Tapi3.TAPIMEDIATYPES.VIDEO
    Private Const mediaFax = JulMar.Tapi3.TAPIMEDIATYPES.G3FAX
    Private Const mediaMultitrack = JulMar.Tapi3.TAPIMEDIATYPES.MULTITRACK

    Private offeringCall As Boolean = False
    Private connectedCall As Boolean = False
    Private inProgressCall As Boolean = False

    Private incomingCall As TCall

    Public Sub New()

        tapiCls = New TTapi
        tapiCls.Initialize()

        'Discover all hardware addresses
        'Make "And" function to discover different media devices

        For Each address In tapiCls.Addresses
            If address.State = ADDRESS_STATE.AS_INSERVICE Then
                mediaTypes = address.MediaTypes

                'We discover and select the audio device usually to be a modem
                If (mediaTypes And mediaAudio) = mediaAudio Then

                    hwAddress = address
                    hwAddress.Open(mediaAudio)
                    'This will show the name of the audio devices discovered
                    'MsgBox(hwAddress.AddressName.ToString)

                End If

            End If
        Next

        'If an hardware has not been found
        If hwAddress Is Nothing Then

            'Hardware address not found

        End If

    End Sub

    Public Sub openLine()
        'Put modem wake up to listem to the line
        Try
            If hwAddress.State = ADDRESS_STATE.AS_INSERVICE Then
                hwAddress.Open(mediaAudio)
            End If

        Catch ex As Exception
            'Manage the exception
        End Try

    End Sub

    Public Sub closeLine()
        'Put modem shut down
        Try
            If hwAddress.State = ADDRESS_STATE.AS_INSERVICE Then
                hwAddress.Close()
            End If

        Catch ex As Exception

            'Manage the exception
        End Try

    End Sub

    Public Sub answerCall()
        If offeringCall = True Then
            incomingCall.Answer()
        End If
    End Sub

    Public Sub hungup()
        If connectedCall = True Then
            incomingCall.Disconnect(DISCONNECT_CODE.DC_REJECTED)
        End If
    End Sub

    Private Sub tapiCallNotification_Event(ByVal sender As Object, _
      ByVal e As TapiCallNotificationEventArgs) Handles tapiCls.TE_CALLNOTIFICATION
        'MsgBox("Call notification")

        Select Case e.Event
            Case CALL_NOTIFICATION_EVENT.CNE_MONITOR
                'Choose you action
            Case CALL_NOTIFICATION_EVENT.CNE_OWNER
                'Choose you action
        End Select
    End Sub

    Private Sub tapiInfoChange_Event(ByVal sender As Object, _
      ByVal e As TapiCallInfoChangeEventArgs) Handles tapiCls.TE_CALLINFOCHANGE
        Dim callerNumber As String = ""

        Try

            callerNumber = e.Call.CallInfo(CALLINFO_STRING.CIS_CALLERIDNUMBER).ToString

            If callerNumber.Length = 0 Then
                'Hidden number
                
            Else
                'Clear number
                
            End If

        Catch ex As Exception

            'Manage the exception

        End Try


    End Sub

    Private Sub tapiGeneral_Event(ByVal sender As Object, _
      ByVal e As TapiCallStateEventArgs) Handles tapiCls.TE_CALLSTATE

        ' MsgBox(e.State.ToString)

        Select Case e.State

            Case CALL_STATE.CS_UNKNOWN

            Case CALL_STATE.CS_OFFERING
                'When a call is coming in during the ring tone
             offeringCall = True

            Case CALL_STATE.CS_CONNECTED
                'When a call is answered
             connectedCall = True
             offeringCall = False
                
            Case CALL_STATE.CS_HOLD
            Case CALL_STATE.CS_IDLE
            Case CALL_STATE.CS_INPROGRESS
            Case CALL_STATE.CS_QUEUED
            Case CALL_STATE.CS_DISCONNECTED
                'When a call is in conversation or closed
                connectedCall = False                
        End Select

    End Sub

    Private Sub tapiGenerate_Event(ByVal sender As Object, _
       ByVal e As TapiDigitGenerationEventArgs) Handles tapiCls.TE_GENERATEEVENT
        ' MsgBox("GENERATE")
    End Sub

    Private Sub tapiSpecific_Event(ByVal sender As Object, _
      ByVal e As TapiAddressDeviceSpecificEventArgs) Handles tapiCls.TE_ADDRESSDEVSPECIFIC
        ' MsgBox("SPECIFIC EVENTS")
    End Sub

    Private Sub tapiObject_Event(ByVal sender As Object, _
      ByVal e As TapiObjectEventArgs) Handles tapiCls.TE_TAPIOBJECT
        'MsgBox("tapi object")
    End Sub

    Private Sub tapiPhone_Event(ByVal sender As Object, _
      ByVal e As TapiPhoneEventArgs) Handles tapiCls.TE_PHONEEVENT
        'MsgBox("Phone events")
    End Sub
    Private Sub tapiDigit_Event(ByVal sender As Object, _
      ByVal e As TapiDigitDetectionEventArgs) Handles tapiCls.TE_DIGITEVENT
        'MsgBox("Digit detection events")
    End Sub

End Class

License

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


Written By
Software Developer (Senior)
Spain Spain
Software developer and system administrator based on linux and windows systems with a strong programming knowledge in web development. Always interested in new and hard challenges regarding the IT field.
Constantly searching innovative solutions for personal use in every environment.
Enjoy my code and contact me for more information.

Comments and Discussions

 
QuestionX64 Errors, and how to Resolve Pin
Member 1384130027-Jul-20 10:21
Member 1384130027-Jul-20 10:21 
Questiondoes not work on X64 Pin
CrazyTmack0312-Nov-19 16:50
CrazyTmack0312-Nov-19 16:50 
QuestionSystem.TypeInitializationException Pin
Member 140089656-Nov-18 3:58
Member 140089656-Nov-18 3:58 
QuestionDownload package link not working Pin
Member 1169005213-Nov-17 22:13
Member 1169005213-Nov-17 22:13 
QuestionWhere is the CHM file? Pin
Member 116900527-Nov-17 4:56
Member 116900527-Nov-17 4:56 
QuestionWhat do I need for the code to work? Pin
Member 1076104517-Oct-16 19:48
Member 1076104517-Oct-16 19:48 
Questionplz help Pin
khallood15-Mar-16 5:47
khallood15-Mar-16 5:47 
QuestionTE_CALLINFOCHANGE Pin
itziki27-Jun-15 10:30
itziki27-Jun-15 10:30 
AnswerRe: TE_CALLINFOCHANGE Pin
iupax8028-Jun-15 13:14
professionaliupax8028-Jun-15 13:14 
AnswerRe: TE_CALLINFOCHANGE Pin
3raser13-Oct-16 1:39
3raser13-Oct-16 1:39 
GeneralRe: TE_CALLINFOCHANGE Pin
Midnight4894-May-17 14:09
Midnight4894-May-17 14:09 
QuestionWarning Processor AMD Pin
Victor Carbon5-Jan-15 9:23
Victor Carbon5-Jan-15 9:23 
AnswerRe: Warning Processor AMD Pin
iupax8028-Jun-15 13:14
professionaliupax8028-Jun-15 13:14 
Questionyour coding Pin
Member 989248611-Jul-14 11:01
Member 989248611-Jul-14 11:01 
QuestionRequirements to run the code Pin
zonender9-Jan-14 0:33
zonender9-Jan-14 0:33 
QuestionHaroun Pin
Member 1023071025-Aug-13 2:09
Member 1023071025-Aug-13 2:09 
AnswerRe: Haroun Pin
iupax8027-Aug-13 23:24
professionaliupax8027-Aug-13 23:24 

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.