1 Replies - 1617 Views - Last Post: 17 October 2012 - 02:41 PM Rate Topic: -----

#1 allgames71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-October 12

get current user location error

Posted 12 October 2012 - 10:08 AM

Hi ,

I write to get the current user name , and the result is :

A> In line 15 , the result is "Raymond\Favorites" , where Raymond is the current user name which locate at Drive D:\

My question is : how can I get "D:\Raymond\Favorites" ???

B> In line 16 , the result is "Raymond\Documents" in Win7

My question is : why is it not "Raymond\Favorites"

I've try to use : FileName = sUserName & "\Videos" , but with the same result : "Raymond\Documents" ???


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

3. Private Sub Form_Load()
4. Dim sUserName As String
5. Dim lSize As Long

6. lSize = 20
7. sUserName = Space(20)
8. Call GetUserName(sUserName, lSize)
9. sUserName = Trim(sUserName)
10. sUserName = Left(sUserName, Len(sUserName) - 1)
11. '------------------------------------------------------------------------------
12. Dim Res
13. Dim FileName
14. FileName = sUserName & "\Favorites"
15. MsgBox FileName
16. Res = Shell("explorer.exe " & FileName, vbNormalFocus)

17. End Sub

Thanks for everybody.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: get current user location error

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: get current user location error

Posted 17 October 2012 - 02:41 PM

I'm not sure why you're getting the error, but lpBuffer should be a string of null characters, not spaces. GetUserName returns a Long, which is the length of the string. So, you pass the string of null characters, and then you take the leftmost characters up to the return value to get the user name. (If you need help with how to do this, ask.)

Now, I'm not sure what you're trying to do with your shell statement. Can you explain what your thinking is there?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1