Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Obtain Windows logged in user name (Any)

    Variables and system function which need to be declared in a Module or the Decleration area of a Form:

    Const READ_CONTROL = &H20000
    Const KEY_QUERY_VALUE = &H1
    Const KEY_SET_VALUE = &H2
    Const KEY_CREATE_SUB_KEY = &H4
    Const KEY_ENUMERATE_SUB_KEYS = &H8
    Const KEY_NOTIFY = &H10
    Const KEY_CREATE_LINK = &H20
    Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
    KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
    KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL

    Const HKEY_LOCAL_MACHINE = &H80000002
    Const ERROR_SUCCESS = 0
    Const REG_SZ = 1
    Const REG_DWORD = 4

    Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long

    FUNCTIONS USED TO RETREIVE THE LOGGED USER NAME
    --------------------------------------------------------------------------------------

    Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, ByRef KeyVal As String) As Boolean

    Dim I As Long
    Dim rc As Long
    Dim hKey As Long
    Dim hDepth As Long
    Dim KeyValType As Long
    Dim tmpVal As String
    Dim KeyValSize As Long

    rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey)

    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError

    tmpVal = String$(1024, 0)
    KeyValSize = 1024

    rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
    KeyValType, tmpVal, KeyValSize)

    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError

    If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then
    tmpVal = Left(tmpVal, KeyValSize - 1)
    Else
    tmpVal = Left(tmpVal, KeyValSize)
    End If

    Select Case KeyValType
    Case REG_SZ
    KeyVal = tmpVal
    Case REG_DWORD
    For I = Len(tmpVal) To 1 Step -1
    KeyVal = KeyVal + hex(Asc(Mid(tmpVal, I, 1)))
    Next
    KeyVal = Format$("&h" + KeyVal)
    End Select

    GetKeyValue = True
    rc = RegCloseKey(hKey)

    Exit Function

    GetKeyError:
    KeyVal = ""
    GetKeyValue = False
    rc = RegCloseKey(hKey)
    End Function



    Private Sub GetOsUser()

    Dim SysInfoPath As String
    Dim gREGKEYSYSINFO As String
    Dim gREGVALSYSINFO As String

    gREGKEYSYSINFO = "SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon"
    gREGVALSYSINFO = "DefaultUserName"

    If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO, SysInfoPath) Then

    MachineUser = SysInfoPath

    Else

    Responce = MsgBox("Unable to obtain User Login Name.", vbCritical)
    DoCmd.Close
    DoCmd.Quit

    End If

    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Obtain Windows logged in user name (Any)

    Your code doesn't quite work - you have a procedure GetOsUser that assigns the user name to an undeclared variable MachineUser, so it's not clear how it can be used.

    Moreover, all this code is overkill, since there is a Windows API call that can be used to retrieve the login name - see the GetNetUser function in <post:=446,010>post 446,010</post:>.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •