Dennis C Dennis C - 1 year ago 90
C# Question

The best way to resolve display username by SID?

I read a list of SIDs from the registry,

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

How would one resolve the display username (e.g.
) given the SID string in C#?

Answer Source

The Win32 API function LookupAccountSid() is used to find the name that corresponds to a SID.

LookupAccountSid() has the following signature:

BOOL LookupAccountSid(LPCTSTR lpSystemName, PSID Sid,LPTSTR Name, LPDWORD cbName,
                       LPTSTR ReferencedDomainName, LPDWORD cbReferencedDomainName,
                       PSID_NAME_USE peUse);


Here's the P/Invoke reference (with sample code):

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError = true)]
static extern bool LookupAccountSid (
  string lpSystemName,
  [MarshalAs(UnmanagedType.LPArray)] byte[] Sid,
  StringBuilder lpName,
  ref uint cchName,
  StringBuilder ReferencedDomainName,
  ref uint cchReferencedDomainName,
  out SID_NAME_USE peUse); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download