Solved

Getting windows login name

Posted on 2007-04-07
4
160 Views
Last Modified: 2010-04-30
Hi,
  For getting the windows login user name as a string , what is to be done ?
 I actually used GetEnvironmentVariable, but the value coming when compared to a string variable is not matching

    Thanks,
      Sam
0
Comment
Question by:sgs1970
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18869335
what do you mean with "is not matching" ?
can you explain that.
0
 

Author Comment

by:sgs1970
ID: 18869336

 Hi,
  See the subroutine I used

 
Function GetUserName() As String
    Dim strUserName As String * 255
    strUserName = ""
    'Create a string buffer of 255 chars in length
    Dim X As Integer
    X = GetEnvironmentVariable("USERNAME", strUserName, Len(strUserName))
    If X > 0 Then        'Look for Null Character, usually included
     X = InStr(strUserName, vbNullChar)
    'Trim off buffered spaces too
        If X > 0 Then
   
         Else
            ''  MsgBox (Left$(strUserName, x))
        End If
    End If

   GetUserName = strUserName
End Function
0
 

Author Comment

by:sgs1970
ID: 18869338
The username is not read as a  proper string , it seems
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 18869341
yes, the API returns a null-terminated string (comes from C programming).
you have to do a left() on the returned string, if the vbNullchar is found:

Function GetUserName() As String
    Dim strUserName As String * 255
    strUserName = ""
    'Create a string buffer of 255 chars in length
    Dim X As Integer
    X = GetEnvironmentVariable("USERNAME", strUserName, Len(strUserName))
    If X > 0 Then        'Look for Null Character, usually included
     X = InStr(strUserName, vbNullChar)
    'Trim off buffered spaces too
        If X > 0 Then
            strUserName = left( strUsername, x-1)
         Else
            ''  MsgBox (Left$(strUserName, x))
        End If
    End If

   GetUserName = strUserName
End Function
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question