⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytapi_.c

📁 支持XMODEM,YMODEM,FAX协议的串口通信软件包的下一个:基于TAPI应用的通信应用
💻 C
📖 第 1 页 / 共 4 页
字号:

#include "comdial.h"
#include "resource.h"


MYTAPI mytapi;
extern HWND hTTYWnd;

/////////////////////////////////////////////////////////////////////
//	telephonyInitialize - megafunction to retrieve all 
//		configuration information needed for telephony, plus
//		initialize TAPI and open the line.
////////////////////////////////////////////////////////////////////
int telephonyInitialize(HWND hWnd, HINSTANCE hInst)
{
	LONG lrc;
	int i;
	LINEEXTENSIONID extensions;

	// initialize application use of TAPI
	while (lineInitialize(&mytapi.hTAPI, hInst, 
			   mytapi.CallbackProc = (LINECALLBACK)MakeProcInstance((FARPROC)LineCallBackProc, hInst),
			"TAPIProcess", &mytapi.dwNumLines) == LINEERR_REINIT) {
		Sleep (5);			// sleep for five seconds
		if (MessageBox(hWnd, "Telephony system is reinitializing \
					   - Click Cancel to abort", "Error",
					   MB_RETRYCANCEL) ==IDCANCEL)
			goto done;
	} // end while (TAPI reinitializing)

	// bool indicates lineInitialize called successfully
	mytapi.bInitialized = TRUE;

	// check every logical line for one that support modem data
	for (i=0; (unsigned)i<mytapi.dwNumLines; i++) {

		// negotiate version of TAPI to use
		lrc = lineNegotiateAPIVersion(mytapi.hTAPI, i, 
				WIN95TAPIVERSION, WIN95TAPIVERSION, 
				&mytapi.dwVersionToUse, &extensions);
		if (lrc)
			continue;
		
		// get line device caps
		lrc = mylineGetDevCaps ();
		if (lrc)
			goto done;

		// check that the line supports modems
		if (mytapi.pLinedevcaps->dwMediaModes & LINEMEDIAMODE_DATAMODEM) {
			mytapi.dwLine = i;
			break;
		}

	}		

	// get current configuration of serial comm device
	lrc = mylineGetOldDevConfig();
	if (lrc)
		goto done;
	lrc = mylineGetDevConfig();
	if (lrc)
		goto done;

	return 0;

done:
	// error!  clean up and return error code

	telephonyShutdown();

	return lrc;

}

int telephonyOpen(HWND hWnd, HINSTANCE hInst)
{
	LONG lrc;

	// open the line device
	lrc = lineOpen(mytapi.hTAPI, mytapi.dwLine, &mytapi.hLine,
				   mytapi.dwVersionToUse, 0, 0, 
				   LINECALLPRIVILEGE_OWNER, LINEMEDIAMODE_DATAMODEM,
				   NULL);
	if (lrc)
		goto done;

	// bool indicates lineOpen called successfully
	mytapi.bLineopen = TRUE;

	// get the telephony icon and make it ours
	myDrawTAPIIcon(hWnd);

	// get other telephony configuration info
	lrc = telephonyCaps();
	if (lrc)
		goto done;

	// receive all possible status messages for the 
	// line device and address
	lineSetStatusMessages(mytapi.hLine, 
		mytapi.pLinedevcaps->dwLineStates, 
		mytapi.pLineaddresscaps->dwAddressStates);

	// get modem capabilities			
	lrc = telephonyGetModemCaps();
	if (lrc)
		goto done;

	// get current modem settings
	lrc = telephonyGetModemSettings();
	if (lrc)
		goto done;

	// success

	return 0;

done:
	// error!  clean up and return error code

	telephonyShutdown();

	return lrc;

} // end procedure (telephony initialize)

