Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

WSASetService function

The WSASetService function registers or removes from the registry a service instance within one or more namespaces.


INT WSASetService(
  _In_ LPWSAQUERYSET    lpqsRegInfo,
  _In_ WSAESETSERVICEOP essOperation,
  _In_ DWORD            dwControlFlags


lpqsRegInfo [in]

A pointer to the service information for registration or deregistration.

essOperation [in]

A value that determines that operation requested. This parameter can be one of the values from the WSAESETSERVICEOP enumeration type defined in the Winsock2.h header file.


Register the service. For SAP, this means sending out a periodic broadcast. This is an NOP for the DNS namespace. For persistent data stores, this means updating the address information.


Remove the service from the registry. For SAP, this means stop sending out the periodic broadcast. This is an NOP for the DNS namespace. For persistent data stores this means deleting address information.


Delete the service from dynamic name and persistent spaces. For services represented by multiple CSADDR_INFO structures (using the SERVICE_MULTIPLE flag), only the specified address will be deleted, and this must match exactly the corresponding CSADDR_INFO structure that was specified when the service was registered.


dwControlFlags [in]

Service install flags value that further controls the operation performed of the WSASetServicefunction. The possible values for this parameter are defined in the Winsock2.h header file.


Controls scope of operation. When this flag is not set, service addresses are managed as a group. A register or removal from the registry invalidates all existing addresses before adding the given address set. When set, the action is only performed on the given address set. A register does not invalidate existing addresses and a removal from the registry only invalidates the given set of addresses.


Return value

The return value for WSASetService is zero if the operation was successful. Otherwise, the value SOCKET_ERROR is returned, and a specific error number can be retrieved by calling WSAGetLastError.

Error codeMeaning

The calling routine does not have sufficient privileges to install the Service.


One or more required parameters were invalid or missing.


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


There was insufficient memory to perform the operation.



The WSASetService function can be used to affect a specific namespace provider, all providers associated with a specific namespace, or all providers across all namespaces.

The available values for essOperation and dwControlFlags combine to control operation of the WSASetService function as shown in the following table.

OperationFlagsService already existsService does not exist
RNRSERVICE_REGISTERNoneOverwrites the object. Uses only addresses specified. The object is REGISTERED.Creates a new object. Uses only addresses specified. Object is REGISTERED.
RNRSERVICE_REGISTERSERVICE_MULTIPLEUpdates the object. Adds new addresses to the existing set. The object is REGISTERED.Creates a new object. Uses all addresses specified. Object is REGISTERED.
RNRSERVICE_DEREGISTERNoneRemoves all addresses, but does not remove the object from the namespace. The object is removed from the registry.WSASERVICE_NOT_FOUND
RNRSERVICE_DEREGISTERSERVICE_MULTIPLEUpdates the object. Removes only addresses that are specified. Only marks the object as DEREGISTERED if no addresses are present. Does not remove the object from the namespace.WSASERVICE_NOT_FOUND
RNRSERVICE_DELETENoneRemoves the object from the namespace.WSASERVICE_NOT_FOUND
RNRSERVICE_DELETESERVICE_MULTIPLERemoves only addresses that are specified. Only removes object from the namespace if no addresses remain.WSASERVICE_NOT_FOUND


Publishing services to directories, such as Active Directory Services, is restricted based on access control lists (ACLs). For more information, see Security Issues for Service Publication.

When the dwControlFlags parameter is set to SERVICE_MULTIPLE, an application can manage its addresses independently. This is useful when the application wants to manage its protocols individually or when the service resides on more than one computer. For instance, when a service uses more than one protocol, it may find that one listening socket aborts but the other sockets remain operational. In this case, the service could remove the aborted address from the registry without affecting the other addresses.

When the dwControlFlags parameter is set to SERVICE_MULTIPLE, an application must not let stale addresses remain in the object. This can happen if the application aborts without issuing a DEREGISTER request. When a service registers, it should store its addresses. On its next invocation, the service should explicitly remove these old stale addresses from the registry before registering new addresses.

Note  If ANSI character strings are used, there is a chance that the WSAQUERYSET data in lpqsRegInfo may not contain any results after this function returns. This is because the ANSI version of this method, WSASetServiceA, converts the ANSI data in WSAQUERYSET to Unicode internally, but does not convert the results back to ANSI. This primarily impacts transports that return a "service record handle" used to uniquely identify a record. To work around this issue, applications should use Unicode string data in WSAQUERYSET when calling this function.

Service Properties

The following table describes how service property data is represented in a WSAQUERYSET structure. Fields labeled as (Optional) can contain a null pointer.

WSAQUERYSET memberService property description
dwSizeMust be set to sizeof (WSAQUERYSET). This is a versioning mechanism.
dwOutputFlagsNot applicable and ignored.
lpszServiceInstanceNameReferenced string contains the service instance name.
lpServiceClassIdThe GUID corresponding to this service class.
lpVersion(Optional) Supplies service instance version number.
lpszComment(Optional) An optional comment string.
dwNameSpaceSee table that follows.
lpNSProviderIdSee table that follows.
lpszContext(Optional) Specifies the starting point of the query in a hierarchical namespace.
dwNumberOfCsAddrsThe number of elements in the array of CSADDR_INFO structures referenced by lpcsaBuffer.
lpcsaBufferA pointer to an array of CSADDR_INFO structures that contain the address(es) that the service is listening on.
lpBlob(Optional) This is a pointer to a provider-specific entity.


As illustrated in the following, the combination of the dwNameSpace and lpNSProviderId members determine that namespace providers are affected by this function.

dwNameSpacelpNSProviderIdScope of impact
IgnoredNon-nullThe specified name-space provider.
A valid name- space identifierNullAll name-space providers that support the indicated namespace.
NS_ALLNullAll name-space providers.


Windows Phone 8: The WSASetServiceW function is supported for Windows Phone Store apps on Windows Phone 8 and later.

Windows 8.1 and Windows Server 2012 R2: The WSASetServiceW function is supported for Windows Store apps on Windows 8.1, Windows Server 2012 R2, and later.


Minimum supported client

Windows 8.1, Windows Vista [desktop apps | Windows Store apps]

Minimum supported server

Windows Server 2003 [desktop apps | Windows Store apps]

Minimum supported phone

Windows Phone 8







Unicode and ANSI names

WSASetServiceW (Unicode) and WSASetServiceA (ANSI)

See also

Bluetooth and WSASetService
Winsock Reference
Winsock Functions



© 2016 Microsoft