Win32 API function GetUserNameEx( ) - only partially working

forum.td.development (2005-2010)
James McCall

Win32 API function GetUserNameEx( ) - only partially working

Post by James McCall » 03 Dec 2008, 05:35

 Posted by:  James McCall 

I am trying to get the Win32 API function GetUserNameEx( ) to return the
user's unique ID (eg. {4fa050f0-f561-11cf-bdd9-00aa003a77b6}) but it always
returns NULL. My implementation works fine for returning the user's name in
the format: \ (enum NameSamCompatible, eg.
Engineering\JohnDoe). For all other values of the EXTENDED_NAME_FORMAT enum
it either:
- fails
- returns NULL
- tells me it needs exactly double the buffer size I allocated, no matter
what that size was

I even tried using the "W" version of the function, with the same result.

Has anybody got a working implementation of this API function?

Would anybody be kind enough to run my sample in their environment and tell
me if they get all values back? I will attach it as the first reply to this
post.

Thanks,
James McCall

James McCall

Win32 API function GetUserNameEx( ) - only partially working

Post by James McCall » 03 Dec 2008, 05:36

 Posted by:  James McCall 

Sample, as promised.
Written in CTD2000, saved as indented text

Attachment: ~Try_GetUserNameEx.zip

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 2813
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Win32 API function GetUserNameEx( ) - only partially working

Post by Dave Rabelink » 03 Dec 2008, 08:04

Hi James,

I tested your source on 2 workstations.

On one machine, not connected to a domain controller (a standalone
WinXP machine) I got the same results as you describe.

On the other machine (same OS and installation), connected to a LAN
using a domain controller I got the results you can see in the
screenshot.

Seems the info you need is only fetched when you have a connection to
a domain controller (LDAP?).

Dave


Attachment: Results1.gif

Rainer Ebert

Win32 API function GetUserNameEx( ) - only partially working

Post by Rainer Ebert » 03 Dec 2008, 13:32

 Posted by:  Rainer Ebert 

For me as member of a domain, the user ID ist retrieved, too.

Regards,
Rainer

James McCall

Win32 API function GetUserNameEx( ) - only partially working

Post by James McCall » 03 Dec 2008, 16:46

 Posted by:  James McCall 

Dave & Rainer,

Thanks very much for trying this out. I don't have easy access to a system
on a domain, at least not one that I am allowed to play around with.

I don't know why it doesn't give the user's unique ID on a standalone PC. I
was hoping to get the user's section in the HKEY_USERS part of the registry.
Back to the drawing board I guess.

James

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 2813
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Win32 API function GetUserNameEx( ) - only partially working

Post by Dave Rabelink » 04 Dec 2008, 00:23

James,

You could try using LookupAccountNameA from Advapi32.dll.
(See http://msdn.microsoft.com/en-us/library/aa379159(VS.85).aspx)

When GetUserNameEx fails when the machine is not on a domain, this
function can get the local SID for the account.
You can use the NameSamCompatible from GetUserNameEx as input for
LookupAccountNameA.

When the SID buffer is fetched, you can use ConvertSidToStringSidA to
get the SID as string.

Here a c++ sample
http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.kernel/2004-06/0390.html

Hope this helps.

Dave

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 2813
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Win32 API function GetUserNameEx( ) - only partially working

Post by Dave Rabelink » 04 Dec 2008, 00:47

 Posted by:  Dave Rabelink 

Well, it is late here but i could not resist ;)

I quickly hacked something together and it gets me my SID.
Change the user account in the sample to your accountname or fetch it
using GetUserName.

I did not check the correctness of the code, but it will give you a
start.

Dave


Attachment: GetSID.zip

James McCall

Win32 API function GetUserNameEx( ) - only partially working

Post by James McCall » 04 Dec 2008, 06:00

 Posted by:  James McCall 

Dave, you're a star. Thanks.

Return to “td.development”

Who is online

Users browsing this forum: [Ccbot] and 0 guests