📄 tapiutils.cpp
字号:
// If the buffer was big enough, then succeed.
if ((lpLineDevCaps -> dwNeededSize) <= (lpLineDevCaps -> dwTotalSize))
return lpLineDevCaps;
// Buffer wasn't big enough. Make it bigger and try again.
sizeofLineDevCaps = lpLineDevCaps -> dwNeededSize;
}
}
//
// FUNCTION: LPVOID CheckAndReAllocBuffer(LPVOID, size_t, LPCSTR)
//
// PURPOSE: Checks and ReAllocates a buffer if necessary.
//
LPVOID CTapiConnection::CheckAndReAllocBuffer(LPVOID lpBuffer, size_t sizeBufferMinimum)
{
size_t sizeBuffer;
if (lpBuffer == NULL) // Allocate the buffer if necessary.
{
sizeBuffer = sizeBufferMinimum;
lpBuffer = (LPVOID) LocalAlloc (LPTR, sizeBuffer);
if (lpBuffer == NULL)
{
OutputDebugString("LocalAlloc failed in CheckAndReAllocBuffer./n");
return NULL;
}
}
else // If the structure already exists, make sure its good.
{
sizeBuffer = LocalSize((HLOCAL) lpBuffer);
if (sizeBuffer == 0) // Bad pointer?
{
OutputDebugString("LocalSize returned 0 in CheckAndReAllocBuffer/n");
return NULL;
}
// Was the buffer big enough for the structure?
if (sizeBuffer < sizeBufferMinimum)
{
OutputDebugString("Reallocating structure\n");
LocalFree(lpBuffer);
return CheckAndReAllocBuffer(NULL, sizeBufferMinimum);
}
}
memset(lpBuffer, 0, sizeBuffer);
((LPVARSTRING) lpBuffer ) -> dwTotalSize = (DWORD) sizeBuffer;
return lpBuffer;
}
//
// FUNCTION: MylineGetAddressCaps(LPLINEADDRESSCAPS, ..)
//
// PURPOSE: Return a LINEADDRESSCAPS structure for the specified line.
//
LPLINEADDRESSCAPS CTapiConnection::MylineGetAddressCaps (
LPLINEADDRESSCAPS lpLineAddressCaps,
DWORD dwDeviceID, DWORD dwAddressID,
DWORD dwAPIVersion, DWORD dwExtVersion)
{
size_t sizeofLineAddressCaps = sizeof(LINEADDRESSCAPS) + 1024;
long lReturn;
// Continue this loop until the structure is big enough.
while(TRUE)
{
// Make sure the buffer exists, is valid and big enough.
lpLineAddressCaps =
(LPLINEADDRESSCAPS) CheckAndReAllocBuffer(
(LPVOID) lpLineAddressCaps,
sizeofLineAddressCaps);
if (lpLineAddressCaps == NULL)
return NULL;
// Make the call to fill the structure.
do
{
lReturn =
::lineGetAddressCaps(m_hLineApp,
dwDeviceID, dwAddressID, dwAPIVersion, dwExtVersion,
lpLineAddressCaps);
if (HandleLineErr(lReturn))
continue;
else
{
OutputDebugString("lineGetAddressCaps unhandled error\n");
LocalFree(lpLineAddressCaps);
return NULL;
}
}
while (lReturn != SUCCESS);
// If the buffer was big enough, then succeed.
if ((lpLineAddressCaps -> dwNeededSize) <=
(lpLineAddressCaps -> dwTotalSize))
{
return lpLineAddressCaps;
}
// Buffer wasn't big enough. Make it bigger and try again.
sizeofLineAddressCaps = lpLineAddressCaps -> dwNeededSize;
}
}
//
// FUNCTION: MakeTheCall(LPLINEDEVCAPS, LPCSTR)
//
// PURPOSE: Dials the call
//
BOOL CTapiConnection::SetModemConfig()
{
CommID *cid;
VARSTRING *vs;
LONG lrc;
DWORD dwSize;
LPCOMMCONFIG pCommConfig;
DWORD dwsize;
MODEMSETTINGS * pModemSettings;
if (!(vs=(VARSTRING *)calloc(1,sizeof(VARSTRING))))
return LINEERR_NOMEM;
vs->dwTotalSize=sizeof(VARSTRING);
for (;;) {
lrc=lineGetID(m_hLine,0L,NULL,
LINECALLSELECT_LINE,vs,"comm/datamodem");
if (lrc) { free(vs); return FALSE; }
if (vs->dwTotalSize<vs->dwNeededSize)
{
dwSize=vs->dwNeededSize;
free(vs);
if (!(vs=(VARSTRING *)calloc(1,dwSize)))
return LINEERR_NOMEM;
vs->dwTotalSize=dwSize;
continue;
}
break;
}
cid=(CommID *)((LPSTR)vs+vs->dwStringOffset);
m_hComm=cid->hComm;
free(vs);
dwsize=sizeof(COMMCONFIG)+sizeof(MODEMSETTINGS);
if(!(pCommConfig=(COMMCONFIG*)calloc(1,dwsize)))
return FALSE;
pCommConfig->dwSize=dwsize;
lrc=GetCommConfig(m_hComm,pCommConfig,&dwsize);
if (!lrc)
{
free(pCommConfig);
return FALSE;
}
pModemSettings=(MODEMSETTINGS *)pCommConfig->wcProviderData;
pModemSettings->dwPreferredModemOptions=467;//DIALOPTION_DIALTONE;
lrc=SetCommConfig(m_hComm,pCommConfig,dwSize);
CloseHandle(m_hComm);
free(pCommConfig);
return TRUE;
}
BOOL CTapiConnection::MakeTheCall(LPLINEDEVCAPS lpLineDevCaps, LPCTSTR lpszAddress)
{
LPLINECALLPARAMS lpCallParams = NULL;
LPLINEADDRESSCAPS lpAddressCaps = NULL;
long lReturn;
BOOL bFirstDial = TRUE;
// Get the capabilities for the line device we're going to use.
// SetModemConfig();
lpAddressCaps = MylineGetAddressCaps(lpAddressCaps,
m_dwDeviceID, 0, m_dwAPIVersion, 0);
if (lpAddressCaps == NULL)
return FALSE;
// Setup our CallParams.
lpCallParams = CreateCallParams (lpCallParams, lpszAddress);
if (lpCallParams == NULL)
return FALSE;
do
{
if (bFirstDial)
// lReturn = ::lineMakeCall(m_hLine, &m_hCall, lpszAddress, 0, lpCallParams);
lReturn = WaitForReply( ::lineMakeCall(m_hLine, &m_hCall, lpszAddress,
0, lpCallParams) );
else
lReturn = WaitForReply(::lineDial(m_hCall, lpszAddress, 0));
if (lReturn == WAITERR_WAITABORTED)
{
OutputDebugString("While Dialing, WaitForReply aborted.\n");
goto errExit;
}
if (lReturn==LINEERR_OPERATIONFAILED)
goto errExit;
if (HandleLineErr(lReturn))
continue;
else
{
if (bFirstDial)
OutputDebugString("lineMakeCall unhandled error\n");
else
OutputDebugString("lineDial unhandled error\n");
goto errExit;
}
}
while (lReturn != SUCCESS);
bFirstDial = FALSE;
if (lpCallParams)
LocalFree(lpCallParams);
if (lpAddressCaps)
LocalFree(lpAddressCaps);
return TRUE;
errExit:
if (lpCallParams)
LocalFree(lpCallParams);
if (lpAddressCaps)
LocalFree(lpAddressCaps);
// AfxMessageBox("登记/取消遇忙转移失败");
return FALSE;
}
//
// FUNCTION: CreateCallParams(LPLINECALLPARAMS, LPCSTR)
//
// PURPOSE: Allocates and fills a LINECALLPARAMS structure
//
//
LPLINECALLPARAMS CTapiConnection::CreateCallParams (
LPLINECALLPARAMS lpCallParams, LPCSTR lpszDisplayableAddress)
{
size_t sizeDisplayableAddress;
if (lpszDisplayableAddress == NULL)
lpszDisplayableAddress = "";
sizeDisplayableAddress = strlen(lpszDisplayableAddress) + 1;
lpCallParams = (LPLINECALLPARAMS) CheckAndReAllocBuffer(
(LPVOID) lpCallParams,
sizeof(LINECALLPARAMS) + sizeDisplayableAddress);
if (lpCallParams == NULL)
return NULL;
// This is where we configure the line.
lpCallParams -> dwBearerMode = LINEBEARERMODE_VOICE;
lpCallParams -> dwMediaMode = LINEMEDIAMODE_DATAMODEM;
//LINEMEDIAMODE_INTERACTIVEVOICE;
// This specifies that we want to use only IDLE calls and
// don't want to cut into a call that might not be IDLE (ie, in use).
lpCallParams -> dwCallParamFlags =LINECALLPARAMFLAGS_SECURE ;
// if there are multiple addresses on line, use first anyway.
// It will take a more complex application than a simple tty app
// to use multiple addresses on a line anyway.
lpCallParams -> dwAddressMode = LINEADDRESSMODE_ADDRESSID;
// Address we are dialing.
lpCallParams -> dwDisplayableAddressOffset = sizeof(LINECALLPARAMS);
lpCallParams -> dwDisplayableAddressSize = sizeDisplayableAddress;
strcpy((LPSTR)lpCallParams + sizeof(LINECALLPARAMS),
lpszDisplayableAddress);
// lpCallParams->dwNoAnswerTimeout=10;
return lpCallParams;
}
//
// FUNCTION: long WaitForReply(long)
//
// PURPOSE: Resynchronize by waiting for a LINE_REPLY
//
// PARAMETERS:
// lRequestID - The asynchronous request ID that we're
// on a LINE_REPLY for.
//
// RETURN VALUE:
// - 0 if LINE_REPLY responded with a success.
// - LINEERR constant if LINE_REPLY responded with a LINEERR
// - 1 if the line was shut down before LINE_REPLY is received.
//
// COMMENTS:
//
// This function allows us to resynchronize an asynchronous
// TAPI line call by waiting for the LINE_REPLY message. It
// waits until a LINE_REPLY is received or the line is shut down.
//
// Note that this could cause re-entrancy problems as
// well as mess with any message preprocessing that might
// occur on this thread (such as TranslateAccelerator).
//
// This function should to be called from the thread that did
// lineInitialize, or the PeekMessage is on the wrong thread
// and the synchronization is not guaranteed to work. Also note
// that if another PeekMessage loop is entered while waiting,
// this could also cause synchronization problems.
//
// One more note. This function can potentially be re-entered
// if the call is dropped for any reason while waiting. If this
// happens, just drop out and assume the wait has been canceled.
// This is signaled by setting bReentered to FALSE when the function
// is entered and TRUE when it is left. If bReentered is ever TRUE
// during the function, then the function was re-entered.
//
// This function times out and returns WAITERR_WAITTIMEDOUT
// after WAITTIMEOUT milliseconds have elapsed.
//
//
long CTapiConnection::WaitForReply (long lRequestID)
{
static BOOL bReentered;
bReentered = FALSE;
DWORD dwTimeStarted,dwTimeEnd;
if (lRequestID > SUCCESS)
{
MSG msg;
m_bReplyReceived = FALSE;
m_dwRequestedID = (DWORD) lRequestID;
// Initializing this just in case there is a bug
// that sets m_bReplyRecieved without setting the reply value.
m_lAsyncReply = LINEERR_OPERATIONFAILED;
dwTimeStarted = GetTickCount();
while(!m_bReplyReceived)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (SystemStatus.m_dwDialStatus==1)
{
bReentered = TRUE;
return LINEERR_OPERATIONFAILED;
}
// This should only occur if the line is shut down while waiting.
if ((m_hCall != NULL) &&(!m_bTapiInUse || bReentered))
{
bReentered = TRUE;
return WAITERR_WAITABORTED;
}
dwTimeEnd=GetTickCount();
// Its a really bad idea to timeout a wait for a LINE_REPLY.
// Ifwe are execting a LINE_REPLY, we should wait till we get
// it; it might take a long time to dial (for example).
// If 5 seconds go by without a reply, it might be a good idea
// to display a dialog box to tell the user that a
// wait is in progress and to give the user the capability to
// abort the wait.
if(dwTimeEnd>dwTimeStarted+13000)
{
bReentered=TRUE;
return SUCCESS;
}
}
bReentered = TRUE;
return m_lAsyncReply;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -