Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Windows and Controls Code Examples

Read the username from the Registry

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
Read the username from the Registry 'getting the username whenever the user is logged in a windows network or a novellnetwork Private Declare Function GetUserName Lib "advapi32.dll" alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Private Declare Function RegOpenKeyEx Lib "advapi32.dll" alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long 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 Const KEY_ALL_CLASSES As long = &HF0063 Private Const REG_SZ As long = 1 Private Const ERROR_SUCCESS = 0& Private Function LooseSpace(invoer$) As String Dim p% p% = InStr(invoer$, Chr(0)) If p% <> 0 Then LooseSpace$ = Left$(invoer$, p% - 1) exit Function end If LooseSpace$ = invoer$ End Function Public Function GetNovellName() As String Dim res&, lpBuffer$, nSize& nSize = 8 lpBuffer = String(8, 0) res& = GetUserName(lpBuffer, nSize) GetNovellName$ = LooseSpace(Left$(lpBuffer, nSize)) 'user is not logged on to a windows-network 'the username is kept in the HKEY_LOCAL_MACHINE\NETWORK\LOGON\username If GetNovellName$ = "" Then _ GetNovellName = RegGetString$(&H80000002, "NETWORK\LOGON", "username") End Function Private Function RegGetString$(hInKey As Long, ByVal subkey$, ByVal valname$) dim RetVal$, hSubKey As Long, dwType As Long, SZ As Long, v$, r As Long RetVal$ = "" r = RegOpenKeyEx(hInKey, subkey$, 0, KEY_ALL_CLASSES, hSubKey) If r <> ERROR_SUCCESS Then GoTo Quit_Now SZ = 256: v$ = String$(SZ, 0) r = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ) If r = ERROR_SUCCESS And dwType = REG_SZ Then RetVal$ = Left(v$, SZ - 1) Else RetVal$ = "" end If If hInKey = 0 Then r = RegCloseKey(hSubKey) Quit_Now: RegGetString$ = RetVal$ End Function Return