DsAddressToSiteNames (netapi32)
Last changed: -

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



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("domaincontroller.mydomain.com", 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;