locked
UserDomainName - Domain or Workgroup?

    Question

  •  

    Hi all,

    I've used System.Environment.UserDomainName to find out what domain/workgroup the user is on, but how do I know if its actually a domain or workgroup that they've logged onto. I want to be able to do stuff if they're not logged into a domain.

    Thanks in advance...

    Scott

     

    Monday, August 07, 2006 11:35 AM

Answers

  • There may be a more official way but I found this:

    http://www.devx.com/tips/Tip/26562?type=kbArticle&trk=MSCP

    Basically you attempt to enumerate the DC name for the "domain" part of the user name, if it pops back with a name then you are in a domain, empty then workgroup.

    Not tried it and seems a bit untidy but may work.

    Had a bit of a play, do not have a domain to test it on but lots of errors in the declaration, looked on MSDN for it and changed it to the code below which give a response of nothing to me, possibly as I am a workgroup not domain.

    Private Declare Function NetGetDCName Lib "netapi32.dll" (ByVal strServerName As String, ByVal strDomainName As String, ByVal pBuffer As String) As Long

       Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal buffer As String) As Long

       Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal dest As String, ByVal source As String, ByVal bytes As Long)

       ' Return the name of the Primary Domain Controller (PDC)
       '
       ' server names are preceded by a double slash

       Public Shared Function GetPDCName() As String
          Dim lpBuffer As Long
          Dim buffer As String = ""

          If NetGetDCName(vbNullString, vbNullString, buffer) = 0 Then
             ' a zero return value means success
             buffer = Space$(128)
             ' copy into local buffer
             CopyMemory(buffer, lpBuffer, buffer.Length)

             ' trim excess chars
             GetPDCName = Left$(buffer, InStr(buffer & vbNullChar, vbNullChar) - 1)
             ' release memory
             NetApiBufferFree(lpBuffer)
          End If

       End Function

    Monday, August 07, 2006 12:15 PM

All replies

  • There may be a more official way but I found this:

    http://www.devx.com/tips/Tip/26562?type=kbArticle&trk=MSCP

    Basically you attempt to enumerate the DC name for the "domain" part of the user name, if it pops back with a name then you are in a domain, empty then workgroup.

    Not tried it and seems a bit untidy but may work.

    Had a bit of a play, do not have a domain to test it on but lots of errors in the declaration, looked on MSDN for it and changed it to the code below which give a response of nothing to me, possibly as I am a workgroup not domain.

    Private Declare Function NetGetDCName Lib "netapi32.dll" (ByVal strServerName As String, ByVal strDomainName As String, ByVal pBuffer As String) As Long

       Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal buffer As String) As Long

       Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal dest As String, ByVal source As String, ByVal bytes As Long)

       ' Return the name of the Primary Domain Controller (PDC)
       '
       ' server names are preceded by a double slash

       Public Shared Function GetPDCName() As String
          Dim lpBuffer As Long
          Dim buffer As String = ""

          If NetGetDCName(vbNullString, vbNullString, buffer) = 0 Then
             ' a zero return value means success
             buffer = Space$(128)
             ' copy into local buffer
             CopyMemory(buffer, lpBuffer, buffer.Length)

             ' trim excess chars
             GetPDCName = Left$(buffer, InStr(buffer & vbNullChar, vbNullChar) - 1)
             ' release memory
             NetApiBufferFree(lpBuffer)
          End If

       End Function

    Monday, August 07, 2006 12:15 PM
  •  

    Thanks for that Rabtok, I'll give that method a go. It looks like it will do just what I want.

    unless of course someone knows of a "proper" microsoft way of doing it :-)
    I was expecting a "IsDomain" property somewhere in the class.

    Scott

     

    Monday, August 07, 2006 4:10 PM