|
-
May 24th, 2002, 11:26 AM
#1
Thread Starter
Registered User
Current User Name
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
-
May 24th, 2002, 11:46 AM
#2
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
-
May 24th, 2002, 12:08 PM
#3
Thread Starter
Registered User
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
-
May 24th, 2002, 12:11 PM
#4
????? What OS are you using?
I've used the code on Win9x, NT, W2K, and never had a problem.
-
May 24th, 2002, 12:15 PM
#5
Thread Starter
Registered User
XP. I'll try it on a 2000 box, because that's what it's going to be used on anyway.
-
May 24th, 2002, 12:17 PM
#6
Thread Starter
Registered User
Just tried it. Didn't work on 2000 either.
-
May 24th, 2002, 12:24 PM
#7
Addicted Member
He is correct Mr. Hack, it does not work on Windows 2000. How do to this on Windows 2000?
Catholics Do It With Beads
-
May 24th, 2002, 12:55 PM
#8
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.
-
May 24th, 2002, 01:21 PM
#9
Thread Starter
Registered User
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
-
May 24th, 2002, 01:26 PM
#10
A little research turned up this. This does work on Windows 2000, but I do not know about XP
VB 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
-
May 24th, 2002, 01:29 PM
#11
Thread Starter
Registered User
Worked like a charm! Youdaman.
-
May 24th, 2002, 01:45 PM
#12
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.
-
May 24th, 2002, 01:47 PM
#13
Thread Starter
Registered User
Currently, I've only tried it on XP. I have some more scripting to do to before I move it to the 2k box.
-
May 24th, 2002, 01:52 PM
#14
Cool!! 
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|