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

📄 tapiconn.cpp

📁 基于Tapi 3.0的软电话源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//------------------------------------------------------------------------------------
//  Copyright (c) 2002 Avaya Global SME Solutions 
//------------------------------------------------------------------------------------
//  Project name: TAPI 3 Test Harness
//  Module file : CTAPIConn.cpp
//  Compiler    : Visual C++ 6.0
//------------------------------------------------------------------------------------
//  Description : In this module CTAPIConn class is implemented. It implements 
//                initialization/deinitialization of TAPI 3, opening of all addresses,
//                registration for call notification. It realizes also some static 
//                functions for getting different sort of information from ITCallInfo
//                and for getting string descriptions of some TAPI3 constants.  
//------------------------------------------------------------------------------------

#include "stdafx.h"
#include "tapi3app.h"
#include "TAPIConn.h"

// Specified template function for TAPI_LINE_MAP
UINT __stdcall HashKey<CString&>(CString& key)
{
	return 0;
}

void CTAPIConn::GetDialableAddress(ITCallInfo *pInfo, CString& sAddr)
{
	ITAddress* pITAddress=NULL;
	HRESULT hr;

	sAddr = "";
	if (pInfo)
	{
		hr = pInfo->get_Address(&pITAddress);
		if (hr == S_OK)
		{
			BSTR bstrAddr;

			hr = pITAddress->get_DialableAddress(&bstrAddr);
			if (hr == S_OK)
			{
				sAddr = bstrAddr;
				SysFreeString(bstrAddr);
			}			
			pITAddress->Release();
		}
	}
}

void CTAPIConn::GetCalledAddress(ITCallInfo* pInfo, CString& sAddr)
{
	BSTR bstrCalledId;
	HRESULT hr;

	sAddr = "";
	if (pInfo)
	{
		hr = pInfo->get_CallInfoString(CIS_CALLEDIDNUMBER, &bstrCalledId);
		if (hr == S_OK)
		{
			sAddr = bstrCalledId;
			SysFreeString(bstrCalledId);
		}
	}
}	

void CTAPIConn::GetCallerAddress(ITCallInfo* pInfo, CString& sAddr)
{
	HRESULT hr;
	BSTR bstrCallerId;

	sAddr = "";
	if (pInfo)
	{
		hr = pInfo->get_CallInfoString(CIS_CALLERIDNUMBER, &bstrCallerId);
		if (hr == S_OK)
		{
			sAddr = bstrCallerId;
			SysFreeString(bstrCallerId);
		}
	}
}

void CTAPIConn::GetAddressName(ITCallInfo *pInfo, CString& sAddr)
{
	ITAddress* pITAddress=NULL;
	HRESULT hr;
    BSTR address;

	sAddr = "";
	if (pInfo)
	{
        hr = pInfo->get_Address(&pITAddress);
        if (hr == S_OK)
        {
            hr = pITAddress->get_AddressName(&address);			    
			if (hr == S_OK)    
			{    
                sAddr = address;    
			}			    
            SysFreeString(address);
			pITAddress->Release();    		    
        }
	}
}

void CTAPIConn::GetRedirectionAddress(ITCallInfo* pInfo, CString& sAddr)
{
	HRESULT hr;
	BSTR bstrCallerId;

	sAddr = "";
	if (pInfo)
	{
		hr = pInfo->get_CallInfoString(CIS_REDIRECTIONIDNUMBER, &bstrCallerId);
		if (hr == S_OK)
		{
			sAddr = bstrCallerId;
			SysFreeString(bstrCallerId);
		}
	}
}

void CTAPIConn::GetRedirectingAddress(ITCallInfo* pInfo, CString& sAddr)
{
	HRESULT hr;
	BSTR bstrCallerId;

	sAddr = "";
	if (pInfo)
	{
		hr = pInfo->get_CallInfoString(CIS_REDIRECTINGIDNUMBER, &bstrCallerId);
		if (hr == S_OK)
		{
			sAddr = bstrCallerId;
			SysFreeString(bstrCallerId);
		}
	}
}

HANDLE CTAPIConn::m_hEventHandle = NULL; 
HLINEAPP CTAPIConn::m_hLineApp = NULL;
static CTAPIConn* g_pTAPIConn = NULL;

// Construction/Destruction
CTAPIConn::CTAPIConn()
{
	g_pTAPIConn = this;
	m_hLineApp = NULL;

	m_dwNumDevs = 0;
	m_bInit = false;

	m_pTAPI3 = NULL;
	m_pTAPIEventNotify = NULL;
}

CTAPIConn::~CTAPIConn()
{

}

inline HLINEAPP GetLineApp() 
{ 
	return g_pTAPIConn->m_hLineApp; 
};

bool CTAPIConn::Init()
{
	bool bRet = false;
	IConnectionPointContainer* pCPC = NULL;
	IConnectionPoint* pCP = NULL;
	HRESULT hr;

	// If already initialised return
	if (m_pTAPI3) return true;

	// Start the TAPI service provider
	hr = CoCreateInstance(CLSID_TAPI, NULL, CLSCTX_INPROC_SERVER, IID_ITTAPI, (LPVOID*)&m_pTAPI3);
	if (SUCCEEDED(hr))
	{
		// Initialise the TAPI service provider
		hr = m_pTAPI3->Initialize();
		if (SUCCEEDED(hr))
		{
			// Register the TAPI event interface
			// Get the interface to the connection poinr container
			hr = m_pTAPI3->QueryInterface(IID_IConnectionPointContainer, (LPVOID*)&pCPC);
			if (SUCCEEDED(hr))
			{
				// Locate the TAPI notification connection point
				hr = pCPC->FindConnectionPoint(IID_ITTAPIEventNotification, &pCP);
				if (SUCCEEDED(hr))
				{
					// Create an instance of the notification object
					m_pTAPIEventNotify = new CTAPIEventNotification(GetCurrentThreadId());

					// Connect the notification object to the connection point
					hr = pCP->Advise(m_pTAPIEventNotify, &m_ulAdviseCookie);
					if (SUCCEEDED(hr))
					{
						// OK TAPI evens will now come through to my notification object
						bRet = true;

						// TAPI events that I am interested in
						hr = m_pTAPI3->put_EventFilter(0x1FFFF);
						if (SUCCEEDED(hr))
						{
							PRINT_INIT(-1, 0, "TAPI3 successfully initiated, opening all lines...");
							OpenAllAddresses();
						}
						else
						{
							PRINT_INIT(-1, 0, "Failed to set event filter, hr=%x", hr);
						}
					}
					else
					{
						PRINT_INIT(-1, 0, "Failed to connect to connection point, hr=%x", hr);
					}
				}
				else
				{
					PRINT_INIT(-1, 0, "Failed to locate IID_ITTAPIEventNotification connection point, hr=%x.", hr);
				}
			}
			else
			{
				PRINT_INIT(-1, 0, "Failed to retrieve interface IID_IConnectionPointContainer, hr=%x", hr);
			}
		}
		else
		{
			PRINT_INIT(-1, 0, "Failed to initialise TAPI3, hr=%x", hr);
		}
	}
	else
	{
		PRINT_INIT(-1, 0, "Failed to start TAPI3 service provider, hr=%x", hr);
	}

	// TAPI init failed, release the TAPI3 interface
	if (!bRet)
	{
		if (m_pTAPI3 != NULL) m_pTAPI3->Release();
		m_pTAPI3 = NULL;
	}

	// Always release these interfaces
	if (pCP != NULL) pCP->Release();
	if (pCPC != NULL) pCPC->Release();

	return bRet;
}

bool CTAPIConn::DeInit()
{
	// If TAPI has been initialised, close it.
	if (m_pTAPI3)
	{
		HRESULT hr;
		IConnectionPointContainer *pCPC = NULL;
		IConnectionPoint *pCP = NULL;

		// Close all the address
		CTAPIAddr::ReleaseAddrMap();

		// Do I need to unadvise??
		if (m_ulAdviseCookie)
		{
			// Get interface pointer to connection point container
			hr = m_pTAPI3->QueryInterface(IID_IConnectionPointContainer, (LPVOID*)&pCPC);
			if (SUCCEEDED(hr))
			{
				// Locate the TAPI event notification 
				hr = pCPC->FindConnectionPoint(IID_ITTAPIEventNotification, &pCP);
				if (SUCCEEDED(hr))
				{
					// Unadvise
					pCP->Unadvise(m_ulAdviseCookie);
				}
			}

			if (pCP) pCP->Release();
			if (pCPC) pCPC->Release();
		}

		// Shutdown TAPI3
		m_pTAPI3->Shutdown();
		m_pTAPI3->Release();
		m_pTAPI3 = NULL;
	}
	return true;
}

long CTAPIConn::OpenAllAddresses()
{
	HRESULT hr;
	IEnumAddress *pEnumAddress;
	ITAddress *pAddress;
	ITMediaSupport *pMediaSupport;

	// Get the enumerator for the addresses
	hr = m_pTAPI3->EnumerateAddresses(&pEnumAddress);
	if (hr == S_OK)
	{
		while (true)
		{
			long lMediaType;
			BSTR Addr, DialAddr, Provider;

			// Get next address from enumerator
			hr = pEnumAddress->Next(1, &pAddress, NULL);
			if (hr == S_OK)
			{
				CString sMediaType;
				Addr = NULL;
				DialAddr = NULL;
				Provider = NULL;
				lMediaType = 0;
				
				// Address display name
				pAddress->get_AddressName(&Addr);

				// Dialable address
				pAddress->get_DialableAddress(&DialAddr);

				// Service provider name
				pAddress->get_ServiceProviderName(&Provider);

				// Get the interface to retrieve supported media
				hr = pAddress->QueryInterface(IID_ITMediaSupport, (LPVOID*)&pMediaSupport);
				if (hr == S_OK)
				{	
					// Get the type of media supported on this address
					hr = pMediaSupport->get_MediaTypes(&lMediaType);
					if (hr == S_OK)
					{
						GetASCIIMediaType(lMediaType, sMediaType);
					}
					pMediaSupport->Release();
				}
				
				PRINT_INIT(-1, 0, "Addr=%s(%s), provider=%s, supported media=%s", 
									CString(Addr), CString(DialAddr), CString(Provider), CString(sMediaType));

				// If the address supports audio, add it to my map
				if (lMediaType & TAPIMEDIATYPE_AUDIO &&
					SysStringLen(Addr) > 0 &&
					SysStringLen(DialAddr) > 0)
				{

⌨️ 快捷键说明

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