Hello,
I'd like to extract the current logged in username (%username%)into a string, then insert it into a specific line in a text file. Any help would be appreciated.
Thanks,
Gabe
Printable View
Hello,
I'd like to extract the current logged in username (%username%)into a string, then insert it into a specific line in a text file. Any help would be appreciated.
Thanks,
Gabe
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 Private Sub Command1_Click() Text1.Text = "Here is the current logged on user: " & UserAccount End Sub
Thanks for the reply, but I can't make that work, or at least it isn't getting my current username.
I saw something called NPGetUser in the MSDN, but I've no idea how to write that into code that works. Any ideas there?
Thanks,
Gabe
????? What OS are you using?
I've used the code on Win9x, NT, W2K, and never had a problem. :confused:
XP. I'll try it on a 2000 box, because that's what it's going to be used on anyway.
Just tried it. Didn't work on 2000 either.
He is correct Mr. Hack, it does not work on Windows 2000. How do to this on Windows 2000?
Very curious. GetUserName works on Win2K in my apps there were developed on a Win98 box, but doesn't work on a project started on a Win2K box.
That makes no sense. :confused:
Can anyone help with integrating the NPGetUser API? I get lost whenever I try to do that on my own. It's description in MSDN looks like it does what I need it to do.
Thanks
A little research turned up this. This does work on Windows 2000, but I do not know about XPVB Code:
Private Enum EXTENDED_NAME_FORMAT NameUnknown = 0 NameFullyQualifiedDN = 1 NameSamCompatible = 2 NameDisplay = 3 NameUniqueId = 6 NameCanonical = 7 NameUserPrincipal = 8 NameCanonicalEx = 9 NameServicePrincipal = 10 End Enum Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long Private Sub Command1_Click() Dim sBuffer As String, Ret As Long sBuffer = String(256, 0) Ret = Len(sBuffer) If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then MsgBox "Username: " + Left$(sBuffer, Ret) Else MsgBox "Error while retrieving the username" End If End Sub
Worked like a charm! Youdaman.
Curiosity question: Did you try it with both XP and Win2K?
I just got a Windows 2000 machine yesterday, but I don't have XP.
As I said, just curious. :)
Currently, I've only tried it on XP. I have some more scripting to do to before I move it to the 2k box.
Cool!! :D
I tried on my 2000 box and it worked, so if you ran it on XP, then it is gold on both platforms! Thats good to know. Thanks!