title: How to get Windows domain name?
body: <p>How can i get the domain name of the machine (if the machine is in fact joined to a domain)?</p>
  
  <p>And, of course, if the machine is not joined to a domain the function should return </p>
  
  <ul>
  <li><code>null</code>, or </li>
  <li>an empty string, or </li>
  <li>the name of the machine, or </li>
  <li><code>"."</code></li>
  </ul>
  
  <p><strong>Notes</strong>: </p>
  
  <ul>
  <li><p>the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa370423%28v=vs.85%29.aspx" rel="noreferrer"><code>NetGetJoinInformation</code></a> Win32 function returns the legacy NetBIOS name of the domain (e.g. <code>AVATOPIA</code>), not the name of the domain (e.g. <code>avatopia.local</code>)</p></li>
  <li><p>the <code>USERDOMAIN</code> environment variable returns the domain of the logged on user, which can be different from the machine; and also returns the legacy NetBIOS name of the domain (e.g. <code>AVATOPIA</code>)</p></li>
  <li><p>the <code>USERDNSDOMAIN</code> environment variable returns the domain name of the logged on user, which can be different from the machine</p></li>
  </ul>
  
  <p>Microsoft has a knowledge base article <a href="http://support.microsoft.com/kb/111544" rel="noreferrer">How to retrieve current user and domain names on Windows NT, Windows 2000, or Windows XP</a>, which relies on getting the user's security token and calling LookupAccountSid.</p>
  
  <ul>
  <li>the LookupAccountSid Win32 function returns the legacy NetBIOS name of the domain (e.g. <code>AVATOPIA</code>); and also returns the domain of the logged on user, which can be different from the machine</li>
  </ul>
  
  <h2>Update One</h2>
  
  <p>i've also tried using ADs object to bind to the <code>IADs</code> interface of the domain:</p>
  
  <pre><code>IADs domain;
  ADsGetObject("LDAP://rootDES", IDs, out domain);
  </code></pre>
  
  <p>problem with this approach is that:</p>
  
  <ul>
  <li>you cannot get the domain name (only the <em>distinguished name</em>)</li>
  <li>it doesn't work if the user does not have permissions to query AD</li>
  <li>it doesn't work if the user is not a valid user in active directory</li>
  <li>it only works for Active Directory domains</li>
  </ul>
  
  <p><strong>Update Two</strong>:</p>
  
  <p>Just to be clear what i want is:</p>
  
  <p><img src="https://i.stack.imgur.com/OCbkR.png" alt="enter image description here"></p>
  
  <ul>
  <li>which radio button is selected</li>
  <li><a href="http://4.bp.blogspot.com/-_WOWYdYK4Tg/TlPERL1jhvI/AAAAAAAABIQ/CUXUCTRU4lc/s400/se7en_movie_still.jpg" rel="noreferrer">what's in the (selected) box</a></li>
  </ul>
  
  <h2>Bonus Reading</h2>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8446940/how-to-get-fully-qualified-domain-name-on-windows-in-delphi">How to get fully qualified domain name on windows in Delphi</a> (user's domain)</li>
  <li><a href="https://stackoverflow.com/questions/508911/machines-domain-name-in-net">Machine's domain name in .NET?</a> (from .NET)</li>
  </ul>