Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

VB.NET ITAPI/TAPI Incoming call and caller ID

0.00/5 (No votes)
14 Aug 2013 2  
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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here