MyTapiDlg.cpp
上传用户:gigotec
上传日期:2019-10-24
资源大小:31k
文件大小:11k
源码类别:

TAPI编程

开发平台:

Visual C++

  1. // MyTapiDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyTapi.h"
  5. #include "MyTapiDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CMyTapiDlg dialog
  50. CMyTapiDlg::CMyTapiDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CMyTapiDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CMyTapiDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CMyTapiDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CMyTapiDlg)
  63. // NOTE: the ClassWizard will add DDX and DDV calls here
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CMyTapiDlg, CDialog)
  67. //{{AFX_MSG_MAP(CMyTapiDlg)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_QUERYDRAGICON()
  71. ON_BN_CLICKED(IDC_ASSIT_PHONE, OnAssitPhone)
  72. ON_BN_CLICKED(IDC_BASIC_PHONE, OnBasicPhone)
  73. ON_BN_CLICKED(IDC_HANG_UP, OnHangUp)
  74. //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CMyTapiDlg message handlers
  78. BOOL CMyTapiDlg::OnInitDialog()
  79. {
  80. CDialog::OnInitDialog();
  81. // Add "About..." menu item to system menu.
  82. // IDM_ABOUTBOX must be in the system command range.
  83. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  84. ASSERT(IDM_ABOUTBOX < 0xF000);
  85. CMenu* pSysMenu = GetSystemMenu(FALSE);
  86. if (pSysMenu != NULL)
  87. {
  88. CString strAboutMenu;
  89. strAboutMenu.LoadString(IDS_ABOUTBOX);
  90. if (!strAboutMenu.IsEmpty())
  91. {
  92. pSysMenu->AppendMenu(MF_SEPARATOR);
  93. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  94. }
  95. }
  96. // Set the icon for this dialog.  The framework does this automatically
  97. //  when the application's main window is not a dialog
  98. SetIcon(m_hIcon, TRUE); // Set big icon
  99. SetIcon(m_hIcon, FALSE); // Set small icon
  100. // TODO: Add extra initialization here
  101. char c[10]={''},sc[10]={''};
  102. tapiGetLocationInfo(LPSTR(c),LPSTR(sc));
  103. char buffer[200];
  104. sprintf(buffer,"您的国家代号是:%s ,你所在地区或城市的区号是:%s",c,sc);
  105. SetDlgItemText(IDC_ZIP_CODE,buffer);
  106. return TRUE;  // return TRUE  unless you set the focus to a control
  107. }
  108. void CMyTapiDlg::OnSysCommand(UINT nID, LPARAM lParam)
  109. {
  110. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  111. {
  112. CAboutDlg dlgAbout;
  113. dlgAbout.DoModal();
  114. }
  115. else
  116. {
  117. CDialog::OnSysCommand(nID, lParam);
  118. }
  119. }
  120. // If you add a minimize button to your dialog, you will need the code below
  121. //  to draw the icon.  For MFC applications using the document/view model,
  122. //  this is automatically done for you by the framework.
  123. void CMyTapiDlg::OnPaint() 
  124. {
  125. if (IsIconic())
  126. {
  127. CPaintDC dc(this); // device context for painting
  128. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  129. // Center icon in client rectangle
  130. int cxIcon = GetSystemMetrics(SM_CXICON);
  131. int cyIcon = GetSystemMetrics(SM_CYICON);
  132. CRect rect;
  133. GetClientRect(&rect);
  134. int x = (rect.Width() - cxIcon + 1) / 2;
  135. int y = (rect.Height() - cyIcon + 1) / 2;
  136. // Draw the icon
  137. dc.DrawIcon(x, y, m_hIcon);
  138. }
  139. else
  140. {
  141. CDialog::OnPaint();
  142. }
  143. }
  144. // The system calls this to obtain the cursor to display while the user drags
  145. //  the minimized window.
  146. HCURSOR CMyTapiDlg::OnQueryDragIcon()
  147. {
  148. return (HCURSOR) m_hIcon;
  149. }
  150. void CMyTapiDlg::OnAssitPhone() 
  151. {
  152. // TODO: Add your control notification handler code here
  153. CString phonenum;
  154. GetDlgItemText(IDC_ASSIT_PHONE,phonenum);
  155. if(phonenum=="")
  156. {
  157. AfxMessageBox("please input the phone num!");
  158. }
  159. else
  160. {
  161. LONG lResult;
  162. lResult=tapiRequestMakeCall(phonenum,"TAPI","TAPI","简单的TAPI");
  163. if(lResult!=0)
  164. {
  165. AfxMessageBox("Sorry,tapiRequestMakeCall error!");
  166. }
  167. }
  168. }
  169. void CMyTapiDlg::InitLine()
  170. {
  171. m_bError=FALSE;
  172. LONG result=lineInitialize(m_hTAPI,AfxGetInstanceHandle(),TapiCallbackFunc,"MyTapi",&m_dwNumLines);
  173. if(result)
  174. {
  175. AfxMessageBox("tapi init error!");
  176. }
  177. }
  178. void  CALLBACK CMyTapiDlg::TapiCallbackFunc(DWORD device, DWORD msg, DWORD callbackinstance, DWORD param1, DWORD param2, DWORD param3)
  179. {
  180. CMyTapiDlg *pctapi=(CMyTapiDlg *)callbackinstance;
  181. switch(msg)
  182. {
  183. case LINE_CALLSTATE:
  184. //displayCallStates(
  185. break;
  186. case LINE_CLOSE:
  187. if(pctapi)
  188. {
  189. pctapi->ShutDown();
  190. }
  191. break;
  192. case LINE_REPLY:
  193. if(pctapi&&(param1==pctapi->m_lRequestID))
  194. {
  195. if(param2)
  196. {
  197. }
  198. else
  199. {
  200. pctapi->m_bhCallValid=TRUE;
  201. }
  202. }
  203. break;
  204. case LINE_CREATE:
  205. AfxMessageBox("Line Create");
  206. default:
  207. AfxMessageBox("TapiCallBackFuck 消息被屏蔽了!");
  208. break;
  209. }
  210. }
  211. void CMyTapiDlg::InitLineEx()
  212. {
  213. m_bError=FALSE;
  214. LONG result;
  215. DWORD version=0x00020002;
  216. LINEINITIALIZEEXPARAMS LIP;
  217. memset(&LIP,0,sizeof(LIP));
  218. LIP.dwTotalSize=LIP.dwUsedSize=sizeof(LIP);
  219. LIP.dwOptions=LINEINITIALIZEEXOPTION_USEHIDDENWINDOW;
  220. result=lineInitializeEx(m_hTAPI,AfxGetInstanceHandle(),TapiCallbackFunc,"MyTapi",&m_dwNumLines,&version,&LIP);
  221. if(result)
  222. {
  223. AfxMessageBox("tapi init error!");
  224. m_bError=TRUE;
  225. }
  226. else
  227. {
  228. m_dwTapiVersion=version;
  229. }
  230. }
  231. void CMyTapiDlg::NegotiateTAPIVersion()
  232. {
  233. if(m_bError) return;
  234. LONG result;
  235. LINEEXTENSIONID extensionid;
  236. DWORD tapiversion;
  237. char buffer[1024];
  238. BOOL having =FALSE;
  239. if(m_hTAPI)
  240. {
  241. for(DWORD line=0;line<m_dwNumLines;line++)
  242. {
  243. result=lineNegotiateAPIVersion(*m_hTAPI,line,0x00010003,0x00020002,&tapiversion,&extensionid);
  244. if(result)
  245. {
  246. continue;
  247. }
  248. having=TRUE;
  249. LPLINEDEVCAPS lpLineDevCaps =GetDevCaps(line,tapiversion);
  250. if((lpLineDevCaps)&&(lpLineDevCaps->dwLineNameSize)&&(lpLineDevCaps->dwLineNameOffset)&&
  251. (lpLineDevCaps->dwStringFormat==STRINGFORMAT_ASCII))
  252. {
  253. char* linename=((char *)lpLineDevCaps)+lpLineDevCaps->dwLineNameOffset;
  254. sprintf(buffer,"Device: %d  name: %s 协商的TAPI版本号: 0x%lx",line,linename,tapiversion);
  255. AfxMessageBox(buffer);
  256. }
  257. else
  258. {
  259. sprintf(buffer,"Device: %d no name 协商的TAPI版本号:0x%lx",line,tapiversion);
  260. AfxMessageBox(buffer);
  261. }
  262. if((lpLineDevCaps)&&(lpLineDevCaps->dwBearerModes&LINEBEARERMODE_VOICE)&&
  263. (lpLineDevCaps->dwLineFeatures&LINEFEATURE_MAKECALL)&&
  264. (lpLineDevCaps->dwMediaModes&LINEMEDIAMODE_INTERACTIVEVOICE))
  265. {
  266. m_dwLineToUse=line;
  267. m_dwTapiVersion=tapiversion;
  268. }
  269. free(lpLineDevCaps);
  270. return;
  271. }
  272. }
  273. }
  274. LPLINEDEVCAPS CMyTapiDlg::GetDevCaps(DWORD device, DWORD tapiversion)
  275. {
  276. if(m_bError) return NULL;
  277. LPLINEDEVCAPS lpLineDevCaps=NULL;
  278. size_t need=sizeof(LINEDEVCAPS);
  279. LONG result=0;
  280. for(int i=0;i<2;i++)
  281. {
  282. lpLineDevCaps=(LPLINEDEVCAPS)calloc(need,1);
  283. if(!lpLineDevCaps)
  284. {
  285. return NULL;
  286. }
  287. lpLineDevCaps->dwTotalSize=need;
  288. result=lineGetDevCaps(*m_hTAPI,device,tapiversion,0,lpLineDevCaps);
  289. if(result)
  290. {
  291. AfxMessageBox("sorry,error in call lineGetDevCaps!");
  292. free(lpLineDevCaps);
  293. return NULL;
  294. }
  295. if(lpLineDevCaps->dwNeededSize<=lpLineDevCaps->dwTotalSize)
  296. break;
  297. need=lpLineDevCaps->dwNeededSize;
  298. free(lpLineDevCaps);
  299. lpLineDevCaps=NULL;
  300. }
  301. if(lpLineDevCaps)
  302. free(lpLineDevCaps);
  303. return NULL;
  304. }
  305. void CMyTapiDlg::OpenLine()
  306. {
  307. if(m_bError) return;
  308. LONG result;
  309. result=lineOpen(*m_hTAPI,m_dwLineToUse,m_hLine,m_dwTapiVersion,0,(DWORD)this,
  310. LINECALLPRIVILEGE_NONE,LINEMEDIAMODE_INTERACTIVEVOICE,0);
  311. if(result)
  312. {
  313. AfxMessageBox("error in calling lineopen!");
  314. }
  315. }
  316. void CMyTapiDlg::Call(CString number)
  317. {
  318. if(m_bError) return;
  319. LPLINETRANSLATEOUTPUT LTO;
  320. char dialstr[256];
  321. LONG result;
  322. LTO=(LPLINETRANSLATEOUTPUT)calloc(512,1);
  323. if(!LTO)
  324. return;
  325. LTO->dwTotalSize=512;
  326. result=lineTranslateAddress(*m_hTAPI,m_dwLineToUse,m_dwTapiVersion,number,0,0,LTO);
  327. if(LTO->dwDialableStringSize>=256)
  328. return;
  329. strncpy(dialstr,&((const char*)LTO)[LTO->dwDialableStringOffset],
  330. LTO->dwDialableStringSize);
  331. dialstr[LTO->dwDialableStringSize]='';
  332. free(LTO);
  333. LINECALLPARAMS lcp;
  334. memset(&lcp,0,sizeof(LINECALLPARAMS));
  335. lcp.dwTotalSize=sizeof(LINECALLPARAMS);
  336. lcp.dwBearerMode=LINEBEARERMODE_VOICE;
  337. lcp.dwMediaMode=LINEMEDIAMODE_INTERACTIVEVOICE;
  338. result=lineMakeCall(*m_hLine,m_hCall,dialstr,0,&lcp);
  339. if(result<0)
  340. {
  341. AfxMessageBox("call error!");
  342. }
  343. if(result>0)
  344. {
  345. m_lRequestID=result;
  346. }
  347. }
  348. void CMyTapiDlg::GetID()
  349. {
  350. if(m_bError) return;
  351. LPVARSTRING str=NULL;
  352. size_t need=sizeof(VARSTRING);
  353. LONG result;
  354. for(int i=0;i<2;i++)
  355. {
  356. str=(LPVARSTRING)calloc(need,1);
  357. if(!str)
  358. return;
  359. result=lineGetID(*m_hLine,0,NULL,LINECALLSELECT_LINE,str,"comm/datamodem");
  360. if(result)
  361. {
  362. free(str);
  363. return;
  364. }
  365. if(str->dwNeededSize<=str->dwTotalSize)
  366. break;
  367. need=str->dwNeededSize;
  368. free(str);
  369. str=NULL;
  370. }
  371. m_hModem=*(LPHANDLE)(str+str->dwStringOffset);
  372. if(str)
  373. free(str);
  374. }
  375. void CMyTapiDlg::Drop()
  376. {
  377. if(m_bError) return;
  378. LONG result=0;
  379. if(m_bhCallValid&&*m_hCall)
  380. result=lineDrop(*m_hCall,NULL,0);
  381. if(result<0)
  382. {
  383. AfxMessageBox("error in drop!");
  384. }
  385. }
  386. void CMyTapiDlg::ShutDown()
  387. {
  388. if(m_bError) return;
  389. LONG result=0;
  390. Drop();
  391. if(*m_hTAPI)
  392. {
  393. result=lineShutdown(*m_hTAPI);
  394. }
  395. if(result)
  396. {
  397. AfxMessageBox("error in lineShutDown");
  398. }
  399. *m_hTAPI=NULL;
  400. m_dwNumLines=0;
  401. m_dwTapiVersion=0;
  402. m_dwLineToUse=0;
  403. *m_hLine=0;
  404. *m_hCall=0;
  405. m_bhCallValid=FALSE;
  406. }
  407. void CMyTapiDlg::OnBasicPhone() 
  408. {
  409. // TODO: Add your control notification handler code here
  410. InitLineEx();
  411. NegotiateTAPIVersion();
  412. OpenLine();
  413. CString phone;
  414. GetDlgItemText(IDC_PHONE_NUM,phone);
  415. Call(phone);
  416. }
  417. void CMyTapiDlg::OnHangUp() 
  418. {
  419. // TODO: Add your control notification handler code here
  420. ShutDown();
  421. }