Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NT Username and Full Name Retrieval in VBA

66 views
Skip to first unread message

Steve

unread,
Aug 23, 2001, 1:41:40 PM8/23/01
to
Dear All
I would like to retrieve the NT Domain User's Full Name using a macro, and
put it into an Excel Worksheet cell.
I know how to receive the NT username, using the Windows API GetUserName
Function, and this works well in Excel 97.
Can anyone help me to retrieve the Full Name? I've seen an example in MSDN
of how to do this in C++, but can it be done in Excel?
My set up is WinNT 4.0, SP4 on an NT Domain, using Excel 97, SR2
Best Regards
Steve


Dev Ashish

unread,
Aug 23, 2001, 5:22:23 PM8/23/01
to
Steve wrote

> Can anyone help me to retrieve the Full Name?

Try this link.

[ http://www.mvps.org/access/api/api0066.htm ]

Although it's written for Access, you shouldn't have any problems porting
it to Excel.

-- Dev

Steve

unread,
Aug 24, 2001, 8:20:34 AM8/24/01
to
Thanks Dev
I'll give it a try!

Dev Ashish <d...@nomail.please> wrote in message
news:Xns9106B04CA86A5...@207.46.230.185...

Justin Gilbert

unread,
Aug 24, 2001, 2:30:34 PM8/24/01
to
Below is an example that came from MSDN.

I have tried out the code and works.
I have added a text inside the pserver variable that took
me a bit to figure out.

Hope it helps.

Justin

>-----Original Message-----
>Dear All
>I would like to retrieve the NT Domain User's Full Name

using a macro, ---

'USER_INFO_3 structure is defined in Win32 SDK,
below is the VB
'declare.
'LPWSTR is a pointer to a Unicode string.
'The storage of the USER_INFO_3 structure,
including the string
'buffer referred by its LPWSTR members, are all
allocated by Windows
'NT so the function caller doesn't have to allocate
memory.

Private Type USER_INFO_3
usri3_name As Long 'LPWSTR in SDK
usri3_password As Long 'LPWSTR in SDK
usri3_password_age As Long 'DWORD in SDK
usri3_priv As Long 'DWORD in SDK
usri3_home_dir As Long 'LPWSTR in SDK
usri3_comment As Long 'LPWSTR in SDK
usri3_flags As Long 'DWORD in SDK
usri3_script_path As Long 'LPWSTR in SDK
usri3_auth_flags As Long 'DWORD in SDK
usri3_full_name As Long 'LPWSTR in SDK
usri3_usr_comment As Long 'LPWSTR in SDK
usri3_parms As Long 'LPWSTR in SDK
usri3_workstations As Long 'LPWSTR in SDK
usri3_last_logon As Long 'DWORD in SDK
usri3_last_logoff As Long 'DWORD in SDK
usri3_acct_expires As Long 'DWORD in SDK
usri3_max_storage As Long 'DWORD in SDK
usri3_units_per_week As Long 'DWORD in SDK
usri3_logon_hours As Long 'PBYTE in SDK
usri3_bad_pw_count As Long 'DWORD in SDK
usri3_num_logons As Long 'DWORD in SDK
usri3_logon_server As Long 'LPWSTR in SDK
usri3_country_code As Long 'DWORD in SDK
usri3_code_page As Long 'DWORD in SDK
usri3_user_id As Long 'DWORD in SDK
usri3_primary_group_id As Long 'DWORD in SDK
usri3_profile As Long 'LPWSTR in SDK
usri3_home_dir_drive As Long 'LPWSTR in SDK
usri3_password_expired As Long 'DWORD in SDK
End Type

Private Declare Function NetUserGetInfo
Lib "netapi32.dll" ( _
strServerName As Any, strUserName As Any, ByVal
dwLevel As Long, _
pBuffer As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32"
Alias "RtlMoveMemory" _
(hpvDest As Any, ByVal hpvSource As Long, ByVal
cbCopy As Long)


Private Sub Command1_Click()
Dim pServer() As Byte, pUser() As Byte
'You need to change "Your_Domain_Logon_Name" in
the next line
'to your valid NT domain logon name.
pUser = "Place your user id here" & vbNullChar
pServer = "\\ + your server or domain here" &
vbNullChar
'The above two lines convert VB string to
Unicode string.

Dim dwLevel As Long
dwLevel = 3

Dim tmpBuffer As USER_INFO_3
Dim ptmpBuffer As Long

Debug.Print NetUserGetInfo(pServer(0), pUser(0),
dwLevel, ptmpBuffer)
'As last param is dimmed as long, the pointer to
ptmpBuffer is
'passed to dll, and the function returns a
pointer to a pointer
'to our UDT. Therefore ptmpBuffer on return
holds a pointer
'to our UDT.

'Deference it!!!
CopyMemory tmpBuffer, ptmpBuffer, LenB(tmpBuffer)

Dim sUser As String
Dim sByte() As Byte
ReDim sByte(255)

'Convert LPWSTR (Unicode string) to VB string.
CopyMemory sByte(0), tmpBuffer.usri3_full_name,
256
sUser = sByte
sUser = sUser & vbNullChar
MsgBox Trim$(sUser)
'Now I get my user name back, it's VB string now'
End Sub

Steve

unread,
Aug 28, 2001, 6:50:07 PM8/28/01
to
Sorry for the late reply - thanks for your help. Your example does work
well, and it has less code in it than Dev's. I guess its because Dev's can
do more than retrieve just the names.
One (of a few!) things I don't understand is why are so many variables
declared in defining the Type USER_INFO_3, when you're only going to use
some of them?
Regards
Steve

Justin Gilbert <JDGi...@mcleodusa.com> wrote in message
news:11f2301c12cca$dd21cb80$a4e62ecf@tkmsftngxa06...

Justin D. Gilbert

unread,
Aug 29, 2001, 7:55:13 PM8/29/01
to
Steve,

These are structures written in C or C++. The definitions expect a certain
amount of memory to be allocated for these. Although you may not use all of
them, the API will populate them. As a result, they need to be completed.

In this case, the API creates its own structure and returns a pointer.

Secondly, the items are positional. What that means is that when the record
is populated, they hold a specific place in memory. Each of the variables
declarations is an offset into that memory.

Here's an simple example. Consider the three fields. (note that I am not
doing byte swapping and this is only for illustration)

Type MyType
A As Long
B As Long
C As Long
End Type

This would be 'XXXXyyyyZZZZ' of memory.
A = XXXX
B = YYYY
C = ZZZZ

I put 1234 in A 5566 in B and 7788 in C.
If I dropped B from the type structure, and the API returned its proper
structure and I copied the entire structure, then 5566 would be put in
variable C instead of the expected 7788. In addition, the data would be put
into memory position YYYY instead of ZZZZ.

Because the structure being returned expects 12 bytes and you only have 8,
the copy would not have the memory location to put 7788 and may get an abend
or GPF.

Hope this helps.

Justin

"Steve" <stevebl...@openlink.org> wrote in message
news:OBPANPBMBHA.1932@tkmsftngp05...

Steve

unread,
Aug 30, 2001, 4:34:58 PM8/30/01
to
Justin D. Gilbert <gilb...@netins.net> wrote in message
news:OZ3bdcOMBHA.3076@tkmsftngp02...
> Steve,
<a large snip>
> Hope this helps.
>
> Justin

Thanks for that clear and concise explanation, Justin, it helped a lot.

Steve


0 new messages