Results 1 to 13 of 13

Thread: Help with username

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    81

    Help with username

    I have this written to get the current Windows username and add stuff before and after:

    Code:
            
    Username = Space(40)
    GetUserName Username, Len(Username)
          
    strSource = "C:\Documents and Settings\" & Username & "\Favorties"
    lblPath.Caption = strSource
    It returns the first string and the username however it doesn't return anything added after that, in this case "\Favorites".

    I am very green when it comes to programming, I know enough for small things but I can't figure this out. Anything would be helpful.

    Thanks!!!

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Help with username

    Perhaps it just doesn't show all the text. Put:
    Code:
    Debug.Print strSource
    ... instead, to see if that's true.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    81

    Re: Help with username

    HAHAHAHA, it DOES worked, this is what it returned

    Code:
    C:\Documents and Settings\Evan                                    \Favs
    So it puting 36 spaces after the username, which is a total of 40 which is

    Code:
    Username = Space(40)
    I got this code from a forum, do I have to have the "space(40)"??

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Help with username

    The returned string i's null terminated, use a nullstrip type function.

    A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. MSDN

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    81

    Re: Help with username

    Quote Originally Posted by Edgemeal
    The returned string i's null terminated, use a nullstrip type function.

    A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. MSDN
    I am sorry I am not that smart!!! LOL

    So I would modify this at the top?

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

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Help with username

    Quote Originally Posted by Edgemeal
    The returned string i's null terminated, use a nullstrip type function.

    A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. MSDN
    It's actually easier than that. The variable passed as Length to GetUserName is returned as how many characters+1 in the string to use.
    Last edited by LaVolpe; Oct 17th, 2008 at 04:14 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Help with username

    This works for me,
    Code:
    Public Function CurrentUser() As String
    Dim sBuff As String * 512
    Dim x As Long
    CurrentUser = ""
    x = GetUserName(sBuff, Len(sBuff) - 1)
    If x > 0 Then
      x = InStr(sBuff, vbNullChar)
      If x > 0 Then CurrentUser = Left$(sBuff, x - 1)
    End If
    End Function

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    81

    Re: Help with username

    Quote Originally Posted by Edgemeal
    This works for me,
    Code:
    Public Function CurrentUser() As String
    Dim sBuff As String * 512
    Dim x As Long
    CurrentUser = ""
    x = GetUserName(sBuff, Len(sBuff) - 1)
    If x > 0 Then
      x = InStr(sBuff, vbNullChar)
      If x > 0 Then CurrentUser = Left$(sBuff, x - 1)
    End If
    End Function

    That worked fricked awesome!!! Thank you so much!!!!!!!!

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Help with username

    A little different, just a little less code...
    Code:
    Public Function CurrentUser() As String
    Dim sBuff As String * 512
    Dim x As Long
        x = Len(sBuff) - 1
        If GetUserName(sBuff, x) <> 0 Then  CurrentUser = Left$(sBuff, x - 1)
    End Function
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Help with username

    Quote Originally Posted by LaVolpe
    A little different, just a little less code...
    Code:
    Public Function CurrentUser() As String
    Dim sBuff As String * 512
    Dim x As Long
        x = Len(sBuff) - 1
        If GetUserName(sBuff, x) <> 0 Then  CurrentUser = Left$(sBuff, x - 1)
    End Function

  11. #11
    Junior Member
    Join Date
    Oct 2008
    Location
    Castle Combe
    Posts
    30

    Re: Help with username

    Alternatively

    You could use

    sName = Environ("USERNAME")

  12. #12
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Help with username

    And all of this is exactly the wrong way to go about getting that path. A user's Profile folder doesn't even have to match the UserName at all (such as when a UserName is renamed after creation). The locations of Windows Special Folders can vary from OS to OS and can even be moved via Group Policy. In Vista for example this is C:\Users\<user>\Favorites by default - though Vista will wink and chuckle slightly at you and substitute for the junction "Documents and Settings" if an old program still uses it. You can see this junction via DIR:

    Code:
    C:\>dir /aL
     Volume in drive C is TeraBlade1093
     Volume Serial Number is A8F3-E016
    
     Directory of C:\
    
    11/02/2006  09:02 AM    <JUNCTION>     Documents and Settings [C:\Users]
                   0 File(s)              0 bytes
                   1 Dir(s)  13,177,818,001,408 bytes free
    
    C:\>
    The right approach is as in:
    Code:
    Private Sub ShowUserFavorites()
        Const ssfFAVORITES = &H6&
        
        With CreateObject("Shell.Application")
            MsgBox .NameSpace(ssfFAVORITES).Self.Path
        End With
    End Sub
    Even better would be to add a reference to Microsoft Shell Controls and Automation and use early binding. This also gives you access to the Enum containing the Special Folder Constants. Alternatively you could use a Declare Function for SHGetFolderPath in Shell32.dll, but why work so hard for something only needed momentarily?

    Getting UserName for this is wrong on many levels then, and can fail in numerous ways. Again, why work so hard when the correct approach is so easy?

  13. #13
    Junior Member
    Join Date
    Oct 2008
    Location
    Castle Combe
    Posts
    30

    Re: Help with username

    Nice and effective....only wish Id read the question a bit better

    Good post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width