////////////////////////////////////////////////////////////////////
//	telephonyGetModemCaps - get COMMPROP and MODEMDEVCAPS 
//		structures
////////////////////////////////////////////////////////////////////
LONG telephonyGetModemCaps()
{
	DWORD dwsize = sizeof(COMMPROP)+sizeof(MODEMDEVCAPS);
	LONG lrc;

	// if space already allocated for structure, free it up
	if (mytapi.bCommpropalloced) {
		free (mytapi.pCommprop);
		mytapi.bCommpropalloced = FALSE;
	}

	// allocate memory for structure
    mytapi.pCommprop = (COMMPROP *) calloc (1, dwsize);
	if (!mytapi.pCommprop)
		return -1;
	mytapi.bCommpropalloced = TRUE;
	// set structure size
    mytapi.pCommprop->wPacketLength = (WORD)dwsize;


	// get modem handle for use with serial comm api
	// NOTE:  WE GET THE HANDLE, USE IT, THEN CLOSE IT AGAIN RIGHT
	// AWAY SO THAT THE MODEM IS NOT RESERVED BY OUR APPLICATION
	lrc = mylineGetID();
	if (lrc) {
		free (mytapi.pCommprop);
		mytapi.bCommpropalloced = FALSE;
		return -1;
	}
	// get information into structure
    lrc = GetCommProperties(mytapi.hComm, mytapi.pCommprop);

	// Close the modem handle
	CloseHandle (mytapi.hComm);
	mytapi.bGotcommhandle = FALSE;

	// bomb out if error
	if (!lrc)  {
		free (mytapi.pCommprop);
		mytapi.bCommpropalloced = FALSE;
		return -1;
	}

	// reallocate and try again
	mytapi.pModemcaps = (MODEMDEVCAPS *) mytapi.pCommprop->wcProvChar;
	dwsize = mytapi.pModemcaps->dwRequiredSize + sizeof(COMMPROP);
	free (mytapi.pCommprop);
	mytapi.bCommpropalloced = FALSE;
    mytapi.pCommprop = (COMMPROP *) calloc (1, dwsize);
	if (!mytapi.pCommprop)
		return -1;
	mytapi.bCommpropalloced = TRUE;
    mytapi.pCommprop->wPacketLength = (WORD)dwsize;

	// get modem handle for use with serial comm api
	// NOTE:  WE GET THE HANDLE, USE IT, THEN CLOSE IT AGAIN RIGHT
	// AWAY SO THAT THE MODEM IS NOT RESERVED BY OUR APPLICATION
	lrc =  mylineGetID();
	if (lrc) {
		free (mytapi.pCommprop);
		mytapi.bCommpropalloced = FALSE;
		return -1;
	}
	// try again
    lrc = GetCommProperties(mytapi.hComm, mytapi.pCommprop);

	// Close the modem handle
	CloseHandle (mytapi.hComm);
	mytapi.bGotcommhandle = FALSE;

	if (!lrc)  {
		free (mytapi.pCommprop);
		mytapi.bCommpropalloced = FALSE;
		return -1;
	}
	mytapi.pModemcaps = (MODEMDEVCAPS *) mytapi.pCommprop->wcProvChar;

	return 0;


} // end procedure (telephonyGetModemCaps)

