WSALookupServiceBegin (Windows Sockets)

A version of this page is also available for

Windows Embedded CE 6.0 R3


This function initiates a client query that is constrained by the information contained within a WSAQUERYSET (Windows Sockets) structure. This function only returns a handle, which should be used by subsequent calls to the WSALookupServiceNext (Windows Sockets) function to get the actual results.


INT WSALookupServiceBegin(
  LPWSAQUERYSET lpqsRestrictions,
  DWORD dwControlFlags,
  LPHANDLE lphLookup


  • lpqsRestrictions
    [in] Pointer to the search criteria.
  • dwControlFlags
    [in] Flag that controls the depth of the search. The following table shows a list of flags for the dwControlFlags parameter.

    Flag Description


    Queries deep as opposed to just the first level.


    Does not return any containers.


    If possible, returns results in the order of distance. The measure of distance is provider specific.


    Indicates whether prime response is in the remote or local part of the CSADDR_INFO structure. The other part needs to be usable in either case.


    Any available alias information is to be returned in successive calls to WSALookupServiceNext (Windows Sockets), and each alias returned will have the RESULT_IS_ALIAS flag set.


    Retrieves the name as lpszServiceInstanceName.


    Retrieves the type as lpServiceClassId.


    Retrieves the version as lpVersion.


    Retrieves the comment as lpszComment.


    Retrieves the addresses as lpcsaBuffer.


    Retrieves the private data as lpBlob.


    Retrieves all of the information.

Return Value

The return value is zero if the operation was successful. If an error occurs, the value SOCKET_ERROR is returned, and a specific error number can be retrieved by calling the WSAGetLastError function.

The following table shows a list of possible error codes.

Error code Description


One or more parameters were missing or invalid for this provider.


The name was found in the database but no data matching the given restrictions was located.


Ws2.dll has not been initialized. The application must first call the WSAStartup function before calling any Windows Sockets functions.


No such service is known. The service cannot be found in the specified name space.


There was insufficient memory to perform the operation.


The restriction parameters specified in lpqsRestrictions or the handle returned in lphLookup by WSALookupServiceBegin are not valid.


If LUP_CONTAINERS is specified in a call, all other restriction values should be avoided. If any are supplied, it is up to the name service provider to decide if it can support this restriction over the containers. If it cannot, it should return an error.

Some name service providers can have other means of finding containers. For example, containers might all be of some well-known type, or of a set of well-known types, and therefore a query restriction can be created for finding them. No matter what other means the name service provider has for locating containers, LUP_CONTAINERS and LUP_NOCONTAINERS take precedence. Hence, if a query restriction is given that includes containers, specifying LUP_NOCONTAINERS will prevent the container items from being returned. Similarly, no matter the query restriction, if LUP_CONTAINERS is given, only containers should be returned. If a name space does not support containers, and LUP_CONTAINERS is specified, it should simply return WSANO_DATA.


The built-in DNS name space provider does not accept the LUP_CONTAINERS flag. Specifying the LUP_CONTAINERS flag in a call to WSALookupServiceBegin will result in a WSANO_DATA error.

The following code sample is the preferred method of obtaining the containers within another container.

dwStatus = WSALookupServiceBegin(

This call is followed by the requisite number of WSALookupServiceNext (Windows Sockets) calls. This will return all containers contained immediately within the starting context; that is, it is not a deep query. With this, one can map the address space structure by walking the hierarchy, perhaps enumerating the content of selected containers. Subsequent uses of WSALookupServiceBegin use the containers returned from a previous call.

As mentioned above, a WSAQUERYSET (Windows Sockets) structure is used as an input parameter to WSALookupBegin to qualify the query. The following table indicates how the WSAQUERYSET (Windows Sockets) structure is used to construct a query. When a parameter is marked as (Optional), a NULL pointer can be supplied, indicating that the parameter will not be used as a search criteria.

Member Query interpretation


Must be set to sizeof(WSAQUERYSET). This is a versioning mechanism.


Ignored for queries.


(Optional) The referenced string contains service name. The semantics for wildcarding within the string are not defined, but it can be supported by certain name space providers.


(Required) The globally unique identifier (GUID) corresponding to the service class.


(Optional) References the desired version number and provides version comparison semantics (that is, the version must match exactly or the version must be not less than the value supplied).


Ignored for queries.


The identifier of a single name space in which to constrain the search, or NS_ALL to include all name spaces.


(Optional) References the GUID of a specific name space provider and limits the query to this provider only.


(Optional) Specifies the starting point of the query in a hierarchical name space.


The size of the protocol constraint array, which can be zero.


(Optional) References an array of the AFPROTOCOLS structure. Only services that use these protocols will be returned.


(Optional) Some name spaces (such as whois++) support enriched SQL-like queries that are contained in a simple text string. This parameter is used to specify that string.


Ignored for queries.


Ignored for queries.


(Optional) This is a pointer to a provider-specific entity.


In most instances, applications interested in only a particular transport protocol should constrain their query by address family and protocol rather than by name space. This would allow an application that needs to locate a TCP/IP service, for example, to have its query processed by all available name spaces such as the local hosts file, DNS, and NIS.

Notes for Bluetooth

Bluetooth clients use WSALookupServiceBegin (Windows Sockets), WSALookupServiceNext (Windows Sockets), and WSALookupServiceEnd to discover the existence of a particular service on the Bluetooth RFCOMM server. Queries can be done for local and remote addresses, although for RFCOMM a client can connect only to remote addresses.


Header winsock2.h
Library Ws2.lib
Windows Embedded CE Windows CE .NET 4.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also


WSALookupServiceEnd (Windows Sockets)
WSALookupServiceNext (Windows Sockets)