• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

Getting the logged in users name and appending to a path

Hello!  I am trying to write a progeam, and have a roadblock that I can't seem to figure out.  Please be kind, this may be really basic, but this is actually my first "real" program that does something useful, and I've been getting code snippets off of the net and cobbling them together, along with my own stuff.

My Problem:  I need PathToSource to equal "M:\Documents and Settings\" + GetUser + "\ntuser.dat".  Getuser is the currently logged in user.  I am getting only the first string and GetUser in my msgbox, leaving out the "\ntuser.dat".  Can someone figure out why?  BTW, the getUser routine below is by Matthew Grove, I found it at:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=891&lngWId=-1

Thanks!!

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long
Private Sub Form_Load()
Dim sBuffer As String
Dim lSize As Long
Dim result As Integer
Dim PathToSource As String


' Parameters for the dll declaration are _
     set
sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer, lSize) ' Call the declared dll function
If lSize > 0 Then
Getuser = Left$(sBuffer, lSize) ' Remove empty spaces
Else
Getuser = vbNullString

End If
PathToSource = "M:\Documents and Settings\" + Getuser + "\ntuser.dat"
result = MsgBox(PathToSource, vbOKOnly, "Test")
End Sub
0
BobbyH
Asked:
BobbyH
1 Solution
 
TimCotteeCommented:
Hi BobbyH,

> If lSize > 0 Then
> Getuser = Left$(sBuffer, lSize) ' Remove empty spaces
> Else

Change this to:

If lSize > 0 Then
  GetUser = Left(sBuffer,Instr(sBuffer,Chr(0)) - 1)
Else
...

There are NULL characters at the end of the returned sBuffer, these are not removed by the original statement and will cause your final result to appear tuncated.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 
BobbyHAuthor Commented:
Beautiful!  Thanks so Much!  Worked like a charm, and I would never have figured that out.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now