////////////////////////////////////////////////////////////////////
//	telephonyGetModemSettings - get COMMCONFIG and MODEMSETTINGS 
//		structures
////////////////////////////////////////////////////////////////////
LONG telephonyGetModemSettings()
{
	DWORD dwsize = sizeof(COMMCONFIG)+sizeof(MODEMSETTINGS);
	LONG lrc;

	do {

		// if space already allocated for structure, free it up
		if (mytapi.bCommconfigalloced) {
			free (mytapi.pCommconfig);
			mytapi.bCommconfigalloced = FALSE;
		}

		// allocate memory for structure
	    mytapi.pCommconfig = (COMMCONFIG *) calloc (1, dwsize);
		if (!mytapi.pCommconfig)
			return -1;
		mytapi.bCommconfigalloced = TRUE;
		// set structure size
	    mytapi.pCommconfig->dwSize = dwsize;

		// get modem handle for use with serial comm api
		// NOTE:  WE GET THE HANDLE, USE IT, THEN CLOSE IT AGAIN
		// RIGHT AWAY SO THAT THE MODEM IS NOT RESERVED BY OUR 
		// APPLICATION
		lrc = mylineGetID();
		if (lrc) {
			free (mytapi.pCommconfig);
			mytapi.bCommconfigalloced = FALSE;
			return -1;
		}
		// get information into structure
	    lrc = GetCommConfig(mytapi.hComm, mytapi.pCommconfig, &dwsize);

		// Close the modem handle
		CloseHandle (mytapi.hComm);
		mytapi.bGotcommhandle = FALSE;

		// bomb out if error
		if (!lrc)  {
			free (mytapi.pCommconfig);
			mytapi.bCommconfigalloced = FALSE;
			return -1;
		}

		mytapi.pModemsettings = (MODEMSETTINGS *) mytapi.pCommconfig->wcProviderData;
		// try again
		if (mytapi.pCommconfig->dwSize < dwsize) {
			continue;
		}

		break;
	} while (TRUE);

	return 0;

} // end procedure (telephonyGetModemSettings)

////////////////////////////////////////////////////////////////////
//	myDrawTAPIIcon - get telephony icon and make it ours
////////////////////////////////////////////////////////////////////
void myDrawTAPIIcon(HWND hwnd) 
{
	HICON hIcon;

	// get handle to telephony icon
	lineGetIcon (mytapi.dwLine, NULL, &hIcon);
	// make the icon ours
	SetClassLong (hwnd, GCL_HICON, (LONG)hIcon);

}

