Using TAPI from C#

Category: Software, Visual C#.NET


With TAPI you can develop many types of telephony applications under Windows.



TAPI is the Microsoft telephony application programming interfaces (TAPI) which support the development of communications applications for Microsoft Windows, or Microsoft Windows NT OS’s.



The the Modem TAPI ActiveX is an OCX component that allows you to add the TAPI functionality to your program with a few source code lines only.


This sample will show you how to use the Modem TAPI ActiveX from C#.



Requirements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
 
static void Main()
{
	Application.Run(new frmMain());
}
 
//=========================================================
 
// TAPI Modem ActiveX - General sample.
 
// TAPI Modem ActiveX is an OCX control for software
// developers which can handle with phone calls using
// TAPI and modem.
 
// With this ActiveX you can make phone calls, receive
// phone calls, detect caller-id (caller name and
// caller number), detect digits, generate digits, and
// much more. This ActiveX can be easily integrate
// with WAV Recorder Player ActiveX, so you can record
// phone calls, play sound to the line, pre-screen
// calls (based on the sound card!), detect silence,
// detect voice, and more… fast and easy!
 
// This ActiveX is full supported with all the
// Voice/Data Modem for windows operation systems.
 
// This ActiveX can integrate easily with new or existing
// programs, all the functionality was compiled to a one
// OCX file, just drop the control to your form, and
// start to work immediately!
 
 
 
private void frmMain_Load(object sender, System.EventArgs e)
{
	//Vars:
	int iLineIndex;
	int iNumOfLines;
	string sDeviceName = "";
	int rc;
 
	//Device details type:
	TAPI_Modem.LineDevCap LineDev = new TAPI_Modem.LineDevCap();
 
	//Init the control, must be call
	//before using any functionality
	//of the control.
	//
	//sKey = Your license key.
	//
	AxcTapi1.Init("Trial Mode.");
 
	//First, init the TAPI environment:
	rc = AxcTapi1.LineInitialize();
 
	if (rc != 0)
	{
		MessageBox.Show("Cannot Init Tapi:" + AxcTapi1.GetTapiStrError(rc));
		return;
	}
 
	//Get the number of lines installed on the local machine:
	iNumOfLines = AxcTapi1.NumOfLines();
 
	// Scan all the lines and get the device name:
	for(iLineIndex=0; iLineIndex <= iNumOfLines ; iLineIndex++) 
	{
 
		// Get device details by the index
		AxcTapi1.LineIndex = iLineIndex;
		AxcTapi1.LineGetDevDetails(ref LineDev);
		sDeviceName = LineDev.DeviceName;
 
		// Add to the list DeviceName and DeviceIndex.
		Combo1.Items.Add(sDeviceName);
 
	} // LineIndex
 
	Combo1.SelectedIndex = 0;
	cboLineID.SelectedIndex = 0;
}
 
 
private void Form_Unload(ref int Cancel)
{
 
//Check if the line is open.
if ( AxcTapi1.IsLineOpen() == true ) AxcTapi1.LineShutdown();
 
//Check if the phone device is open.
if ( AxcTapi1.IsPhoneOpen() == true ) AxcTapi1.PhoneShutdown();
 
//Release the control
AxcTapi1.DeInit();
 
}
 
private void cmdLocProp_Click(object sender, System.EventArgs e)
{
 
	AxcTapi1.LineTranslateDialog();
 
}
 
private string Now()
{
	return DateTime.Now.ToString("hh:mm:ss");
}
 
//Just return True or False string
private string sTrueOrFalse(System.Int16 iVal)
{
	if (iVal == 0)
		return "False";
	else
		return "True";
}
 
