Home > Archive > Visual Basic Syntax > August 2005 > Capture Logged-in UserName ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Capture Logged-in UserName ?
|
|
|
| First of all, apologies if this is covered elsewhere in the forum but I've
looked high and low and can't find anything.
I'm relatively new to VB6 and am trying to do something thats probably quite
simple: I need to capture the user's ID or username while they're logged in
and display it on a form. Can anyone explain how I do that please ?
Thanks
| |
| Bob Butler 2005-08-18, 6:01 pm |
| "Will" <Will@discussions.microsoft.com> wrote in message news:A4ED50D4-
F8AF-4EF9-A0A4-F3731CE1BE73@microsoft.com
> First of all, apologies if this is covered elsewhere in the forum but
> I've looked high and low and can't find anything.
> I'm relatively new to VB6 and am trying to do something thats
> probably quite simple: I need to capture the user's ID or username
> while they're logged in and display it on a form. Can anyone explain
> how I do that please ? Thanks
The simplest, and least reliable, is to use the Environ$ function to read
environment variables
Assuming you just need the simple username and no domain information then:
Private Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal UserName As String, _
ByRef BuffSize As Long) As Long
Public Function CurrentUser() As String
Dim sBuff As String * 512
Dim x As Long
CurrentUser = ""
x = GetUserName(sBuff, Len(sBuff) - 1)
If x > 0 Then
x = InStr(sBuff, vbNullChar)
If x > 0 Then CurrentUser = Left$(sBuff, x - 1)
End If
End Function
If you need the domain also then it's more involved
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
| |
| Jeff Johnson [MVP: VB] 2005-08-18, 6:01 pm |
|
"Will" <Will@discussions.microsoft.com> wrote in message
news:A4ED50D4-F8AF-4EF9-A0A4-F3731CE1BE73@microsoft.com...
> First of all, apologies if this is covered elsewhere in the forum but I've
> looked high and low and can't find anything.
> I'm relatively new to VB6 and am trying to do something thats probably
> quite
> simple: I need to capture the user's ID or username while they're logged
> in
> and display it on a form. Can anyone explain how I do that please ?
Try again, searching microsoft.public.vb.general.discussion for the keyword
"GetUserName".
| |
| Tony Proctor 2005-08-18, 6:01 pm |
| Getting the domain name isn't that much more involved Bob. Here's a very
slightly modified version of your own code: :-)
Private Declare Function GetUserNameEx Lib "secur32" Alias "GetUserNameExA"
( _
ByVal format As Long, ByVal Name As String, ByRef namlen As Long) As
Long
Private Const NameSamCompatible As Long = 2
Public Function CurrentUser() As String
Dim sBuff As String * 512
Dim x As Long
CurrentUser = ""
x = GetUserNameEx(NameSamCompatible, sBuff, Len(sBuff) - 1)
If x > 0 Then
x = InStr(sBuff, vbNullChar)
If x > 0 Then CurrentUser = Left$(sBuff, x - 1)
End If
End Function
Tony Proctor
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:O35jrQ$oFHA.572@TK2MSFTNGP15.phx.gbl...
> "Will" <Will@discussions.microsoft.com> wrote in message news:A4ED50D4-
> F8AF-4EF9-A0A4-F3731CE1BE73@microsoft.com
>
> The simplest, and least reliable, is to use the Environ$ function to read
> environment variables
>
> Assuming you just need the simple username and no domain information then:
> Private Declare Function GetUserName Lib "advapi32.dll" _
> Alias "GetUserNameA" (ByVal UserName As String, _
> ByRef BuffSize As Long) As Long
>
> Public Function CurrentUser() As String
> Dim sBuff As String * 512
> Dim x As Long
> CurrentUser = ""
> x = GetUserName(sBuff, Len(sBuff) - 1)
> If x > 0 Then
> x = InStr(sBuff, vbNullChar)
> If x > 0 Then CurrentUser = Left$(sBuff, x - 1)
> End If
> End Function
>
> If you need the domain also then it's more involved
>
> --
> Reply to the group so all can participate
> VB.Net: "Fool me once..."
>
| |
| Bob Butler 2005-08-18, 6:01 pm |
| "Tony Proctor" <tony_proctor@aimtechnology_NoMoreSPAM_.com> wrote in
message news:e7VGh5ApFHA.1304@TK2MSFTNGP10.phx.gbl
> Getting the domain name isn't that much more involved Bob. Here's a
> very slightly modified version of your own code: :-)
&%$&%& &$&%$^%
I guess I can toss all the code I have that uses the process/thread tokens
and lookupaccountXXX functions! I don't remember seeing GetUserNameEx
before; I was probably working with Windows 95 last time I looked for this
functionality though.
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
| |
| Karl E. Peterson 2005-08-18, 6:01 pm |
| Bob Butler wrote:
> "Tony Proctor" <tony_proctor@aimtechnology_NoMoreSPAM_.com> wrote in
> message news:e7VGh5ApFHA.1304@TK2MSFTNGP10.phx.gbl
>
> &%$&%& &$&%$^%
>
> I guess I can toss all the code I have that uses the process/thread
> tokens and lookupaccountXXX functions! I don't remember seeing
> GetUserNameEx before; I was probably working with Windows 95 last
> time I looked for this functionality though.
Yeah, I was gonna add it's for W2K+ only...
--
Working Without a .NET?
http://classicvb.org/petition
| |
| Tony Proctor 2005-08-19, 4:22 pm |
| ....you mean people still use older O/S versions? .............snigger!
Tony Proctor
"Karl E. Peterson" <karl@mvps.org> wrote in message
news:#u$$ClBpFHA.1148@TK2MSFTNGP12.phx.gbl...
> Bob Butler wrote:
>
> Yeah, I was gonna add it's for W2K+ only...
> --
> Working Without a .NET?
> http://classicvb.org/petition
>
>
| |
| Karl E. Peterson 2005-08-22, 7:05 pm |
| Tony Proctor wrote:
> ...you mean people still use older O/S versions? .............snigger!
So I hear. <g> For me, I'm just about ready to abandon support for things
pre-Win2000. Shareware folks need to be aware, but probably not corp-types.
--
Working Without a .NET?
http://classicvb.org/petition
| |
| Dick Grier 2005-08-22, 7:05 pm |
| I get a few questions about 9x. I have to say, "Sorry, I no longer have any
systems that run it." Of course, I could install it under Virtual PC, but
why cause (me) pain?
Dick
--
Richard Grier (Microsoft Visual Basic MVP)
See www.hardandsoftware.net for contact information.
Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
www.mabry.com/vbpgser4 to order.
| |
| Karl E. Peterson 2005-08-22, 7:05 pm |
| Dick Grier wrote:
> I get a few questions about 9x. I have to say, "Sorry, I no longer
> have any systems that run it." Of course, I could install it under
> Virtual PC, but why cause (me) pain?
Yeah, I actually still have both 95 and 98 functional systems, but there's a lot of
pain to be found simply logging into either. <g> That, and, I never even bothered
installing VB6 on the 95 box.
--
Working Without a .NET?
http://classicvb.org/petition
|
|
|
|
|