CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: User name

  1. #1
    Join Date
    Oct 1999
    Location
    US
    Posts
    34

    User name

    I need to get user name from the registry.
    What is the best registry key I should look too?
    Or is there any API function I can use to get it?
    Thank you!
    Alex.


  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: User name

    Do you mean the Username used to Login to Windows or the Registered User Name?

    Registered User Name:
    private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (byval hKey as Long, byval lpValueName as string, byval lpReserved as Long, lpType as Long, lpData as Any, lpcbData as Long) as Long
    private Declare Function RegCloseKey Lib "advapi32.dll" (byval hKey as Long) as Long
    private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (byval hKey as Long, byval lpSubKey as string, phkResult as Long) as Long

    private Const HKEY_LOCAL_MACHINE = &H80000002

    private Function GetRegSetting(byval sKey as string, sValue as string, optional byval sDefault as string = "") as string
    Dim lRegKey as Long
    Dim sData as string * 255
    Dim lData as Long
    If RegOpenKey(HKEY_LOCAL_MACHINE, sKey, lRegKey) = 0 then
    lData = 255
    Call RegQueryValueEx(lRegKey, sValue, 0&, 0&, byval sData, lData)
    GetRegSetting = Left(sData, lData - 1)
    else
    GetRegSetting = sDefault
    End If
    Call RegCloseKey(lRegKey)
    End Function

    private Sub cmdGetValue_Click()
    Dim sOwner as string
    sOwner = GetRegSetting("software\Microsoft\Windows\CurrentVersion", "RegisteredOwner")
    MsgBox sOwner
    End Sub


    Currently Logged In Username:
    private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (byval lpBuffer as string, nSize as Long) as Long

    private Sub Command1_Click()
    Dim sUser as string * 255
    Dim lLen as Long

    lLen = 255
    Call GetUserName(sUser, lLen)
    MsgBox Left$(sUser, lLen)
    End Sub



    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured