'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