////////////////////////////////////////////////////////////////////
//	mylineGetDevConfig - get configuration of serial device
////////////////////////////////////////////////////////////////////
LONG mylineGetDevConfig()
{
	LONG lrc;
	DWORD dwSize;


	// if space already allocated for structure, free it up
	if (mytapi.bDevconfigalloced) {
		free (mytapi.pDevconfig);
		mytapi.bDevconfigalloced = FALSE;
	}

	// allocate memory for structure
    mytapi.pDevconfig = (VARSTRING *) calloc (1, sizeof(VARSTRING));
	if (!mytapi.pDevconfig)
		return -1;
	mytapi.bDevconfigalloced = TRUE;

	// set structure size
    mytapi.pDevconfig->dwTotalSize = sizeof(VARSTRING);

    do {
			// get information into structure
            lrc = lineGetDevConfig(mytapi.dwLine, 
            	mytapi.pDevconfig, 
            	"comm/datamodem");
			// bomb out if error
			if (lrc)  {
				free (mytapi.pDevconfig);
				mytapi.bDevconfigalloced = FALSE;
				return lrc;
			}
			// reallocate and try again
			if (mytapi.pDevconfig->dwTotalSize 
				< mytapi.pDevconfig->dwNeededSize) {
                    dwSize = mytapi.pDevconfig->dwNeededSize;
                    free (mytapi.pDevconfig);
					mytapi.bDevconfigalloced = FALSE;
                    mytapi.pDevconfig = (VARSTRING *) calloc(1, dwSize);
					if (!mytapi.pDevconfig)
						return -1;
            		mytapi.bDevconfigalloced = TRUE;
			        mytapi.pDevconfig->dwTotalSize = dwSize;
					continue;
            } /* end if (need more space) */
            break; /* success  */
    } while (TRUE);

	return lrc;

}
////////////////////////////////////////////////////////////////////
//	mylineGetOldDevConfig - get configuration of serial device for
//	later restore when program terminates
////////////////////////////////////////////////////////////////////
LONG mylineGetOldDevConfig()
{
	LONG lrc;
	DWORD dwSize;


	// if space already allocated for structure, free it up
	if (mytapi.bOlddevconfigalloced) {
		free (mytapi.pOlddevconfig);
		mytapi.bOlddevconfigalloced = FALSE;
	}

	// allocate memory for structure
    mytapi.pOlddevconfig = (VARSTRING *) calloc (1, sizeof(VARSTRING));
	if (!mytapi.pOlddevconfig)
		return -1;
	mytapi.bOlddevconfigalloced = TRUE;

	// set structure size
    mytapi.pOlddevconfig->dwTotalSize = sizeof(VARSTRING);

    do {
			// get information into structure
            lrc = lineGetDevConfig(mytapi.dwLine, 
            	mytapi.pOlddevconfig, 
            	"comm/datamodem");
			// bomb out if error
			if (lrc)  {
				free (mytapi.pOlddevconfig);
				mytapi.bOlddevconfigalloced = FALSE;
				return lrc;
			}
			// reallocate and try again
			if (mytapi.pOlddevconfig->dwTotalSize 
				< mytapi.pOlddevconfig->dwNeededSize) {
                    dwSize = mytapi.pOlddevconfig->dwNeededSize;
                    free (mytapi.pOlddevconfig);
					mytapi.bOlddevconfigalloced = FALSE;
                    mytapi.pOlddevconfig = (VARSTRING *) calloc(1, dwSize);
					if (!mytapi.pOlddevconfig)
						return -1;
            		mytapi.bOlddevconfigalloced = TRUE;
			        mytapi.pOlddevconfig->dwTotalSize = dwSize;
					continue;
            } /* end if (need more space) */
            break; /* success  */
    } while (TRUE);

	return lrc;

}
////////////////////////////////////////////////////////////////////
//	mylineGetID - get modem handle
////////////////////////////////////////////////////////////////////
LONG mylineGetID ()
{
    CommID FAR *cid;
    VARSTRING  *vs;
    LONG lrc;
    DWORD dwSize;

		mytapi.bGotcommhandle = FALSE;

		// allocate memory for structure
        vs = (VARSTRING *) calloc (1, sizeof(VARSTRING));
		// set structure size
        vs->dwTotalSize = sizeof(VARSTRING);

        do {
				// get information into structure
                lrc = lineGetID(mytapi.hLine, 0L, NULL, 
							    LINECALLSELECT_LINE, vs, 
								"comm/datamodem");
				// bomb out if error
				if (lrc)  {
					free (vs);
					return -1;
				}
				// reallocate and try again
				if (vs->dwTotalSize < vs->dwNeededSize) {
                        dwSize = vs->dwNeededSize;
                        free (vs);
                        vs = (VARSTRING *) calloc(1, dwSize);
                        vs->dwTotalSize = dwSize;
						continue;
                } /* end if (need more space) */
                break; /* success  */
        } while (TRUE);

		// copy modem handle and modem name from structure
        cid = (CommID FAR *) ((LPSTR)vs + vs->dwStringOffset);
        lstrcpy (mytapi.szModemName, &cid->szDeviceName[0]);
		// save modem handle
		mytapi.hComm = cid->hComm;
		// done with structure; free it
		free (vs);

		// set flag to indicate modem handle has been retrieved
		mytapi.bGotcommhandle = TRUE;

		return 0;

} /* end function (GetCommHandle) */
////////////////////////////////////////////////////////////////////
//	mylineGetCallID - get modem handle associated with call
////////////////////////////////////////////////////////////////////
LONG mylineGetCallID ()
{
    CommID FAR *cid;
    VARSTRING  *vs;
    LONG lrc;
    DWORD dwSize;

		mytapi.bGotcommhandle = FALSE;

		// allocate memory for structure
        vs = (VARSTRING *) calloc (1, sizeof(VARSTRING));
		// set structure size
        vs->dwTotalSize = sizeof(VARSTRING);

        do {
				// get information into structure
                lrc = lineGetID(NULL, 0L, mytapi.hCall, 
								LINECALLSELECT_CALL, vs, 
								"comm/datamodem");
				// bomb out if error
				if (lrc)  {
					free (vs);
					return -1;
				}
				// reallocate and try again
				if (vs->dwTotalSize < vs->dwNeededSize) {
                        dwSize = vs->dwNeededSize;
                        free (vs);
                        vs = (VARSTRING *) calloc(1, dwSize);
                        vs->dwTotalSize = dwSize;
						continue;
                } /* end if (need more space) */
                break; /* success  */
        } while (TRUE);

		// copy modem handle and modem name from structure
        cid = (CommID FAR *) ((LPSTR)vs + vs->dwStringOffset);
        lstrcpy (mytapi.szModemName, &cid->szDeviceName[0]);
		// save modem handle
		mytapi.hComm = cid->hComm;
		// done with structure; free it
		free (vs);

		// set flag to indicate modem handle has been retrieved
		mytapi.bGotcommhandle = TRUE;

		return 0;

} /* end function (GetCommHandle) */

