다음을 통해 공유


Enumerating Clouds (Windows CE 5.0)

Send Feedback

When it enumerates clouds, an application must provide the scope of the search for clouds. After the scope is identified, the application can begin the enumeration process. Peer Name Resolution Protocol (PNRP) uses Winsock functions to resolve peer names.

To enumerate clouds

  1. Start the enumeration resolution process by calling the WSALookupServiceBegin (Windows Sockets) function.

    For more information about values to pass in WSALookupServiceBegin parameters, see WSALookupServiceBegin Parameter Values for Enumerating Clouds.

  2. To match the queries specified in the previous call to WSALookupServiceBegin, call the WSALookupServiceNext (Windows Sockets) function. The results of the WSALookupServiceNext function are determined by settings in the WSAQUERYSET structure passed in the initial WSALookupServiceBegin function call.

    For more information about values to pass in WSALookupServiceBegin parameters, see WSALookupServiceNext Parameter Values for Enumerating Clouds.

  3. To complete the enumeration process, call the WSALookupServiceEnd (Windows Sockets) function. Pass the handle obtained from the previous call to WSALookupServiceBegin.

    A call to WSALookupServiceEnd terminates the cloud enumeration process.

The following code example shows how to enumerate and print the names of available link-local clouds.

//------------------------------------------------------------------------
//Function: PrintLinkLocalClouds
//Purpose:  Enumerate and print the names of available link-local clouds.
//Returns:  DWORD
//------------------------------------------------------------------------
DWORD PrintLinkLocalClouds()
{
  WSAQUERYSETW    qset;
  BLOB            Blob;
  PNRPCLOUDINFO   CloudInfo;
  HANDLE          hLookup = NULL;
  int             err;
  DWORD           dwErr = NO_ERROR;
  DWORD           dwSize;
  WSAQUERYSETW    *pResults = NULL;

  ZeroMemory(&qset, sizeof(qset));
  ZeroMemory(&CloudInfo, sizeof(CloudInfo));

  CloudInfo.dwSize = sizeof(PNRPCLOUDINFO);
  CloudInfo.Cloud.Scope = PNRP_LINK_LOCAL_SCOPE;

  Blob.cbSize = sizeof(PNRPCLOUDINFO);
  Blob.pBlobData = (LPBYTE)&CloudInfo;

  qset.dwSize = sizeof(WSAQUERYSET);
  qset.dwNameSpace = NS_PNRPCLOUD;
  qset.lpServiceClassId = (LPGUID)&SVCID_PNRPCLOUD;
  qset.lpBlob = &Blob;

  // Start enumeration.
  err = WSALookupServiceBegin(&qset, LUP_RETURN_NAME, &hLookup);
  if(err !=0)
  {
    return WSAGetLastError();
  }
  // Getting results.
  while(TRUE)
  {
    // Get size.
    ZeroMemory(&qset, sizeof(qset));
    dwSize = sizeof(qset);
    pResults = &qset;
    err = WSALookupServiceNext(hLookup, 0, &dwSize, pResults);
    if(err != 0)
    {
      dwErr = WSAGetLastError();
    }
    if(dwErr != NO_ERROR)
    {
      if(dwErr == WSA_E_NO_MORE)
      {
        // No more entries.
        dwErr = ERROR_SUCCESS;
        break;
      }
      else if (dwErr == WSAEFAULT)
      {
        // This usually means result buffer too small. Allocate space
        pResults = (WSAQUERYSET *)malloc(dwSize);
        if(pResults == NULL)
        {
          dwErr = ERROR_OUTOFMEMORY;
          break;
        }
        // Get cloud name.
        err = WSALookupServiceNext(hLookup, 0, &dwSize,pResults);
        if(err == 0)
        {
          wprintf(L"%s\n", pResults->lpszServiceInstanceName);
          dwErr = NO_ERROR;
        }
        else
        {
          dwErr = WSAGetLastError();
        }
        free(pResults);
        if(dwErr != NO_ERROR)
        {
          break;
        }
      }
      else
      {
        // Some other unexpected error.
        break;
      }
    }
    else
    {
      // Should never happen.
      dwErr = ERROR_GEN_FAILURE;
      break;
    }
  }
  // Close the enumeration.
  WSALookupServiceEnd(hLookup);
  return dwErr;

}

See Also

Peer-to-Peer Application Development

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.