Solved

Getting windows login name

Posted on 2007-04-07
4
162 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

685 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