当前位置:首页 > 网络编程 > 软件语言 > .NET > 检验系统帐号是否存在的实例

检验系统帐号是否存在的实例

点击次数:35 次 发布日期:2008-11-06 08:05:20 作者:源代码网
源代码网推荐
广告载入中

源代码网整理以下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


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华