Visual Basic Network Services
NetGetJoinInformation: Join Information for a Local or Remote Machine
Posted:   Sunday July 07, 2002
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   Windows 2000, Windows XP or later
Author:   VBnet - Randy Birch
Windows 2000 or Windows XP or later.

NetGetJoinInformation is part of the network management directory service functions provided to allow developers to work with the domain controller and domain membership in the directory service. NetGetJoinInformation is used to obtain the join status information of a specified computer.

The valid return values are:

  NetSetupUnknownStatus   The status is unknown.
  NetSetupUnjoined   The computer is not joined.
  NetSetupWorkgroupName   The computer is joined to a workgroup.
  NetSetupDomainName   The computer is joined to a domain

No special group membership is required to successfully execute the NetGetJoinInformation function.

 BAS Module Code

 Form Code
To a form add a command button (Command1) and two text boxes (Text1, Text2). Labels are optional. Add the following to the form:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const NERR_SUCCESS As Long = 0&
Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Declare Function NetGetJoinInformation Lib "Netapi32" _
   (ByVal lpServer As Long, _
    lpNameBuffer As Long, _
    BufferType As Long) As Long

Private Declare Function NetApiBufferFree Lib "netapi32.dll" _
   (ByVal Buffer As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (pTo As Any, uFrom As Any, _
   ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" _
  (ByVal lpString As Long) As Long

Private Sub Form_Load()

   Command1.Caption = "NetGetJoinInformation"

End Sub

Private Sub Command1_Click()

   Dim bufptr          As Long
   Dim dwServer        As Long
   Dim dwBufferType    As Long
   Dim sServer         As String
  'This retrieves the join info for the 
  'local machine. You can also pass the 
  'name of a remote machine instead. 
   sServer = QualifyServer(Environ$("COMPUTERNAME") & vbNullString)
   dwServer = StrPtr(sServer)
   If NetGetJoinInformation(dwServer, _
                            bufptr, _
                            dwBufferType) = NERR_SUCCESS Then
      Text1.Text = GetPointerToByteStringW(bufptr)
      Text2.Text = GetJoinStatus(dwBufferType)
   End If

   NetApiBufferFree bufptr

End Sub

Private Function GetJoinStatus(dwStatus As Long) As String

   Select Case dwStatus
      Case 0: GetJoinStatus = "The status is unknown"
      Case 1: GetJoinStatus = "The computer is not joined"
      Case 2: GetJoinStatus = "The computer is joined to a workgroup"
      Case 3: GetJoinStatus = "The computer is joined to a domain"
      Case Else: GetJoinStatus = "dwStatus outside valid enum range"
   End Select
End Function

Private Function GetPointerToByteStringW(ByVal dwData As Long) As String
   Dim tmp() As Byte
   Dim tmplen As Long
   If dwData <> 0 Then
      tmplen = lstrlenW(dwData) * 2
      If tmplen <> 0 Then
         ReDim tmp(0 To (tmplen - 1)) As Byte
         CopyMemory tmp(0), ByVal dwData, tmplen
         GetPointerToByteStringW = tmp
     End If
   End If
End Function

Private Function QualifyServer(ByVal sServer As String) As String

  'see if there are already two slashes
  'preceeding the server name
   If Left$(sServer, 2) = "\\" Then
     'there are, so the server is already
     'qualified; return the passed string
      QualifyServer = sServer
     'there aren't two, but is there one?
      If Left$(sServer, 1) = "\" Then
        'yes, so add one more
         QualifyServer = "\" & sServer
        'the string needs both
         QualifyServer = "\\" & sServer
      End If  'If Left$(sServer, 1) <> "\"
   End If  'If Left$(sServer, 2) = "\\"
End Function


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter