Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
DsAddressToSiteNames (netapi32)
The DsAddressToSiteNames function obtains the site names corresponding to the specified addresses.

C# Signature:

[DllImport("netapi32.dll", CharSet=CharSet.Auto)]
public static extern int DsAddressToSiteNames(string computerName, int entryCount, SOCKET_ADDRESS[] socketAddresses, ref IntPtr siteNames);

VB Signature:

Declare Function DsAddressToSiteNames Lib "netapi32.dll" (TODO) As TODO


  • computerName: String that specifies the name of the remote server to process this function. This parameter must be the name of a domain controller. A non-domain controller can call this function by calling DsGetDcName to find the domain controller.
  • entryCount: The number of elements in the socketAddresses array.
  • socketAddresses: An array of SOCKET_ADDRESS structures that contain the addresses to convert. Each address in this array must be of the type AF_INET. EntryCount contains the number of elements in this array.
  • siteNames: An array of null-terminated string pointers that contain the site names for the addresses. Each element in this array corresponds to the same element in the socketAddresses array. An element is NULL if the corresponding address does not map to any known site or if the address entry is not of the proper form. The caller must free this array when it is no longer required by calling NetApiBufferFree.


Prior to calling he DsAddressToSiteNames function, a SOCKET_ADDRESS structure must be created for each address that is to be resolved to a site name. This can be done using the WSAStringToAddress method shown in the sample code.

Tips & Tricks:

Please add some!

C# Sample Code:

public static string GetSiteNameForAddress(string address)
    if (string.IsNullOrEmpty(address))
         throw new ArgumentNullException("address");

    WSADATA data = new WSADATA();
    SockAddr sockAddr = new SockAddr();
    IntPtr pSockAddr = IntPtr.Zero;
    IntPtr pSites = IntPtr.Zero;
    SOCKET_ADDRESS[] SocketAddresses = new SOCKET_ADDRESS[1];
    string siteName = string.Empty;

    if (WSAStartup(0x201, ref data) == ERROR_SUCCESS)
        int sockAddrSize = Marshal.SizeOf(sockAddr);

        // Call into WSAStringToAddress to build SOCKET_ADDRESS structure from the address string
        int result = WSAStringToAddress(
            ref sockAddr,
            ref sockAddrSize);


        // Check for failure from the WSAStringToAddress method
        if (result != ERROR_SUCCESS)
           throw new Win32Exception(result);

        // Allocate memory on the heap for the SockAddr structure
        pSockAddr = Marshal.AllocHGlobal(Marshal.SizeOf(sockAddr));
        Marshal.StructureToPtr(sockAddr, pSockAddr, true);

        // Fill in the appropriate fields
        SocketAddresses[0].lpSockaddr = pSockAddr;
        SocketAddresses[0].iSockaddrLength = Marshal.SizeOf(sockAddr);

        // Get the site name for this address
        result = DsAddressToSiteNames("", 1, SocketAddresses, ref pSites);

        if(result != ERROR_SUCCESS)
            throw new Win32Exception(result);

        // Read the string out of memory
        IntPtr pSiteName = Marshal.ReadIntPtr(pSites, 0);

        // If a site could not be found for this address, pSiteName will be 0, so only marshal out the string if
        // we got back a valid pointer.
        if (pSiteName != IntPtr.Zero)
            siteName = Marshal.PtrToStringAuto(pSiteName);

        // Be sure to free the memory allocated

    return siteName;


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version