檢驗系統帳號是否存在的例項

TolyHuang發表於2009-02-05

[@more@]
Option Explicit

Private Declare Function LookupAccountName Lib "advapi32" _
   Alias "LookupAccountNameA" _
  (ByVal lpSystemName As String, _
   ByVal lpAccountName As String, _
   Sid As Byte, _
   cbSid As Long, _
   ByVal DomainName As String, _
   cbDomainName As Long, _
   peUse As Long) As Long


Private Sub Command1_Click()

   Dim sAccount As String  'account name of interest
   Dim sSystem As String   'specifying the system
   Dim sDomain As String   'domain validating user
   Dim sValid As String
   
  'The account name is a null-terminated
  'string specifying the account name of
  'interest.
   sAccount = Text1.Text
     
  'The system name is a null-terminated
  'string specifying the system - this
  'string can be the name of a remote computer.
  'If this string is null, the account name
  'is looked up on the local system.
   sSystem = ""
   
  'The domain name is a buffer where the
  'call returns the name of the domain where
  'the account name is found. It is not for
  'specifying the domain that you want the
  'lookup made on. If this parameter is passed,
  'the function returns the required buffer size.
   sDomain = ""
   
   Label1.Caption = "working ..."
   Label1.Refresh
   
   Select Case ValidateUser(sAccount, sDomain, sSystem)
      Case True:  sValid = "User has been validated."
      Case False: sValid = "User not found."
   End Select

   Label1.Caption = sValid
   Label2.Caption = sDomain
   
End Sub


Public Function ValidateUser(ByRef sAccountName As String, _
                             Optional ByRef sDomainName As String, _
                             Optional ByVal sSystemName As String) As Boolean


   Dim success As Long
   Dim cbSid As Long
   Dim cbDomainName As Long
   Dim peUse As Long
   Dim bSID() As Byte

   sDomainName = vbNullString
   cbDomainName = 0
   
   If Len(sSystemName) = 0 Then
   
     'If the system name (machine name)
     'not specified, pass a null string
     'to have the account lookup on
     'the local machine
      sSystemName = vbNullString

   End If
   
  'First call passes null as the SID.
  'The call returns a success of 0 and
  'the required buffer size in cbSid.
  'In addition, because sDomainName is
  'passed as null, cbDomainName returns
  'the required buffer size for the lookup
  'domain.
   success = LookupAccountName(sSystemName, _
                           sAccountName, _
                           0&, _
                           cbSid, _
                           sDomainName, _
                           cbDomainName, _
                           peUse)
                        
  'prevent errors
   If (success = 0) And (cbSid > 0) Then
   
     'Prepare a buffer into which
     'the domain where the account
     'name is found will be returned
      sDomainName = Space$(cbDomainName)
            
     'create a buffer for the SID and
     'call again.
      ReDim bSID(0 To cbSid - 1)

     'The function attempts to find a SID
     'for the specified name by first
     'checking a list of well-known SIDs.
     'If the name does not correspond to a
     'well-known SID, the function checks
     'built-in and administratively-defined
     'local accounts. Next, the function
     'checks the primary domain. If the name
     'is not found there, trusted domains
     'are checked.
     'On Windows 2000/XP, in addition to
     'lookup local accounts, local domain
     'accounts, and explicitly trusted
     'domain accounts, LookupAccountName
     'can look up the name for any account
     'in any domain in the Windows 2000 forest.
     '
     'The further 'out' the search has to go,
     'the longer it will take to return.
     '
     'peUse returns a pointer to a SID_NAME_USE
     'enumerated type indicating the type of
     'the account when the function returns.
     '
     'A (SID) is a value that uniquely identifies
     'a user or group on all Windows NT implementations.
      success = LookupAccountName(sSystemName, _
                                  sAccountName, _
                                  bSID(0), _
                                  cbSid, _
                                  sDomainName, _
                                  cbDomainName, _
                                  peUse)
      If success > 0 Then
      
        'obtain the domain name
        'returned
         If cbDomainName > 0 Then
            sDomainName = Left$(sDomainName, cbDomainName)
         End If
         
      End If
      
   End If
      
  'the call succeeded if success is greater than 0
   ValidateUser = success
   
End Function

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9437124/viewspace-1016752/,如需轉載,請註明出處,否則將追究法律責任。

相關文章