i want to show who is using the pc now. just show the username of pc to a text box.
thanks
Printable View
i want to show who is using the pc now. just show the username of pc to a text box.
thanks
Use GetUserName api from AdvAPI32.dll
VB Code:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
VB Code:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As _ String, nSize As Long) As Long Private UserAccount As String Private Sub GetUserAccount() Dim sBuffer As String Dim lSize As Long sBuffer = Space$(255) lSize = Len(sBuffer) Call GetUserName(sBuffer, lSize) If lSize > 0 Then UserAccount = Left$(sBuffer, lSize - 1) Else UserAccount = vbNullString End If End Sub
Check out the robdog's post:
How Do I Get The Current Windows Username?
An easier way would beVB Code:
Environ("UserName")
There are few drawbacks if you use environ variables check out the link i posted in my previous post.
DONE
Thanks Everyone
I don't see any drawbacks.Quote:
Originally Posted by cssriraman
Using the environment variables are easy to use but unreliable for these reasons -
1. The user can edit the value to anything they want by going to the System Properties and changing the values.
2. The user can delete the environment variables too.
3. To get the user name is that this variable is not set by default on Win9x/Me.
So, The GetUserName API is probably the most reliable and secure way to retrieve the username. It cannot be changed by the user as long as Windows permissions dissallow it.
How can a user change the UserName in the System Properties or Delete it? Can you give me the steps that will delete this particular Environment Variable or change it.Quote:
Originally Posted by cssriraman
Never tested it on Win9x/ME.Quote:
Originally Posted by cssriraman
I believe its when used under Windows 95/98 that you can mess with the username environment variable. You can create a duplicate Environmental variable but depends on the method used to read it (API vs Environ(UserName)).
what are the other arguments (aside from Username) for Environ()? (my first time to hear that) :DQuote:
Originally Posted by Shuja Ali
:thumb: :thumb:
You can view, add, edit, and delete the variables from -
Control Panel > System Properties > Advanced tab > Environment Variables button.
to get the variables execute this codeQuote:
what are the other arguments (aside from Username) for Environ()? (my first time to hear that)
but before using the environ variables consider Post #9 by cssriramanVB Code:
Dim lEnv As Long Do While True If Environ(lEnv) = "" Then Exit Do End If Debug.Print Environ(lEnv) lEnv = lEnv + 1 Loop
In Win2K & XP, the Environmental Variables like UserName, etc are not shown to the user in the System Properties. Even If the user tries to change it using Set Command the System will still show the correct USERNAME. So I guess using Environ in Win 2K/XP is safe. What say?Quote:
Originally Posted by RobDog888
Yes, but if your app is multi-platform then you cant take the chance. Easier to just use the more stable and secure way and not have to worry about it.
You know you can create a UserName variable in 2000/XP but when reading it it will give the system value. ;)
I strongly beleive that people who are still using Win98/ME do not have any right to get my applications. :(Quote:
Originally Posted by RobDog888
I especially hate people who are using Win ME. Win ME comes up with all kind of weird problems.
Thats what I said in my previous post. ;) And using Environ in that case is Safe. :duck:Quote:
Originally Posted by RobDog888
Well I just believe you dont assume anything in programming and using the Environ function is partly an assumption of what OS your app may be running on and other issues already posted. I dont like problems in my apps so I dont use it.
True. :thumb: I also beleive in making software that doesn't give unusual results..
But the place where I work, they don't use any other OS apart from XP. So I guess my Apps are safe.