STDMETHODIMP
ServiceAndAttributeSearchParse(
UCHAR *szResponse,
// in –
指向SDP记录的缓冲区,二进制格式
//
由Bthnscreate示例获得
DWORD cbResponse,
// in – szResponse
的大小
ISdpRecord ***pppSdpRecords,
// out – pSdpRecords
的数组
ULONG *pNumRecords
// out – pSdpRecords
数组中的元素个数
)
{
HRESULT hres = E_FAIL;
*pppSdpRecords = NULL;
*pNumRecords = 0;
ISdpStream *pIStream = NULL;
//
创建一个流对象
if (FAILED(CoCreateInstance(__uuidof(SdpStream),NULL,CLSCTX_INPROC_SERVER,
__uuidof(ISdpStream),(LPVOID *) &pIStream)))
{
return E_FAIL;
}
//
确保SDP流是有效的并且格式正确
hres = pIStream->Validate(szResponse,cbResponse,NULL);
if (SUCCEEDED(hres))
{
//
确保SDP流的序列是有效的并且格式正确
hres = pIStream->VerifySequenceOf(szResponse,cbResponse,
SDP_TYPE_SEQUENCE,NULL,pNumRecords);
if (SUCCEEDED(hres) && *pNumRecords > 0)
{
*pppSdpRecords = (ISdpRecord **)
//
为SDP记录缓冲区分配内存空间
CoTaskMemAlloc(sizeof(ISdpRecord*) * (*pNumRecords));
if (pppSdpRecords != NULL)
{
//
从流中获取SDP记录
hres = pIStream->RetrieveRecords ( szResponse,
cbResponse,*pppSdpRecords,pNumRecords);
//
如果获取记录失败,释放已分配的内存,设SDP记录总数为0
if (!SUCCEEDED(hres))
{
CoTaskMemFree(*pppSdpRecords);
*pppSdpRecords = NULL;
*pNumRecords = 0;
}
}
else
{
hres = E_OUTOFMEMORY;
}
}
}
// Release the stream.
if (pIStream != NULL)
{
pIStream->Release();
pIStream = NULL;
}
return hres;
}