|
-
Oct 17th, 2008, 03:33 PM
#1
Thread Starter
Lively Member
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!!!
-
Oct 17th, 2008, 03:51 PM
#2
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.
-
Oct 17th, 2008, 03:55 PM
#3
Thread Starter
Lively Member
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)"??
-
Oct 17th, 2008, 03:56 PM
#4
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
-
Oct 17th, 2008, 04:02 PM
#5
Thread Starter
Lively Member
Re: Help with username
 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
-
Oct 17th, 2008, 04:03 PM
#6
Re: Help with username
 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.
-
Oct 17th, 2008, 04:04 PM
#7
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
-
Oct 17th, 2008, 04:07 PM
#8
Thread Starter
Lively Member
Re: Help with username
 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!!!!!!!!
-
Oct 17th, 2008, 04:15 PM
#9
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
-
Oct 17th, 2008, 04:31 PM
#10
Re: Help with username
 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
-
Oct 18th, 2008, 03:02 AM
#11
Junior Member
Re: Help with username
Alternatively
You could use
sName = Environ("USERNAME")
-
Oct 18th, 2008, 02:11 PM
#12
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?
-
Oct 18th, 2008, 03:06 PM
#13
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|