Share via


Parsing an SDP Record Using COM Interfaces (Windows Embedded CE 6.0)

1/6/2010

Windows Embedded CE provides COM interfaces that you can use to parse SDP records as streams. ISdpStream defines methods that you can use to manage streamed data. Before your application retrieves records, use the ISdpStream::Validate method to ensure that the raw SDP stream is valid and well formed. The application can also use the ISdpStream::VerifySequenceOf method to ensure that the elements of the raw SDP array are valid. To retrieve the record from the stream, use the ISdpStream::RetrieveRecords method. This method returns the SDP record in a ISdpRecord array and a count of the number of records parsed. You can then use the ISdpRecord methods to perform SDP record operations such as retrieving the attribute of the SDP service record. For information about retrieving SDP record attributes see Searching SDP Attributes Using COM Interfaces.

The following example code shows how to implement a function to parse an SDP record, in binary format, by using ISdpStream methods, and return the records as an ISdpRecord array.

STDMETHODIMP
ServiceAndAttributeSearchParse(
  UCHAR *szResponse,   // in - pointer to buffer representing 
                       // the SDP record, in binary format, 
                       // returned by the Bthnscreate tool.
  DWORD cbResponse,   // in - size of szResponse 
  ISdpRecord ***pppSdpRecords, // out - array of pSdpRecords
  ULONG *pNumRecords   // out - number of elements in pSdpRecords array
)
{

  HRESULT hres = E_FAIL;
  *pppSdpRecords = NULL;
  *pNumRecords = 0;
  ISdpStream *pIStream = NULL;
// Create a stream object.
if (FAILED(CoCreateInstance(__uuidof(SdpStream),NULL,CLSCTX_INPROC_SERVER,
                        __uuidof(ISdpStream),(LPVOID *) &pIStream))) 
{
  return E_FAIL;
}
// Ensure that the stream is valid and is well formed.
  hres = pIStream->Validate(szResponse,cbResponse,NULL);

  if (SUCCEEDED(hres)) 
  {
  // Ensure that the sequence of the stream is valid and is well formed.
    hres = pIStream->VerifySequenceOf(szResponse,cbResponse,
                       SDP_TYPE_SEQUENCE,NULL,pNumRecords);
    if (SUCCEEDED(hres) && *pNumRecords > 0) 
    {
      *pppSdpRecords = (ISdpRecord **) 
      //Allocate memory for the SDP record buffer.
      CoTaskMemAlloc(sizeof(ISdpRecord*) * (*pNumRecords));
      if (pppSdpRecords != NULL) 
      {
         // Retrieve the SDP records from the stream.
         hres = pIStream->RetrieveRecords ( szResponse, 
                    cbResponse,*pppSdpRecords,pNumRecords);
        //If retrieval of records from the stream failed,
        // free memory allocated to the SDP record array and set the 
        // SDP record count to zero (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;
}

See Also

Other Resources

Bluetooth Application Development