////////////////////////////////////////////////////////////////////
//	mylineGetDevCaps - get LINEDEVCAPS structure
////////////////////////////////////////////////////////////////////
LONG mylineGetDevCaps()
{
	LONG lrc;	
	DWORD dwsize;

	// if space already allocated for structure, free it up
	if (mytapi.bLinedevcapsalloced) {
		free (mytapi.pLinedevcaps);
		mytapi.bLinedevcapsalloced = FALSE;
	}

	// allocate memory for structure
	mytapi.pLinedevcaps = (LINEDEVCAPS *) calloc(1, sizeof(LINEDEVCAPS));
	if (!mytapi.pLinedevcaps)
		return LINEERR_NOMEM;
	mytapi.bLinedevcapsalloced = TRUE;
	// set structure size
	mytapi.pLinedevcaps->dwTotalSize = sizeof(LINEDEVCAPS);
	do {
		// get information into structure
		lrc = lineGetDevCaps(mytapi.hTAPI, mytapi.dwLine, 
							 mytapi.dwVersionToUse,
						     0, mytapi.pLinedevcaps);
		// bomb out if error
		if (lrc) {
			free (mytapi.pLinedevcaps);
			mytapi.bLinedevcapsalloced = FALSE;
			return lrc;
		}
		dwsize = mytapi.pLinedevcaps->dwNeededSize;
		// reallocate and try again
		if (mytapi.pLinedevcaps->dwTotalSize < dwsize) {
			free (mytapi.pLinedevcaps);
			mytapi.bLinedevcapsalloced = FALSE;
			mytapi.pLinedevcaps = (LINEDEVCAPS *) calloc(1, dwsize);
			if (!mytapi.pLinedevcaps)
				return LINEERR_NOMEM;
			mytapi.bLinedevcapsalloced = TRUE;
			mytapi.pLinedevcaps->dwTotalSize = dwsize;
			continue;
		}
		break;
	} while (TRUE);

	return lrc;
}

////////////////////////////////////////////////////////////////////
//	telephonyCaps - mega function to get LINEADDRESSCAPS, 
//		LINETRANSLATECAPS, LINEDEVSTATUS, and LINEADDRESSSTATUS
//		structures.
////////////////////////////////////////////////////////////////////
LONG telephonyCaps()
{
	LONG lrc;	
	DWORD dwsize;
	int i;

	// if space already allocated for structure, free it up
	if (mytapi.bLineaddresscapsalloced) {
		free (mytapi.pLineaddresscaps);
		mytapi.bLineaddresscapsalloced = FALSE;
	}

	// allocate memory for structure
	mytapi.pLineaddresscaps = (LINEADDRESSCAPS *) calloc(1, sizeof(LINEADDRESSCAPS));
	if (!mytapi.pLineaddresscaps)
		return LINEERR_NOMEM;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -