dsgetsitename (netapi32)
Last changed: zippy1981@gmail.com-72.231.2.126

.
Summary
Returns the name of the site where a computer resides. For a domain controller (DC), the name of the site is the location of the configured DC. For a member workstation or member server, the name specifies the workstation site as configured in the domain of the computer.

C# Signature:

[DllImport("NetApi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern UInt32 DsGetSiteName([MarshalAs(UnmanagedType.LPTStr)]string ComputerName, out IntPtr SiteNameBuffer);

VB Signature:

    <DllImport("NetApi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function DsGetSiteName( _
    <MarshalAs(UnmanagedType.LPTStr)> ByVal ComputerName As String, _
    <Out()> ByRef SiteNameBuffer As IntPtr) As UInt32
    End Function

User-Defined Types:

None.

Alternative Managed API:

None

Notes:

The DsGetSiteName function does not require any particular access to the specified domain. The function is sent to the Netlogon service on the computer specified by ComputerName.

Return codes:

ERROR_NO_SITENAME The computer is not in a site.

ERROR_NOT_ENOUGH_MEMORY Insufficient memory is available.

Tips & Tricks:

Please add some!

Sample Code:

IntPtr pBuffer = IntPtr.Zero;

UInt32 result = DsGetSiteName(ComputerName, out pBuffer);

if (result == 0)

{

    String sSiteName = Marshal.PtrToStringAuto(pBuffer);
    NetApiBufferFree(pBuffer);

}

VB.NET Version:

    Dim pBuffer As IntPtr = IntPtr.Zero
    Dim result As UInt32 = DsGetSiteName("MEASUS", pBuffer)
    If result.ToString = "0" Then
        Dim SiteName As String = Marshal.PtrToStringAuto(pBuffer)
        NetApiBufferFree(pBuffer)
        Console.WriteLine(SiteName)
    End If

Documentation