private void cmdLineInfo_Click(object sender, System.EventArgs e)
{
 
	//Display the results:
	TAPI_Modem.LineDevCap LineDev = new TAPI_Modem.LineDevCap();
 
	//Set the line index to get the results for:
	AxcTapi1.LineIndex = Combo1.SelectedIndex;
 
	//Get the device index to the LineDev:
	AxcTapi1.LineGetDevDetails(ref LineDev);
 
	//Display the results:
	txtResults.Text = Now() + ":" + 
		Constants.vbCrLf + "Device Name:" + LineDev.DeviceName + Constants.vbCrLf +
		"Tapi Capability: " + sTrueOrFalse(LineDev.TapiCapability) + Constants.vbCrLf +
		"Max Rate: " + LineDev.MaxRate + Constants.vbCrLf +
		"Support Monitor Digits: " + sTrueOrFalse(LineDev.MonitorDigits) + Constants.vbCrLf + 
		"Support Generate Digits: " + sTrueOrFalse(LineDev.GenerateDigits) + Constants.vbCrLf + 
		"Data Mode:" + sTrueOrFalse(LineDev.MediaDatamodem) + Constants.vbCrLf + 
		"Interactivevoice:" + sTrueOrFalse(LineDev.MediaInteractivevoice) + Constants.vbCrLf + 
		"Auto Voice:" + sTrueOrFalse(LineDev.MediaAutomatedvoice) + Constants.vbCrLf + 
		"IsOpened:" + AxcTapi1.IsLineOpen() + Constants.vbCrLf + 
		"IsConnected:" + AxcTapi1.LineIsConnected() + Constants.vbCrLf;
 
}
 
private void cmdLocInfo_Click(object sender, System.EventArgs e)
{
 
	//Get the country code:
	txtResults.Text = txtResults.Text + "Local country Code:" + AxcTapi1.GetCountryCode() + Constants.vbCrLf;
 
	//Get the city code:
	txtResults.Text = txtResults.Text + "Local city Code:" + AxcTapi1.GetCityCode() + Constants.vbCrLf;
 
}
 
private void cmdConfig_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Display configuration:
	lErr = AxcTapi1.LineConfigDialog(Combo1.SelectedIndex);
	if (lErr != 0)
		txtResults.Text = txtResults.Text + AxcTapi1.GetTapiStrError(lErr) + Constants.vbCrLf;
}
 
private void cmdOn_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Set the line index:
	AxcTapi1.LineIndex = Combo1.SelectedIndex;
 
	//Open the line for in/out calls:
	lErr = AxcTapi1.LineOpen(TAPI_Modem.LINECALLPRIVILEGE.LINECALLPRIVILEGE_OWNER
	, TAPI_Modem.LINEMEDIAMODE.LINEMEDIAMODE_AUTOMATEDVOICE);
 
	//For data modem (not a voice modem):
	//lErr = AxcCallerID1.LineOpen(TAPI_Modem.LINECALLPRIVILEGE.LINECALLPRIVILEGE_OWNER
	//, Caller_ID_ActiveX.LINEMEDIAMODE.LINEMEDIAMODE_DATAMODEM);
 
	//Check the results:
	if (lErr != 0)
		txtResults.Text = txtResults.Text + Now() + ":Error open:" + Combo1.Text + " (" + AxcTapi1.GetTapiStrError(lErr) + ") Try to open with LINEMEDIAMODE_DATAMODEM parameter." + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + ":LineOpen " + Combo1.Text + " success." + Constants.vbCrLf;
}
 
private void cmdOff_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Close the line:
	lErr = AxcTapi1.LineClose();
	if (lErr != 0)
		txtResults.Text = txtResults.Text + Now() + ":LineClose Error:" + Combo1.Text + " (" + AxcTapi1.GetTapiStrError(lErr) + ")" + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + ":Close " + Combo1.Text + " is success." + Constants.vbCrLf;
 
	//Shutdown the line
	lErr = AxcTapi1.LineShutdown();
	if (lErr != 0)
		txtResults.Text = txtResults.Text + Now() + ":LineShutdown Error:" + Combo1.Text + " (" + AxcTapi1.GetTapiStrError(lErr) + ")" + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + ":Shutdown " + Combo1.Text + " is success." + Constants.vbCrLf;
 
}
 
