Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using JulMar.Atapi;
  11.  
  12. namespace TAPI20
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.  
  17.         private string lineName = string.Empty;
  18.         private string name = "";
  19.         TapiManager myTAPI;
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.             initTAPI();
  25.            
  26.         }
  27.  
  28.  
  29.  
  30.  
  31.         void initTAPI()
  32.         {
  33.  
  34.             myTAPI = new TapiManager("GetCaller");
  35.  
  36.             if (!myTAPI.Initialize())
  37.             {
  38.                 MessageBox.Show("FAILED!");
  39.             }else
  40.             {
  41.                 name = myTAPI.Lines[0].Name;
  42.                 lineName = (myTAPI != null && myTAPI.Lines.Length > 0 ? name : string.Empty);
  43.  
  44.                 foreach(TapiLine line in myTAPI.Lines)
  45.                 {
  46.                     line.NewCall += this.OnNewCall;
  47.                     line.Ringing += this.OnRinging;
  48.                     line.CallStateChanged += this.OnCallState;
  49.                     line.CallInfoChanged += this.OnCallInfo;
  50.                 }
  51.  
  52.                 MessageBox.Show(lineName);
  53.  
  54.             }
  55.         }
  56.  
  57.         private void OnNewCall(object sender, NewCallEventArgs e)
  58.         {
  59.             if(InvokeRequired == true)
  60.             {
  61.                 this.BeginInvoke(new EventHandler<NewCallEventArgs>(this.OnNewCall), new object[] { sender, e });
  62.                 return;
  63.             }
  64.  
  65.             label1.Text = "Dialing...";
  66.         }
  67.  
  68.         private void OnRinging(object sender, RingEventArgs e)
  69.         {
  70.             if (InvokeRequired == true)
  71.             {
  72.                 this.BeginInvoke(new EventHandler<RingEventArgs>(this.OnRinging), new object[] { sender, e });
  73.                 return;
  74.             }
  75.  
  76.             label1.Text = "Ringing...";
  77.  
  78.  
  79.         }
  80.  
  81.         private void OnCallState(object sender, CallStateEventArgs e)
  82.         {
  83.             if (InvokeRequired == true)
  84.             {
  85.                 this.BeginInvoke(new EventHandler<CallStateEventArgs>(this.OnCallState), new object[] { sender, e });
  86.                 return;
  87.             }
  88.  
  89.             label1.Text = "Outgoing Call...";
  90.  
  91.  
  92.         }
  93.  
  94.         private void OnCallInfo(object sender, CallInfoChangeEventArgs e)
  95.         {
  96.             if (InvokeRequired == true)
  97.             {
  98.                 this.BeginInvoke(new EventHandler<CallInfoChangeEventArgs>(this.OnCallInfo), new object[] { sender, e });
  99.                 return;
  100.             }
  101.  
  102.             label1.Text = "Incoming Call...";
  103.  
  104.  
  105.         }
  106.  
  107.         private void MakePhoneCall(string phoneNumber)
  108.         {
  109.             TapiLine line = myTAPI.GetLineByName(this.lineName, true);
  110.  
  111.             if (line != null)
  112.             {
  113.                 if (!line.IsOpen)
  114.                     line.Open(MediaModes.All);
  115.  
  116.                 line.NewCall += new EventHandler<NewCallEventArgs>(line_NewCall);
  117.                 line.CallInfoChanged += new EventHandler<CallInfoChangeEventArgs>(line_CallInfoChanged);
  118.                 line.CallStateChanged += new EventHandler<CallStateEventArgs>(line_CallStateChanged);
  119.  
  120.                 if (phoneNumber.Length > 0)
  121.                 {
  122.                     MakeCallParams makeCallParams = new MakeCallParams();
  123.                     makeCallParams.DialPause = 2000;
  124.                     try
  125.                     {
  126.                         TapiCall call = line.MakeCall(phoneNumber, null, makeCallParams);
  127.                         MessageBox.Show("Created call " + phoneNumber + " -> " + call, "Sending…");
  128.                     }
  129.                     catch (Exception exc)
  130.                     {
  131.                         if (exc.Message.IndexOf("[0x80000005]") > -1 && myTAPI != null)
  132.                         {
  133.                             myTAPI.Shutdown();
  134.                             myTAPI = new TapiManager("GetCaller");
  135.                             if (myTAPI.Initialize())
  136.                                 this.MakePhoneCall(phoneNumber);
  137.                         }
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.  
  143.  
  144.         private void line_NewCall(object sender, NewCallEventArgs e)
  145.         {
  146.             MessageBox.Show("New call: " + e.Call, e.Privilege.ToString());
  147.         }
  148.  
  149.         private void line_CallStateChanged(object sender, CallStateEventArgs e)
  150.         {
  151.             MessageBox.Show("CallState: " + e.Call.ToString(), e.CallState.ToString());
  152.         }
  153.  
  154.         private void line_CallInfoChanged(object sender, CallInfoChangeEventArgs e)
  155.         {
  156.             MessageBox.Show("CallInfo: " + e.Call.ToString(), e.Change.ToString());
  157.         }
  158.  
  159.         private void button1_Click(object sender, EventArgs e)
  160.         {
  161.             MakePhoneCall("123456789");
  162.         }
  163.  
  164.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  165.         {
  166.             myTAPI.Shutdown();
  167.             Application.ExitThread();
  168.         }
  169.  
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement