Link to home
Start Free TrialLog in
Avatar of JeePeeTee
JeePeeTee

asked on

C# and TAPI(3.1)

Hello experts,

Im trying to get C# to work with TAPI to generate a db search query and popups on certain events.
Ive installed the ActiveTSP to communicate with the Asterisks VoIP server, thats the TSP i want to use.

Lucky for me, i cant even get C# to enumerate TSP's.. Ive tried the tutorial on codeprojects (http://www.codeproject.com/KB/IP/devangpro.aspx) and some similair script on the same site.
---------------------------------------------------------------------------------------------
Error:
Cannot convert method group 'EnumerateAddresses' to non-delegate type 'TAPI3Lib.IEnumAddress[]'. Did you intend to invoke the method?

Script:
TAPIClass controller = new TAPIClass();
controller.Initialize();
IEnumAddress[] lines = controller.EnumerateAddresses;
foreach (IEnumAddress line in lines)
{
    outputBox.Text += line.Name;
}

---------------------------------------------------------------------------------------------

I also used the C# Wrapper located at http://www.julmar.com/blog/mark/2008/07/23/ATAPIAssemblyUpdated.aspx but it generated an error.
---------------------------------------------------------------------------------------------
error:
System.ArgumentOutOfRangeException was unhandled - Message="Index and count must refer to a location within the buffer.\r\nParameter name: bytes"

Script:
er controller = new TapiManager("CallerMAX");
try
{
    if (controller.Initialize() && controller.Lines.Length > 0)
    {
        outputBox.Text = ("I found the following TSP's:...\r\n");
        TapiLine[] lines = controller.Lines;
        for (int i = 0; i < 8; i++)
        {
            outputBox.Text += i + ". " + lines[i].Name + "\r\n";
        }
    }
    else
    {
        outputBox.Text = ("Nothing found!");
    }
}
catch (TapiException e)
{
    Console.WriteLine(e.Message);
}
controller.Shutdown();
---------------------------------------------------------------------------------------------

My question:
- Does someone have a good, easy to follow, tutorial on how to achieve what i want ?
- Does someone know where my mistake is in the above script ?
- If none of the above counts then can someone tell me the best way to achieve my goal ?

To all the experts: Thanks in advance! P.S. My experience in C# is low.
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

According to the error, EnumerateAddresses is a function, not a property.
 
Try using EnumerateAddresses() instead?
Avatar of JeePeeTee
JeePeeTee

ASKER

I've applied your suggestion, showed below, but i get the following message:

Cannot implicitly convert type 'TAPI3Lib.IEnumAddress' to 'TAPI3Lib.IEnumAddress[]'
TAPIClass controller = new TAPIClass();
controller.Initialize();
IEnumAddress[] lines = controller.EnumerateAddresses();
foreach (IEnumAddress line in lines)
{
    // Code
}

Open in new window

controller.EnumerateAddresses does not return an array but an enumerator -object to obtain the adresses.
 

TAPIClass controller = new TAPIClass();
controller.Initialize();
IEnumAddress addressEnum = controller.EnumerateAddresses();
ITAddress address;
uint arg;

addressEnum.Next(1, out address, ref arg);
while (address!= null) {

  // Do your code here

  addressEnum.Next(1, out address, ref arg);
}

Open in new window

I've applied the code above, it works like a charm!
----------------------------------------------------------------------
public Form1()
        {
            InitializeComponent();
            TAPIClass controller = new TAPIClass();
            controller.Initialize();
            IEnumAddress addressEnum = controller.EnumerateAddresses();
            ITAddress address;
            uint arg=0;

            addressEnum.Next(1, out address, ref arg);
            int i = 0;
            while (address != null)
            {
                //string test = address.AddressName.Substring(0, 3);
                if (address.AddressName.Substring(0, 3) == "AST")
                {
                    outputBox.Text += i + ". " + address.AddressName.Substring(0, 3) + "\r\n";
                    // This is the right TSP
                }
                addressEnum.Next(1, out address, ref arg);
                i++;
            }
        }
----------------------------------------------------------------------

As you can see this is fired at the loading of the application, i now need to be able to trigger events, you perhappen to know how this works?
Depends on which event you want to trigger. If you want to trigger an existing even, just call the function you created for it or one of the existing.
For instance, to trigger the click of a button, you can set the button-value to true. In a lot of objects, you can call the On<eventname>-routine.
If you want to create a new event for subscribers outside your class, you create a multi-cast delegate and call that one if it is not equal to null.
Let me know what your goal for the event is.
 
Thanks for your input!

My goals are:
-Select the right TSP for handling AsterisksTSP events.
-Fire a search query based upon the telephone number when recieving a new incoming telephone call (search db for company and/or person name).
-When this searchquery is done display a screen popup with the caller information (even if there is none, then it will just display the phone number).
CodeProject had a cood implementation of the TapiClass, the file is not there any more, but it still is in the cache of Google:
http://74.125.77.132/search?q=cache:uQ0zYkbEwU8J:69.10.233.10/KB/IP/devangpro.aspx%3Fmsg%3D1454427+Tapi3Lib+TapiClass&cd=19&hl=nl&ct=clnk&gl=nl
That should help you on your way I think.
That article was moved over here http://www.codeproject.com/KB/IP/devangpro.aspx, its the same example i used before starting this topic. I really cannot understand that code. Ill give it a try, again.
ASKER CERTIFIED SOLUTION
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've added the class and added that snippet and im recieving the following error on compiling:

'CallerMAX.Form1' does not contain a definition for 'status' and no extension method 'status' accepting a first argument of type 'CallerMAX.Form1' could be found (are you missing a using directive or an assembly reference?)
status is just a string that you define in your form-class, so just add 'private string status' to your form in order to make this work.
I will try you comment. Im sorry to respond this late, i was 'out of the office'. I will post any results and/or succes!
No problem.
I've added private string status; but now i get a different error. I know for sure that im messing it up so i've posted the entire code with this comment.

Could you tell me what im doing wrong here?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using JulMar.Atapi;
using TAPI3Lib; 

namespace CallerMAX
{
    public partial class Form1 : Form
    {
        private string status;
        public Form1()
        {
            InitializeComponent();
            // Code starts here

            TAPIClass controller = new TAPIClass();
            controller.Initialize();
            IEnumAddress addressEnum = controller.EnumerateAddresses();
            ITAddress address;
            uint arg=0;

            addressEnum.Next(1, out address, ref arg);
            while (address != null)
            {
                if (address.AddressName.Substring(0, 3) == "AST")
                {
                    // This is the TSP I want to call events on
                    callnotification cn = new callnotification();
                    cn.addtolist = new callnotification.listshow(this.status);
                    controller.ITTAPIEventNotification_Event_Event += new
                        TAPI3Lib.ITTAPIEventNotification_EventEventHandler(cn.Event);
                    controller.EventFilter = (int)(TAPI_EVENT.TE_CALLNOTIFICATION |
                        TAPI_EVENT.TE_DIGITEVENT |
                        TAPI_EVENT.TE_PHONEEVENT |
                        TAPI_EVENT.TE_CALLSTATE |
                        TAPI_EVENT.TE_GENERATEEVENT |
                        TAPI_EVENT.TE_GATHERDIGITS |
                        TAPI_EVENT.TE_REQUEST);
                    break;
                }
                else
                {
                    addressEnum.Next(1, out address, ref arg);
                }
            }
             
        }
        class callnotification : TAPI3Lib.ITTAPIEventNotification
        {
            public delegate void listshow(string str);
            public listshow addtolist;

            public void Event(TAPI3Lib.TAPI_EVENT te, object eobj)
            {
                switch (te)
                {
                    case TAPI3Lib.TAPI_EVENT.TE_CALLNOTIFICATION:
                        addtolist("call notification event has occured");
                        break;
                    case TAPI3Lib.TAPI_EVENT.TE_DIGITEVENT:
                        TAPI3Lib.ITDigitDetectionEvent dd =
                           (TAPI3Lib.ITDigitDetectionEvent)eobj;
                        addtolist("Dialed digit" + dd.ToString());
                        break;
                    case TAPI3Lib.TAPI_EVENT.TE_GENERATEEVENT:
                        TAPI3Lib.ITDigitGenerationEvent dg =
                             (TAPI3Lib.ITDigitGenerationEvent)eobj;
                        MessageBox.Show("digit dialed!");
                        addtolist("Dialed digit" + dg.ToString());
                        break;
                    case TAPI3Lib.TAPI_EVENT.TE_PHONEEVENT:
                        addtolist("A phone event!");
                        break;
                    case TAPI3Lib.TAPI_EVENT.TE_GATHERDIGITS:
                        addtolist("Gather digit event!");
                        break;
                    case TAPI3Lib.TAPI_EVENT.TE_CALLSTATE:
                        TAPI3Lib.ITCallStateEvent a =
                             (TAPI3Lib.ITCallStateEvent)eobj;
                        TAPI3Lib.ITCallInfo b = a.Call;
                        switch (b.CallState)
                        {
                            case TAPI3Lib.CALL_STATE.CS_INPROGRESS:
                                addtolist("dialing");
                                break;
                            case TAPI3Lib.CALL_STATE.CS_CONNECTED:
                                addtolist("Connected");
                                break;
                            case TAPI3Lib.CALL_STATE.CS_DISCONNECTED:
                                addtolist("Disconnected");
                                break;
                            case TAPI3Lib.CALL_STATE.CS_OFFERING:
                                addtolist("A party wants to communicate with you!");
                                break;
                            case TAPI3Lib.CALL_STATE.CS_IDLE:
                                addtolist("Call is created!");
                                break;
                        }
                        break;
                }
            }
        }



        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
            {
                Hide();
            }
        }

        private void trayIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && IsHandleCreated)
            {
                if (FormWindowState.Normal == WindowState)
                {
                    WindowState = FormWindowState.Minimized;
                    Hide();
                }
                else
                {
                    Show();
                    WindowState = FormWindowState.Normal;
                    BringToFront();
                }
            }
        }

        private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
            {
                Show();
                WindowState = FormWindowState.Normal;
                BringToFront();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Icon.Dispose();
        }

        private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

Open in new window

I fixed it using the Julmar TAPI Wrapper. Im closing this thread and will give djjaries the exp. points. Thanks for all your help!
Great job!