getting NT user names 
Author Message
 getting NT user names

Any ideas on how to get the user's login name from within a VB app?



Sun, 19 Jan 2003 03:00:00 GMT  
 getting NT user names
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long

Private Function GetUser() As String
    Dim UserName As String
    Dim POS As Integer

    UserName = Space(256)

    RetCode = GetUserName(UserName, 256)

    POS = InStr(1, UserName, Chr(0), vbTextCompare)

    UserName = Left(UserName, POS - 1)

    GetUser = UserName
End Function

Andy

Quote:
> Any ideas on how to get the user's login name from within a VB app?



Sun, 19 Jan 2003 03:00:00 GMT  
 getting NT user names
Ok ,thanks-- API only I guess


Quote:
> Private Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA"
> (ByVal lpBuffer As String, nSize As Long) As Long

> Private Function GetUser() As String
>     Dim UserName As String
>     Dim POS As Integer

>     UserName = Space(256)

>     RetCode = GetUserName(UserName, 256)

>     POS = InStr(1, UserName, Chr(0), vbTextCompare)

>     UserName = Left(UserName, POS - 1)

>     GetUser = UserName
> End Function

> Andy


> > Any ideas on how to get the user's login name from within a VB app?



Mon, 20 Jan 2003 03:00:00 GMT  
 getting NT user names

You can also use UserName = Environ("Username") but I have found that it is
not as reliable.


Quote:
> Ok ,thanks-- API only I guess



> > Private Declare Function GetUserName Lib "advapi32.dll" Alias
> "GetUserNameA"
> > (ByVal lpBuffer As String, nSize As Long) As Long

> > Private Function GetUser() As String
> >     Dim UserName As String
> >     Dim POS As Integer

> >     UserName = Space(256)

> >     RetCode = GetUserName(UserName, 256)

> >     POS = InStr(1, UserName, Chr(0), vbTextCompare)

> >     UserName = Left(UserName, POS - 1)

> >     GetUser = UserName
> > End Function

> > Andy


> > > Any ideas on how to get the user's login name from within a VB app?



Mon, 20 Jan 2003 03:00:00 GMT  
 getting NT user names
how did you find it to be unreliable?


Quote:
> You can also use UserName = Environ("Username") but I have found that it
is
> not as reliable.



> > Ok ,thanks-- API only I guess



> > > Private Declare Function GetUserName Lib "advapi32.dll" Alias
> > "GetUserNameA"
> > > (ByVal lpBuffer As String, nSize As Long) As Long

> > > Private Function GetUser() As String
> > >     Dim UserName As String
> > >     Dim POS As Integer

> > >     UserName = Space(256)

> > >     RetCode = GetUserName(UserName, 256)

> > >     POS = InStr(1, UserName, Chr(0), vbTextCompare)

> > >     UserName = Left(UserName, POS - 1)

> > >     GetUser = UserName
> > > End Function

> > > Andy


> > > > Any ideas on how to get the user's login name from within a VB app?



Mon, 20 Jan 2003 03:00:00 GMT  
 getting NT user names
Andy, it's just an Environment Variable
and can be changed using the 'SET' command
You could easily set the variable to indicate
a user with more permissions than your own.
(if you knew what you where doing of course)

If its just a case of;

msgbox "Hi " & environ("Username")

it's fine.
HTH
Mike :o)

Quote:

>how did you find it to be unreliable?



>> You can also use UserName = Environ("Username") but I have found that it
>is not as reliable.



Mon, 20 Jan 2003 03:00:00 GMT  
 getting NT user names

I have had instances when Environ("username") returned a blank value and the
API returned the correct value.

-Andy



Mon, 20 Jan 2003 03:00:00 GMT  
 getting NT user names
Great, I created a user name activex dll and registered it in my system.
Thanks for all your help!
Code below for clsCurrentUser:

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsCurrentUser"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Attribute VB_Ext_KEY = "RVB_UniqueId" ,"39897B570394"
'
Option Base 0

'private variables
'##ModelId=39897B580037
Private Const SIZE As Long = 255
'##ModelId=39897B58005F
Private user_name_buffer As String * SIZE
' Custom error constants:
'##ModelId=39897B580069
Private Const CUSTOM_ERR_SOURCE As String = "clsCurrentUser Object"
'##ModelId=39897B580073
Private Const CUSTOM_ERR_ERRNUMBASE As Long = vbObjectError + 512
'API Function Calls
'##ModelId=39897B5800A5
Private Declare Function getUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
'local variable(s) to hold property value(s)
'##ModelId=39897B580091
Private mvarUserName As String

'##ModelId=39897CF50153
Private Sub Class_Terminate()
    #If DebugMode Then
    'the class is being destroyed
    Debug.Print "'" & TypeName(Me) & "' instance " & CStr(mlClassDebugID) &
" is terminating"
    #End If
End Sub

'##ModelId=39897CF5013F
Private Sub Class_Initialize()
    #If DebugMode Then
        'get the next available class ID, and print out
        'that the class was created successfully
        mlClassDebugID = GetNextClassDebugID()
        Debug.Print "'" & TypeName(Me) & "' instance " &
CStr(mlClassDebugID) & " created"
    #End If
End Sub

'##ModelId=39897B5800CD
Public Property Get UserName() As String
Attribute UserName.VB_Description = "This property returns the current user
of the  Operating System."
'used when retrieving value of a property, on the right side of an
assignment.
'Syntax: Debug.Print X.UserName
  Dim succeeded As Long

  On Error GoTo UserName_Err
  succeeded = getUserName(user_name_buffer, SIZE)
  If (succeeded <> 0) Then 'GetUserName succeeded
    'may need to remove the empty space
    UserName = user_name_buffer
  Else
    Err.Raise CUSTOM_ERR_ERRNUMBASE, CUSTOM_ERR_SOURCE, "GetUserName failed"
  End If
UserName_End:
  Exit Property
UserName_Err:
  Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile,
Err.HelpContext
  Resume UserName_End
End Property


Quote:
> I have had instances when Environ("username") returned a blank value and
the
> API returned the correct value.

> -Andy



Mon, 20 Jan 2003 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Getting NT User Name in VBScript

2. getting windows NT user name

3. Getting NT Users LOGIN NAME?

4. Getting NT User Login Name

5. Getting user names on NT domain

6. Getting the NT User Name

7. Getting user name during NT logon

8. Getting the full user name under Win NT

9. getting users name and workstation name Urgent

10. Getting the user's logon name and computer name

11. LDB Viewer -- Need to get NT user login name in addition to computer name

12. Getting the User Group name using Current User Function

 

 
Powered by phpBB® Forum Software