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
В успешном случае функция возвращает ненулевое значение. В случае ошибки функция возвращает 0 (для получения кода ошибки используйте GetLastError)
' положите на форму кнопку,
' текстовое поле и две надписи,
' в которых будет отображаться информация
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