Here is some easy code to get the name of the user currently logged into Win95 or WinNT. This is the name the user types into the login dialog when Win95 or WinNT starts.
Declare
Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" (lpName As Any, ByVal lpUserName$, lpnLength&)
Code
Public Function GetMachineUserName() As String
Dim pstrUserName As String
Dim plngUserNameLen As Long
Dim plngReturn As Long
pstrUserName = Space(256)
plngUserNameLen = Len(pstrUserName)
plngReturn = WNetGetUser(ByVal 0&, pstrUserName, plngUserNameLen)
If plngReturn = 0 Then
' Success - strip off the null.
GetMachineUserName = Left(pstrUserName, InStr(pstrUserName, Chr(0))
- 1)
Else
GetMachineUserName = ""
End If
End Function
|