// '' <summary> // '' Gets account info using an account name through the LookupAccountName function. // '' </summary> // '' <param name="Account">The name to use as a lookup parameter.</param> // '' <param name="System">The system to search for the SID, or blank if the account is not a local account.</param> // '' <param name="SID">A reference to a string that will contain the SID.</param> // '' <param name="Domain">A reference to a string that will contain the Domain.</param> // '' <param name="SIDUse">A reference to an integer that will contain the SID type.</param> // '' <returns></returns> // '' <remarks></remarks> public static int GetAccountInfoByName(string Account, string System, ref string SID, ref string Domain, ref int SIDUse) { int Result; // The buffer sizes can be adjusted if desired, but the return values will probably never exceed 255. // So this is a good, safe value. string AccountName = Account; IntPtr SIDPointer = new IntPtr(); int SIDPointerBufferSize = 255; IntPtr DomainNamePointer = new IntPtr(); int DomainNameBufferSize = 255; int Use = 0; // Since we're dealing with unmanaged code, we have to allocate memory for these manually. SIDPointer = Marshal.AllocHGlobal(SIDPointerBufferSize); DomainNamePointer = Marshal.AllocHGlobal(DomainNameBufferSize); // Calling the function, the return value will contain either 0 on failure, or non-zero on success. Result = SIDInfo.LookupAccountName(System, AccountName, SIDPointer, ref SIDPointerBufferSize, DomainNamePointer, ref DomainNameBufferSize, ref Use); // If the result is zero, it failed. Return the error code. if ((Result == 0)) { return(GetLastError()); } IntPtr SDDLString = new IntPtr(); // We got the SID in the form of a byte array. So we have to convert this to a string. This function returns // non-zero on success. Result = SIDInfo.ConvertSidToStringSid(SIDPointer, ref SDDLString); if ((Result == 0)) { return(GetLastError()); } // Getting the account and domain values from the pointers we sent to LookupAccountName. Account = AccountName; SID = Marshal.PtrToStringAnsi(SDDLString); Domain = Marshal.PtrToStringAnsi(DomainNamePointer); SIDUse = Use; // Free up the memory we used for the operation. Marshal.FreeHGlobal(SIDPointer); Marshal.FreeHGlobal(DomainNamePointer); SIDInfo.LocalFree(SDDLString.ToInt32()); return(0); }
// '' <summary> // '' Gets account info using an SID through the LookupAccountSid function. // '' </summary> // '' <param name="SID">The SID to use as a lookup parameter.</param> // '' <param name="System">The system to search for the SID, or blank if the SID is not for a local account.</param> // '' <param name="Account">A reference to a string that will contain the Account.</param> // '' <param name="Domain">A reference to a string that will contain the Domain.</param> // '' <param name="SIDUse">A reference to an integer that will contain the SID type.</param> // '' <returns></returns> // '' <remarks></remarks> public static int GetAccountInfoBySID(string SID, string System, ref string Account, ref string Domain, ref int SIDUse) { long Result; int SIDInteger; IntPtr SIDPointer = new IntPtr(); // Convert the string SID to a byte array. If this returns false, then the function failed, return last error. if (!SIDInfo.ConvertStringSidToSid(SID, ref SIDPointer)) { return(GetLastError()); } SIDInteger = SIDPointer.ToInt32(); // The buffer sizes can be adjusted if desired, but the return values will probably never exceed 255. // So this is a good, safe value. IntPtr AccountNamePointer = new IntPtr(); int AccountNameBufferSize = 255; IntPtr DomainNamePointer = new IntPtr(); int DomainNameBufferSize = 255; int SIDType = 0; // Since we're dealing with unmanaged code, we have to allocate memory for these manually. AccountNamePointer = Marshal.AllocHGlobal(AccountNameBufferSize); DomainNamePointer = Marshal.AllocHGlobal(DomainNameBufferSize); // Calling the function, the return value will contain either 0 on failure, or non-zero on success. Result = SIDInfo.LookupAccountSid(System, SIDInteger, AccountNamePointer, ref AccountNameBufferSize, DomainNamePointer, ref DomainNameBufferSize, ref SIDType); // If the result is zero, it failed. Return the error code. if ((Result == 0)) { return(GetLastError()); } // Getting the account and domain values from the pointers we sent to LookupAccountSid. Account = Marshal.PtrToStringAnsi(AccountNamePointer); Domain = Marshal.PtrToStringAnsi(DomainNamePointer); SIDUse = SIDType; // Free up the memory we used for the operation. Marshal.FreeHGlobal(AccountNamePointer); Marshal.FreeHGlobal(DomainNamePointer); SIDInfo.LocalFree(SIDPointer.ToInt32()); return(0); }