private void cmdHold_Click(object sender, System.EventArgs e)
{
		int lErr;
 
	//Pleas note:
	//This function may not support a standard
	//56k voice modem (phone device only).
	lErr = AxcTapi1.lineHold();
 
	if (lErr != 0)
		txtResults.Text = txtResults.Text + Now() + "::LineHold Error:" + AxcTapi1.GetTapiStrError(lErr) + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::LineHold:Success." + Constants.vbCrLf;
 
}
 
private void cmdLineUnhold_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Pleas note:
	//This function may not support a standard
	//56k voice modem (phone device only).
	lErr = AxcTapi1.lineHold();
 
	if (lErr != 0)
		txtResults.Text = txtResults.Text + Now() + "::LineUnhold Error:" + AxcTapi1.GetTapiStrError(lErr) + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::LineUnhold:Success." + Constants.vbCrLf;
 
}
 
private void cmdAnswer_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Answer to call:
	lErr = AxcTapi1.LineAnswer();
	if (lErr < 0) 
		txtResults.Text = txtResults.Text + Now() + "::LineAnswer Error:" + Combo1.Text + " (" + AxcTapi1.GetTapiStrError(lErr) + ")" + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::LineAnswer:Success." + Constants.vbCrLf;
 
 
}
 
private void cmdDrop_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Drop the call:
	lErr = AxcTapi1.LineDrop();
 
	if (lErr > 0) 
		txtResults.Text = txtResults.Text + Now() + "::LineDrop:Success." + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::LineDrop Error:" + lErr + Constants.vbCrLf;
 
}
 
private void cmdMonitorDigit_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Monitor digits (to the MonitorDigit event):
	lErr = AxcTapi1.LineMonitorDigits(true);
 
	if (lErr != 0)
		txtResults.Text = txtResults.Text + Now() + "::LineMonitorDigits Error: " + AxcTapi1.GetTapiStrError(lErr) + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::LineMonitorDigits:Success." + Constants.vbCrLf;
 
}
 
private void cmdHandOff_Click(object sender, System.EventArgs e)
{
	int lErr;
	string sApplication;
 
	//Set the application mame to handoff to:
	sApplication = "Your Application.exe";
 
	lErr = AxcTapi1.LineHandoff(sApplication, TAPI_Modem.LINEMEDIAMODE.LINEMEDIAMODE_INTERACTIVEVOICE);
 
	if (lErr != 0) 
		txtResults.Text = txtResults.Text + Now() + "::LineHandoff Error: " + AxcTapi1.GetTapiStrError(lErr) + ")" + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::LineHandoff to " + sApplication + Constants.vbCrLf;
 
}
 
private void cmdPrivilege_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Set the call privilege:
	lErr = AxcTapi1.LineSetCallPrivilege(TAPI_Modem.LINECALLPRIVILEGE.LINECALLPRIVILEGE_OWNER);
	if (lErr != 0)
		txtResults.Text = txtResults.Text + AxcTapi1.GetTapiStrError(lErr) + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::LineSetPrivilege: " + AxcTapi1.GetTapiStrError(lErr) + Constants.vbCrLf;
 
}
 
private void cmdDial_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Dial a call
	lErr = AxcTapi1.LineMakeCall(txtPhoneNum.Text);
 
	if (lErr > 0)
		txtResults.Text = "LineMakeCall to " + txtPhoneNum.Text + "  in:" + Combo1.Text + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::Make a call to : " + txtPhoneNum.Text + " error (" + AxcTapi1.GetTapiStrError(lErr) + ")" + Constants.vbCrLf;
 
}
 
private void cmdGenerateDigits_Click(object sender, System.EventArgs e)
{
	int lErr;
 
	//Generate digits
	lErr = AxcTapi1.LineGenerateDigits(txtDigitsToGenerate.Text);
 
	if (lErr != 0)
		txtResults.Text = txtResults.Text + Now() + "::Generate Digits Error:" + Combo1.Text + " (" + AxcTapi1.GetTapiStrError(lErr) + ")" + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::Generate Digits " + txtDigitsToGenerate.Text + " is success." + Constants.vbCrLf;
 
}
 
private void cmdID_Click(object sender, System.EventArgs e)
{
 
	string sResults = ""; 
	int lResults;
 
	switch (cboLineID.SelectedIndex) 
	{ 
		case 0: 
 
			//Get the line ID for playing and recording 
 
			lResults = AxcTapi1.lineGetID(TAPI_Modem.DEVICECLASS.WAVEINOUT); 
 
			if (lResults > 0) 
			{ 
				sResults = "WAVEINOUT: " + lResults; 
			} 
			else 
			{ 
				sResults = "WAVEINOUT ERROR: " + AxcTapi1.GetTapiStrError(lResults); 
			} 
 
 
			break; 
		case 1: 
			//Get the line ID for recording 
			lResults = AxcTapi1.lineGetID(TAPI_Modem.DEVICECLASS.WAVEIN); 
			if (lResults > 0) 
			{ 
				sResults = "WAVEIN: " + lResults; 
			} 
			else 
			{ 
				sResults = "WAVEIN ERROR: " + AxcTapi1.GetTapiStrError(lResults); 
			} 
 
 
			break; 
		case 2: 
			//Get the line ID for playing 
			lResults = AxcTapi1.lineGetID(TAPI_Modem.DEVICECLASS.WAVEOUT); 
			if (lResults > 0) 
			{ 
				sResults = "WAVEOUT: " + lResults; 
			} 
			else 
			{ 
				sResults = "WAVEOUT ERROR: " + AxcTapi1.GetTapiStrError(lResults); 
			} 
 
 
			break; 
 
		case 3: 
			//Get the Port ID of the line 
			lResults = AxcTapi1.lineGetID(TAPI_Modem.DEVICECLASS.COMMDATA); 
			if (lResults > 0) 
			{ 
				sResults = "COMMDATA: " + lResults; 
			} 
			else 
			{ 
				sResults = "COMMDATA ERROR: " + AxcTapi1.GetTapiStrError(lResults); 
			} 
 
 
			break; 
	} 
 
	txtResults.Text = txtResults.Text + sResults + Constants.vbCrLf; 
 
}
 
private void cmdNumOfPhones_Click(object sender, System.EventArgs e)
{
	//Pleas note:
	//This function may not support a standard
	//56k voice modem (phone device only).
 
	//This will count the number of the devices that support the phone functionlity.
	AxcTapi1.PhoneInitialize();
	MessageBox.Show("" + AxcTapi1.NumOfPhones());
 
}
 
private void cmdOpenPhone_Click(object sender, System.EventArgs e)
{
	//Pleas note:
	//This function may not support a standard
	//56k voice modem (phone device only).
 
	int lErr;
 
	//Set the phone device index:
	AxcTapi1.PhoneIndex = Combo1.SelectedIndex;
 
	//Open the phone device:
	lErr = AxcTapi1.PhoneOpen();
 
	if (lErr != 0)
		txtResults.Text = txtResults.Text + Now() + "::Cannot open " + Combo1.Text + " as a phone device(" + AxcTapi1.GetTapiStrError(lErr) + ")" + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::Open " + Combo1.Text + " as a phone device success." + Constants.vbCrLf;
 
}
 
private void cmdClosePhone_Click(object sender, System.EventArgs e)
{
 
	//Pleas note:
	//This function may not support a standard
	//56k voice modem (phone device only).
 
	int lErr;
 
	//Close the phone device:
	lErr = AxcTapi1.PhoneShutdown();
 
	if (lErr != 0)
		txtResults.Text = txtResults.Text + AxcTapi1.GetTapiStrError(lErr) + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::Close " + Combo1.Text + " as a phone device" + Constants.vbCrLf;
 
 
}
 
private void cmdSetTerm_Click(object sender, System.EventArgs e)
{
	//Pleas note:
	//This function may not support a standard
	//56k voice modem (phone device only).
 
	int lErr;
 
	lErr = AxcTapi1.PhoneSetHookSwitch(TAPI_Modem.PHONEHOOKSWITCHDEV.PHONEHOOKSWITCHDEV_SPEAKER, TAPI_Modem.PHONEHOOKSWITCHMODE.PHONEHOOKSWITCHMODE_MICSPEAKER);
 
	if ( lErr != 0 )
		txtResults.Text = txtResults.Text + Now() + "::Error PhoneSetHookSwitch:" + AxcTapi1.GetTapiStrError(lErr) + Constants.vbCrLf;
	else
		txtResults.Text = txtResults.Text + Now() + "::PhoneSetHookSwitch " + Combo1.Text + Constants.vbCrLf;
}
 
 
 
 
// *************************************************************
// TAPI Events
// *************************************************************
 
private void AxcTapi1_CallerID(object sender, AxTAPI_Modem.__cTapi_CallerIDEvent e)
{
	txtResults.Text = txtResults.Text + Now() + ":Caller name:" + e.callerName + " Caller number:" + e.callerNumber + " in line index: " + AxcTapi1.LineIndex + Constants.vbCrLf;
}
 
private void AxcTapi1_Connected(object sender, System.EventArgs e)
{
	txtResults.Text = txtResults.Text + Now() + ":The call in line index:" + AxcTapi1.LineIndex + " is connected" + Constants.vbCrLf;
}
 
private void AxcTapi1_Dialing(object sender, System.EventArgs e)
{
	txtResults.Text = txtResults.Text + Now() + ":Dialing " + txtPhoneNum.Text + " in line index:" + AxcTapi1.LineIndex + Constants.vbCrLf;
}
 
private void AxcTapi1_Disconnected(object sender, System.EventArgs e)
{
	txtResults.Text = txtResults.Text + Now() + ":Disconnected call in line index:" + AxcTapi1.LineIndex + Constants.vbCrLf;
}
 
private void AxcTapi1_DoneGenerateDigits(object sender, System.EventArgs e)
{
	txtResults.Text = txtResults.Text + Now() + ":Done generate the " + txtDigitsToGenerate.Text + " digits in line index" + AxcTapi1.LineIndex + Constants.vbCrLf;
}
 
private void AxcTapi1_Idle(object sender, System.EventArgs e)
{
	txtResults.Text = txtResults.Text + Now() + ":Idel call in line index:" + AxcTapi1.LineIndex + Constants.vbCrLf;
}
 
private void AxcTapi1_IncomingCall(object sender, System.EventArgs e)
{
	txtResults.Text = txtResults.Text + Now() + ":Incoming call in line index:" + AxcTapi1.LineIndex + Constants.vbCrLf;
}
private void AxcTapi1_ProcNumber(object sender, AxTAPI_Modem.__cTapi_ProcNumberEvent e)
{
	txtResults.Text = txtResults.Text + Now() + AxcTapi1.LineIndex + " From process number" + e.procNumber + e.resultNumber + Constants.vbCrLf;
}
 
private void AxcTapi1_MonitorDigit(object sender, AxTAPI_Modem.__cTapi_MonitorDigitEvent e)
{
	txtResults.Text = txtResults.Text + Now() + ":Digit:" + e.digit + " Line index:" + AxcTapi1.LineIndex + Constants.vbCrLf;
}
 
private void AxcTapi1_Ring(object sender, AxTAPI_Modem.__cTapi_RingEvent e)
{
	txtResults.Text = txtResults.Text + Now() + ":Ring:" + e.ringNumber +
	"  Line index:" + AxcTapi1.LineIndex + Constants.vbCrLf;
}

Please note, the TAPI Modem ActiveX is using the classic version of TAPI (version 1.4), this allows you to use the TAPI under any version of Windows OS (95/NT/98/ME/2000/XP/2003/VISTA/Win7).


Links:
http://smart-activex.com/
http://smart-activex.com/tapi/





read the original story ...
comments: Closed tags: , , , , , ,

One Response to “Using TAPI from C#”

  1. administrative assistant says:

    Thanks for some quality points there. I am kind of new to online , so I printed this off to put in my file, any better way to go about keeping track of it then printing?



Pings responses to this post