?? extapiasync.cpp
字號:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// ExTAPIASync.CPP
//
// Sample that demonstrates some asynchronous functionality of ExTAPI
//
// NOTE: ExTAPI is a privileged API and any application using it must
// be signed using a priviliged certificate.
//
#include <windows.h>
#include <tapi.h>
#include <extapi.h>
#include <tsp.h>
#include <winuserm.h>
#include <aygshell.h>
#include "newres.h"
#include "resource.h"
// some definitons
#define ARRAY_LENGTH(x) (sizeof(x)/sizeof((x)[0]))
#define TAPI_API_LOW_VERSION 0x00020000
#define TAPI_API_HIGH_VERSION 0x00020000
#define EXT_API_LOW_VERSION 0x00010000
#define EXT_API_HIGH_VERSION 0x00010000
#define REASONABLE_BUFFER 1024
#define EINITFAILED (-1)
// global variables
HINSTANCE g_hInstance;
TCHAR g_szAppName[80];
TCHAR g_szTitle[80];
HLINEAPP g_hLineApp;
HLINE g_hLine;
HWND g_hwndOpList;
HWND g_hDlg;
// function declarations
BOOL InitExTAPI();
BOOL GetCurrentOperator(HWND hDlg);
BOOL GetAvailableOperators(HWND hDlg);
DWORD GetTSPLineDeviceID(const HLINEAPP hLineApp, const DWORD dwNumberDevices,
const DWORD dwAPIVersionLow, const DWORD dwAPIVersionHigh,
const TCHAR* const psTSPLineName);
BOOL CALLBACK MainDialogProc(const HWND hDlg, const UINT uiMessage,
const WPARAM wParam, const LPARAM lParam);
void CALLBACK TAPIProc(DWORD hDevice, DWORD dwMessage, DWORD dwInstance,
DWORD dwParam1, DWORD dwParam2, DWORD dwParam3);
BOOL InitDialog(const HWND hDlg, UINT nToolBarId);
void RegisterNewOperator();
void CleanupLB();
// ***************************************************************************
// Function Name: WinMain
//
// Purpose: Main entry point into the ExTAPIAsync program
//
// Arguments: Standard WinMain arguments
//
// Return Values: TRUE
//
// Description:
// Our WinMain function essentialy just pops up a Dialog box that displays
// the current operator and allows you to register with a new one
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPreviousInstance,
LPWSTR pszCommandLine,
int nCommandShow)
{
int iResult;
// Load strings
LoadString(hInstance, IDS_EXTAPIA_APPNAME, g_szAppName, ARRAY_LENGTH(g_szAppName));
LoadString(hInstance, IDS_EXTAPIA_TITLE, g_szTitle, ARRAY_LENGTH(g_szTitle));
// Use a globally named mutex to detect another instance of EXTAPIAsync
const HANDLE hMutex = CreateMutex(0, 0, TEXT("_EXTAPIA_EXE_MUTEX_"));
// check the result code
if(0 != hMutex) {
// First instance running? Okay to proceed...
if(ERROR_ALREADY_EXISTS != GetLastError()) {
g_hInstance = hInstance;
InitCommonControls();
// try to initialize ExTAPI
if (InitExTAPI()) {
iResult = DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_EXTAPIA_DLGMAIN),
0, (DLGPROC)MainDialogProc);
if (iResult) {
MessageBox(NULL, TEXT("An error occurred while getting ExTAPI information"),
TEXT("Error!!!"), MB_OK | MB_ICONERROR);
}
// cleanup
lineClose(g_hLine);
lineShutdown(g_hLineApp);
} else {
MessageBox(NULL, TEXT("Unable to initialize ExTAPI"),
TEXT("Error!!!"), MB_OK | MB_ICONERROR);
}
}
else {
// Already an instance running - attempt to switch to it and then exit
const HWND hWndExistingInstance = FindWindow(TEXT("Dialog"), g_szTitle);
VERIFY((0 == hWndExistingInstance) || SetForegroundWindow(hWndExistingInstance));
}
VERIFY(CloseHandle(hMutex));
}
return TRUE;
}
// ***************************************************************************
// Function Name: InitExTAPI
//
// Purpose: Set up ExTAPI
//
// Arguments: none
//
// Return Values:
// TRUE if successful, FALSE otherwise
//
// Side Effects:
// Sets g_hLineApp and g_hLine
// Sets up TapiProc as a TAPI callback function
//
// Description:
// This function initializes and opens lines for ExTAPI. The HLINEAPP and HLINE
// will be used later to get and set operators.
BOOL InitExTAPI()
{
DWORD dwNumDevs;
DWORD dwAPIVersion = TAPI_API_HIGH_VERSION;
LINEINITIALIZEEXPARAMS liep;
DWORD dwExtVersion;
// set the line init params
liep.dwTotalSize = sizeof(liep);
liep.dwOptions = LINEINITIALIZEEXOPTION_USEHIDDENWINDOW;
if (lineInitializeEx(&g_hLineApp, g_hInstance, &TAPIProc, TEXT("EXTAPISAMPLE"),
&dwNumDevs, &dwAPIVersion, &liep)) {
return FALSE;
}
// get the device ID
DWORD dwTAPILineDeviceID = GetTSPLineDeviceID(g_hLineApp, dwNumDevs,
TAPI_API_LOW_VERSION,
TAPI_API_HIGH_VERSION,
CELLTSP_LINENAME_STRING);
// error getting the line device ID?
if (0xffffffff == dwTAPILineDeviceID) {
lineShutdown(g_hLineApp);
return FALSE;
}
// now try and open the line
const DWORD dwMediaMode = LINEMEDIAMODE_DATAMODEM | LINEMEDIAMODE_INTERACTIVEVOICE;
if(lineOpen(g_hLineApp, dwTAPILineDeviceID,
&g_hLine, dwAPIVersion, 0, 0,
LINECALLPRIVILEGE_OWNER, dwMediaMode, 0)) {
lineShutdown(g_hLineApp);
return FALSE;
}
// set up ExTAPI
if (lineNegotiateExtVersion(g_hLineApp, dwTAPILineDeviceID,
dwAPIVersion, EXT_API_LOW_VERSION,
EXT_API_HIGH_VERSION, &dwExtVersion)) {
lineClose(g_hLine);
lineShutdown(g_hLineApp);
return FALSE;
}
return TRUE;
}
// ***************************************************************************
// Function Name: GetTSPLineDeviceID
//
// Purpose: To get a TSP Line Device ID
//
// Arguments:
// hLineApp = the HLINEAPP returned by lineInitializeEx
// dwNumberDevices = also returned by lineInitializeEx
// dwAPIVersionLow/High = min version of TAPI that we need
// psTSPLineName = "Cellular Line"
//
// Return Values: Current Device ID
//
// Description:
// This function returns the device ID of a named TAPI TSP. The Device ID is
// used in the call to lineOpen
DWORD GetTSPLineDeviceID(const HLINEAPP hLineApp,
const DWORD dwNumberDevices,
const DWORD dwAPIVersionLow,
const DWORD dwAPIVersionHigh,
const TCHAR* const psTSPLineName)
{
DWORD dwReturn = 0xffffffff;
for(DWORD dwCurrentDevID = 0 ; dwCurrentDevID < dwNumberDevices ; dwCurrentDevID++)
{
DWORD dwAPIVersion;
LINEEXTENSIONID LineExtensionID;
if(0 == lineNegotiateAPIVersion(hLineApp, dwCurrentDevID,
dwAPIVersionLow, dwAPIVersionHigh,
&dwAPIVersion, &LineExtensionID)) {
LINEDEVCAPS LineDevCaps;
LineDevCaps.dwTotalSize = sizeof(LineDevCaps);
if(0 == lineGetDevCaps(hLineApp, dwCurrentDevID,
dwAPIVersion, 0, &LineDevCaps)) {
BYTE* pLineDevCapsBytes = new BYTE[LineDevCaps.dwNeededSize];
if(0 != pLineDevCapsBytes) {
LINEDEVCAPS* pLineDevCaps = (LINEDEVCAPS*)pLineDevCapsBytes;
pLineDevCaps->dwTotalSize = LineDevCaps.dwNeededSize;
if(0 == lineGetDevCaps(hLineApp, dwCurrentDevID,
dwAPIVersion, 0, pLineDevCaps)) {
if(0 == _tcscmp((TCHAR*)((BYTE*)pLineDevCaps+pLineDevCaps->dwLineNameOffset), psTSPLineName)) {
dwReturn = dwCurrentDevID;
}
}
delete[] pLineDevCapsBytes;
}
}
}
}
return dwReturn;
}
// ***************************************************************************
// Function Name: MainDialogProc
//
// Purpose: Message Handler for ExTAPIAsync Dialog Box
//
// Arguments: Standard Dialog Procedure Arguments
//
// Return Values:
// EINITFAILED - One of the components in the dialog box could not be initialized
// 0 - success
//
// Side Effects:
// Sets g_hDlg and g_hwndOpList
//
// Description:
// Dialog Procedure for the main ExTAPIAsync Dialog. Displays the current
// operator and a list of available operators. Allows the user to try and register
// with a different operator.
BOOL CALLBACK MainDialogProc(const HWND hDlg, const UINT uiMessage,
const WPARAM wParam, const LPARAM lParam)
{
BOOL bProcessedMsg = TRUE;
switch(uiMessage)
{
case WM_INITDIALOG:
if (!InitDialog(hDlg, IDR_EXTAPIA_DLGMENU)) {
EndDialog(hDlg, EINITFAILED);
return TRUE;
}
// store the hDlg
g_hDlg = hDlg;
// get current operator
if (!GetCurrentOperator(hDlg)) {
EndDialog(hDlg, EINITFAILED);
return TRUE;
}
// get a handle to the listbox
VERIFY(g_hwndOpList = GetDlgItem(hDlg, IDC_EXTAPIA_AVAILOP));
// get available operators
if (!GetAvailableOperators(hDlg)) {
CleanupLB();
EndDialog(hDlg, EINITFAILED);
return TRUE;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -