OK everyone I have just pulled out my last strand of hair and I'm not a happy bloke.

I'm trying to get my little program to go into the Windows NT registry and take the Username of the current user so that my program can make a log in the form of:

"Username read the message at date and time"

eg

if my NT Boat ID is Paul:

"Paul read the message at 26/9/00 21:00"


But it isn't printng the Username, just the remainder of the message.

Here's the code I've tried:


'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$ 'used in getting the logged in username

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 'on WIN 98 using this path works
If GetNovellName$ = "" Then _
GetNovellName = RegGetString$(&H80000002, "Software\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultUserName") 'this is the NTregistry ocation of the 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$ 'defines regstring for geting logged in username

End Function


I've also tried:


Private Declare Function win32_nlWNetGetUserA Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long

Public Function duwinsys_sWNetGetUser() As String

'Retrieve the logged-in username.

Dim nPos As Integer
Dim nlBufLength As Long
Dim sUserId As String
Dim sBuf As String

sUserId = ""
sBuf = Space$(256)
nlBufLength = Len(sBuf)

If win32_nlWNetGetUserA("", sBuf, nlBufLength) = 0 Then
nPos = InStr(sBuf, Chr$(0))
If nPos > 0 Then
sUserId = Left$(sBuf, nPos - 1)
Else
sUserId = sBuf
End If
End If

duwinsys_sWNetGetUser = sUserId

But this also fails to put in the Username


I have also been told that our NT system has a system varialble called %username%, which, in NT script automatically gets the logged in username, but me being a complete novice, can't work out how to use this in my program.


Please help me try to get this sorted out, it the last part of this project that I'm working on and its the part thats giving me the most grief.

Any help would be most gratefully appreciated by this stuck newbie


Cheers


Paul