Check out the formatting tips on the right for help formatting and making links.
Use the template below then delete this header:
[DllImport( "advapi32.dll", CharSet=CharSet.Auto, SetLastError=true, PreserveSig=true)]
private static extern bool LookupAccountName(
string lpSystemName,
string lpAccountName,
IntPtr psid,
ref int cbsid,
StringBuilder domainName, //StringBuilder or string?
ref int cbdomainLength,
ref int use );
Declare Function LookupAccountName Lib "advapi32.dll" (TODO) As TODO
None.
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.
Please add some!
using System;
using System.Runtime.InteropServices;
using System.Text;
class Test
{
static void Main() {
string strSid = GetSIDForAccount("Administrator");
Console.WriteLine( strSid );
}
public static string GetSIDForAccount(string Account) {
long winErrorCode = 0; //contains the last error
//pointer and size for the SID
IntPtr pSid = IntPtr.Zero;
int iSidSize = 0;
//StringBuilder for Domain
StringBuilder sbDomain = new StringBuilder();
int iDomainSize = 0;
//account-type variable for lookup
int iAccountType = 0;
//get required buffer size (iSidSize=0 causes this...)
LookupAccountName(String.Empty, Account, pSid, ref iSidSize,
sbDomain, ref iDomainSize, ref iAccountType);
//allocate buffers
sbDomain = new StringBuilder(iDomainSize);
pSid = Marshal.AllocHGlobal(iSidSize);
//lookup the SID for the account
bool bResult = LookupAccountName(String.Empty,
Account,
pSid,
ref iSidSize,
sbDomain,
ref iDomainSize,
ref iAccountType);
if (!bResult) {
winErrorCode = GetLastError();
Console.WriteLine("LookupAccountName failed: "+ winErrorCode);
FreeSid(pSid);
throw new ApplicationException("Could not read SID");
}
string strSID = "";
if ( ConvertSidToStringSid( pSid, ref strSID)) {
FreeSid(pSid);
return strSID;
} else {
throw new ApplicationException("Could not convert SID");
}
}
[DllImport("kernel32.dll")]
private static extern int GetLastError();
[DllImport( "advapi32.dll", CharSet=CharSet.Auto, SetLastError=true, PreserveSig=true)]
private static extern bool LookupAccountName(
string lpSystemName,
string lpAccountName,
IntPtr psid,
ref int cbsid,
StringBuilder domainName, //StringBuilder or string?
ref int cbdomainLength,
ref int use );
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
private static extern bool ConvertSidToStringSid(
IntPtr pSID,
[In,Out,MarshalAs(UnmanagedType.LPTStr)] ref string strSID);
[DllImport("advapi32")]
private static extern void FreeSid(IntPtr pSid);
}
TODO