Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than advapi32, prefix the name with the module name and a period.
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Shared Function LookupAccountName(lpSystemName As String, lpAccountName As String, <MarshalAs(UnmanagedType.LPArray)> Sid As Byte(), ByRef cbSid As UInteger, ReferencedDomainName As StringBuilder, ByRef cchReferencedDomainName As UInteger, peUse As SID_NAME_USE) As Boolean
End Function
Private Declare Auto Function LookupAccountName Lib "advapi32.dll" ( _
ByVal lpSystemName As String, _
ByVal lpAccountName As String, _
ByVal Sid As IntPtr, _
ByRef cbSid As Integer, _
ByVal lpReferenceDomainName As String, _
ByRef cchReferencedDomainName As Integer, _
ByRef peUse As Integer _
) As Boolean
User-Defined Types:
None.
Notes:
From the SDK:
The LookupAccountName function attempts to find a SID for the specified name by first checking a list of well-known SIDs. If the name does not correspond to a well-known SID, the function checks built-in and administratively defined local accounts. Next, the function checks the primary domain. If the name is not found there, trusted domains are checked.
Use fully qualified account names (for example, domain_name\user_name) instead of isolated names (for example, user_name). Fully qualified names are unambiguous and provide better performance when the lookup is performed. This function also supports fully qualified DNS names (for example, example.example.com\user_name) and user principal names (UPN) (for example, someone@example.com).
Windows NT: DNS and UPN names are not supported by this function.
In addition to looking up local accounts, local domain accounts, and explicitly trusted domain accounts, LookupAccountName can look up the name for any account in any domain in the forest.
Windows NT: Forest lookup is not supported.
Tips & Tricks:
Please add some!
C# Sample Code:
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace test
{
class clsLookupAccountName
{
const int NO_ERROR = 0;
const int ERROR_INSUFFICIENT_BUFFER = 122;
const int ERROR_INVALID_FLAGS = 1004; // On Windows Server 2003 this error is/can be returned, but processing can still continue
int err = NO_ERROR;
if (!LookupAccountName(null,accountName,Sid,ref cbSid,referencedDomainName,ref cchReferencedDomainName,out sidUse))
{
err = Marshal.GetLastWin32Error();
if (err == ERROR_INSUFFICIENT_BUFFER || err == ERROR_INVALID_FLAGS)
if (err == ERROR_INSUFFICIENT_BUFFER)
{
Sid = new byte[cbSid];
referencedDomainName.EnsureCapacity((int)cchReferencedDomainName);
err = NO_ERROR;
if (!LookupAccountName(null,accountName,Sid,ref cbSid,referencedDomainName,ref cchReferencedDomainName,out sidUse))
err = Marshal.GetLastWin32Error();
}
}
else
{
// Consider throwing an exception since no result was found
}
if (err == 0)
{
IntPtr ptrSid;
if (!ConvertSidToStringSid(Sid,out ptrSid))
{
err = Marshal.GetLastWin32Error();
Console.WriteLine(@"Could not convert sid to string. Error : {0}",err);
}
else
{
string sidString = Marshal.PtrToStringAuto(ptrSid);
LocalFree(ptrSid);
Console.WriteLine(@"Found sid {0} : {1}",sidUse,sidString);
}
}
else
Console.WriteLine(@"Error : {0}",err);
}
}
}
VB.Net Sample Code:
Dim ret As Boolean
Dim ptrSid As IntPtr
Dim cbSid As Integer
Dim ptrSidString As IntPtr
Dim SidString As String
Dim refDomainName As New StringBuilder
Dim cbRefDomainName As Integer
Dim peUse As SID_USE
'First get the buffer sizes
ret = LookupAccountName(Nothing, UserName, Nothing, cbSid, Nothing, cbRefDomainName, peUse)
'Adjust the buffers to the needed size
ptrSid = Marshal.AllocHGlobal(cbSid)
refDomainName.EnsureCapacity(cbRefDomainName)
'Get the data again, now with the changed buffers
ret = LookupAccountName(Nothing, UserName, ptrSid, cbSid, refDomainName, cbRefDomainName, peUse)
If ret Then
'If return doesn't fail (ret=true) then convert the buffer data at ptrSid to the SidString)
If ConvertSidToStringSid(ptrSid, ptrSidString) Then
'Since ptrSidString is a memory pointer we need to make it a string
Sid = Marshal.PtrToStringAuto(ptrSidString)
msgBox(Sid)
'Finally free up the memory
LocalFree(ptrSidString)
End If
End If
Marshal.FreeHGlobal(ptrSid)
Alternative Managed API:
TODO
The LookupAccountName function accepts the name of a system and an account as input. It retrieves a security identifier (SID) for the account and the name of the domain on which the account was found.
11/20/2010 8:27:50 AM - Patrick-65.73.255.18
The LookupAccountName function accepts the name of a system and an account as input. It retrieves a security identifier (SID) for the account and the name of the domain on which the account was found.
11/20/2010 8:27:50 AM - Patrick-65.73.255.18
The SID_IDENTIFIER_AUTHORITY structure represents the top-level authority of a security identifier (SID).
5/28/2007 2:31:00 PM - -209.87.228.158
The LookupAccountName function accepts the name of a system and an account as input. It retrieves a security identifier (SID) for the account and the name of the domain on which the account was found.
11/20/2010 8:27:50 AM - Patrick-65.73.255.18
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).