📄 win32_modem.cpp
字号:
OutputDebugString("Service provider has closed the port");
return false;
} */
void Win32_Modem::Close()
{
CloseHandle(Comm);
Comm=INVALID_HANDLE_VALUE;
}
Win32_Modem::Win32_Modem()
{
connectState = false;
isInited = false;
commFile = NULL;
Comm = INVALID_HANDLE_VALUE;
// hCommFile = NULL;
}
Win32_Modem::~Win32_Modem()
{
ShutdownTAPI();
}
HWND Win32_Modem::GetMainWindow()
{
return g_hWndMainWindow;
}
void Win32_Modem::SetMainWindow(HWND theMainWindow)
{
g_hWndMainWindow=theMainWindow;
}
bool Win32_Modem::Init()
{
if(!isInited)
{
if (InitializeTAPI(NULL)==TRUE)
{
isInited=true;
return true;
}
else
return false;
}
return true;
}
bool Win32_Modem::Connect()
{
COMMCONFIG tempModemConfigSettings;
LINECALLSTATUS lpLineCallStatus;
long lReturn = 0;
if(!isInited)
{
if (InitializeTAPI(NULL)==TRUE)
isInited=true;
}
// GetCommProperties(Comm, &modemCommProperties);
// GetModemConfig(&tempModemConfigSettings);
if (connectState == false)
{
if(CheckForDataAccess()==false)
return false;
connectState = DialCall();
short count=0;
while ((WaitForCallState(LINECALLSTATE_CONNECTED)) &&
(count < 30))
{
/* lpLineCallStatus.dwTotalSize = sizeof(LINECALLSTATUS);
lReturn = lineGetCallStatus(*g_hCall, &lpLineCallStatus);
if (lReturn >= 0)
OutputDebugString("Waiting for connection to complete ");//lpLineCallStatus.dwCallState);
else
OutputDebugString("Failed call to lineGetCallStatus");
*/ count++;
}
if (count >= 30)
{
//OutputDebugString("Extremely long wait to connect, aborting");
return false;
}
else
{
LPVARSTRING lpvarString;
lpvarString = (VARSTRING *)malloc(100);
lpvarString->dwTotalSize = 100;
long lReturn = lineGetID(0,0, *g_hCall, LINECALLSELECT_CALL, lpvarString, "comm/datamodem");
if(lReturn < 0)
{
// OutputDebugString("Failed to set up comm");
}
else
{
Comm = *((LPHANDLE)((LPBYTE)lpvarString + lpvarString -> dwStringOffset));
/* DWORD bytes_written;
OVERLAPPED ovr_write = {0};
WriteFile(Comm, "FTGHJKL", 7, &bytes_written, &ovr_write);
OutputDebugString("Wrote bytes"); */
SetTimeouts();
}
free(lpvarString);
return true;
}
}
else
return false;
}
bool Win32_Modem::CheckForDataAccess()
{
return true; // in the future, use lineGetDevCaps to determine safety for data
}
bool Win32_Modem::GetModemConfig(COMMCONFIG *modemConfig)
{
bool err;
DWORD sizeReturned;
// err = GetCommConfig(Comm, &modemConfigSettings, &sizeReturned);
// memcpy(modemConfig, &modemConfigSettings, sizeReturned);
return err;
}
bool Win32_Modem::SetModemConfig(COMMCONFIG modemConfig)
{
bool err;
// memcpy(&modemConfigSettings, &modemConfig, sizeof(COMMCONFIG));
// err = SetCommConfig(Comm, &modemConfigSettings, sizeof(COMMCONFIG));
return err;
}
bool Win32_Modem::GetModemProperties(COMMPROP *modemProp)
{
bool err;
// err = GetCommProperties(Comm, &modemCommProperties);
// memcpy(modemProp, &modemCommProperties, sizeof(COMMPROP));
return err;
}
void Win32_Modem::SetBaudRate(long theBaudRate)
{
baudRate = theBaudRate;
}
void Win32_Modem::SetPhoneNumber(char * thePhoneNumber)
{
phoneNumber.assign(thePhoneNumber);
strcpy(g_szDialableAddress, thePhoneNumber);
strcpy(g_szDisplayableAddress, thePhoneNumber);
}
void Win32_Modem::GetBaudRate(long *theBaudRate)
{
*theBaudRate = baudRate;
}
void Win32_Modem::GetPhoneNumber(char **thePhoneNumber)
{
*thePhoneNumber = new char[phoneNumber.length()];
strcpy(*thePhoneNumber, phoneNumber.c_str());
}
void Win32_Modem::GetPhoneNumber(string &thePhoneNumber)
{
thePhoneNumber.assign(phoneNumber);
}
void Win32_Modem::StopComm()
{
// CloseHandle(g_hCloseEvent);
// Now close the comm port handle.
//CloseHandle(g_hCommFile);
// g_hCommFile = NULL;
}
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright 1995 - 1998 Microsoft Corporation. All Rights Reserved.
//
// MODULE: TapiCode.c
//
// PURPOSE: Handles all the TAPI routines for TapiComm.
//
//
// EXPORTED FUNCTIONS: These functions are for use by other modules.
//
// InitializeTAPI - Initialize this app with TAPI.
// ShutdownTAPI - Shutdown this app from TAPI.
// DialCall - Dial a Call.
// ModemHangupCall - Hangup an existing Call.
// PostHangupCall - Posts a HangupCall message to the main window.
//
// INTERNAL FUNCTIONS: These functions are for this module only.
//
// DialCallInParts - Actually Dial the call.
//
// lineCallbackFunc - TAPI callback for async messages.
//
// CheckAndReAllocBuffer - Helper function for I_ wrappers functions.
//
// I_lineNegotiateAPIVersion - Wrapper for lineNegotiateAPIVersion.
// I_lineGetDevCaps - Wrapper for lineGetDevCaps.
// I_lineGetAddressStatus - Wrapper for lineGetAddressStatus.
// I_lineTranslateAddress - Wrapper for lineTranslateAddress.
// I_lineGetCallStatus - Wrapper for lineGetCallStatus.
// I_lineGetAddressCaps - Wrapper for lineGetAddressCaps.
//
// WaitForCallState - Resynchronize by Waiting for a CallState.
// WaitForReply - Resynchronize by Waiting for a LINE_REPLY.
//
// DoLineReply - Handle asynchronous LINE_REPLY.
// DoLineClose - Handle asynchronous LINE_CLOSE.
// DoLineDevState - Handle asynchronous LINE_LINEDEVSTATE.
// DoLineCallState - Handle asynchronous LINE_CALLSTATE.
// DoLineCreate - Handle asynchronous LINE_CREATE.
//
// HandleLineErr - Handler for most LINEERR errors.
//
// HandleIniFileCorrupt - LINEERR handler for INIFILECORRUPT.
// HandleNoDriver - LINEERR handler for NODRIVER.
// HandleNoDevicesInstalled - LINEERR handler for NODEVICE.
// HandleReInit - LINEERR handler for REINIT.
// HandleNoMultipleInstance - LINEERR handler for NOMULTIPLEINSTANCE.
// HandleNoMem - LINEERR handler for NOMEM.
// HandleOperationFailed - LINEERR handler for OPERATIONFAILED.
// HandleResourceUnavail - LINEERR handler for RESOURCEUNAVAIL.
//
// LaunchModemControlPanelAdd - Launches the Modem Control Panel.
//
// WarningBox - Warn user if a line in use is removed.
//
// GetAddressToDial - Launches a GetAddressToDial dialog.
// DialDialogProc - Dialog Proc for the GetAddressToDial API.
//
// I_lineNegotiateLegacyAPIVersion - Wrapper to negoitiate with legacy TSPs
// VerifyUsableLine - Verify that a line device is usable
// FillTAPILine - Fill a combobox with TAPI Device names
// VerifyAndWarnUsableLine - Verify and warn if a line device is usable
// FillCountryCodeList - Fill a combobox with country codes
// FillLocationInfo - Fill a combobox with current TAPI locations
// UseDialingRules - Enable/Disable dialing rules controls
// DisplayPhoneNumber - Create and display a valid phone number
// PreConfigureDevice - Preconfigure a device line
// Structures needed to handle special non-dialable characters.
//**************************************************
// Entry points from the UI
//**************************************************
//
// FUNCTION: bool InitializeTAPI(HWND)
//
// PURPOSE: Initializes TAPI
//
// PARAMETERS:
// hWndParent - Window to use as parent of any dialogs.
//
// RETURN VALUE:
// Always returns 0 - command handled.
//
// COMMENTS:
//
// This is the API that initializes the app with TAPI.
// If NULL is passed for the hWndParent, then its assumed
// that re-initialization has occurred and the previous hWnd
// is used.
//
//
bool Win32_Modem::InitializeTAPI(HWND hWndParent)
{
long lReturn;
bool bTryReInit = TRUE;
// If we're already initialized, then initialization succeeds.
if (g_hLineApp)
return TRUE;
// If we're in the middle of initializing, then fail, we're not done.
if (g_bInitializing)
return FALSE;
g_bInitializing = TRUE;
// Initialize TAPI
do
{ //NULL was hInst
lReturn = lineInitialize(&g_hLineApp, GetAppInstance(), lineCallbackFunc, "TapiComm", &g_dwNumDevs);
// If we get this error, its because some other app has yet
// to respond to the REINIT message. Wait 5 seconds and try
// again. If it still doesn't respond, tell the user.
if (lReturn == LINEERR_REINIT)
{
if (bTryReInit)
{
MSG msg;
DWORD dwTimeStarted;
dwTimeStarted = GetTickCount();
while(GetTickCount() - dwTimeStarted < 5000)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
bTryReInit = FALSE;
continue;
}
else
{
MessageBox(g_hDlgParentWindow,
"A change to the system configuration requires that "
"all Telephony applications relinquish their use of "
"Telephony before any can progress. "
"Some have not yet done so."
,"Warning",MB_OK);
g_bInitializing = FALSE;
return FALSE;
}
}
if (lReturn == LINEERR_NODEVICE)
{
if (HandleNoDevicesInstalled())
continue;
else
{
//OutputDebugString("No devices installed.\n");
g_bInitializing = FALSE;
return FALSE;
}
}
if (HandleLineErr(lReturn))
continue;
else
{
g_bInitializing = FALSE;
return FALSE;
}
}
while(lReturn != SUCCESS);
// if hWndParent is a valid hWnd, we keep it as the parent for
// all dialogs.
if (IsWindow(hWndParent))
{
SetMainWindow(hWndParent);
g_hDlgParentWindow = Win32_Modem::GetMainWindow();//g_hWndMainWindow;
}
else
{
// Has the old g_hWndMainWindow gone away?
if (!IsWindow(Win32_Modem::GetMainWindow()))
{
//OutputDebugString("Main window unavailable.\n");
SetMainWindow(NULL);
g_hDlgParentWindow = NULL;
}
}
g_hCall = NULL;
g_hLine = NULL;
//OutputDebugString("Tapi initialized.\n");
g_bInitializing = FALSE;
g_dwAPIVersion = 0x00010004;
DWORD dwAPIVersion = 0x00010004;
gh_dwAPIVersion = dwAPIVersion;
return TRUE;
}
//
// FUNCTION: bool ShutdownTAPI()
//
// PURPOSE: Shuts down all use of TAPI
//
// PARAMETERS:
// None
//
// RETURN VALUE:
// True if TAPI successfully shut down.
//
// COMMENTS:
//
// If ShutdownTAPI fails, then its likely either a problem
// with the service provider (and might require a system
// reboot to correct) or the application ran out of memory.
//
//
/*bool Win32_Modem::ModemShutdownTAPI()
{
return(ShutdownTAPI());
} */
bool Win32_Modem::ShutdownTAPI()
{
long lReturn;
// If we aren't initialized, then Shutdown is unnecessary.
if (g_hLineApp == NULL)
return TRUE;
// Prevent ShutdownTAPI re-entrancy problems.
if (g_bShuttingDown)
return TRUE;
g_bShuttingDown = TRUE;
HangupCall();
do
{
lReturn = lineShutdown(g_hLineApp);
if (HandleLineErr(lReturn))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -