Link to home
Start Free TrialLog in
Avatar of BobbyH
BobbyH

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BobbyH
BobbyH

ASKER

Beautiful!  Thanks so Much!  Worked like a charm, and I would never have figured that out.