VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



This code is useful when you are getting your logon user with domain name. This code is applicable

by Bhami Reddy (1 Submission)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 27th March 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

This code is useful when you are getting your logon user with domain name. This code is applicable for Windows NT and 9x.

API Declarations


Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long


Rate This code is useful when you are getting your logon user with domain name. This code is applicable



   Dim lResult As Long            ' Result of various API calls.
   Dim I As Integer               ' Used in looping.
   Dim bUserSid(255) As Byte      ' This will contain your SID.
   Dim sUserName As String

   Dim sDomainName As String * 255   ' Domain the user belongs to.
   Dim lDomainNameLength As Long     ' Length of domain name needed.

   Dim lSIDType As Long              ' The type of SID info we are
                                     ' getting back.
   
   ' Get the SID of the user. (Refer to the MSDN for more information on SIDs
   ' and their function/purpose in the operating system.) Get the SID of this
   ' user by using the LookupAccountName API. In order to use the SID
   ' of the current user account, call the LookupAccountName API
   ' twice. The first time is to get the required sizes of the SID
   ' and the DomainName string. The second call is to actually get
   ' the desired information.
   sUserName = GetLogonUser
   lResult = LookupAccountName(vbNullString, sUserName, _
      bUserSid(0), 255, sDomainName, lDomainNameLength, _
      lSIDType)

   ' Now set the sDomainName string buffer to its proper size before
   ' calling the API again.
   sDomainName = Space(lDomainNameLength)
   ' Call the LookupAccountName again to get the actual SID for user.
   lResult = LookupAccountName(vbNullString, sUserName, _
      bUserSid(0), 255, sDomainName, lDomainNameLength, _
      lSIDType)

   ' Return value of zero means the call to LookupAccountName failed;
   ' test for this before you continue.
     If (lResult = 0) Then
        MsgBox "Error: Unable to Lookup the Current User Account: " _
           & sUserName
        Exit Function
     End If
     sDomainName = Left$(sDomainName, InStr(sDomainName, Chr$(0)) - 1)
     GetLogonDomainuser = Trim(sDomainName) & "\" & sUserName

End Function
Private Function GetLogonUser() As String
    Dim strTemp As String, strUserName As String
    'Create a buffer
    strTemp = String(100, Chr$(0))
    'strip the rest of the buffer
    strTemp = Left$(strTemp, InStr(strTemp, Chr$(0)) - 1)

    'Create a buffer
    strUserName = String(100, Chr$(0))
    'Get the username
    GetUserName strUserName, 100
    'strip the rest of the buffer
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    GetLogonUser = strUserName
End Function


Download this snippet    Add to My Saved Code

This code is useful when you are getting your logon user with domain name. This code is applicable Comments

No comments have been posted about This code is useful when you are getting your logon user with domain name. This code is applicable . Why not be the first to post a comment about This code is useful when you are getting your logon user with domain name. This code is applicable .

Post your comment

Subject:
Message:
0/1000 characters