Click to See Complete Forum and Search --> : User name


alexK
January 29th, 2000, 02:51 PM
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.

Aaron Young
January 29th, 2000, 08:25 PM
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
ajyoung@pressenter.com
aarony@redwingsoftware.com