private bool SetOwner(string strUserName) { bool res = false; // Do we have the required Privilege? if (SetPrivileges()) { int sid_len = NativeMethods.SID_SIZE; IntPtr pNewOwner = Marshal.AllocHGlobal(sid_len); int domain_len = DOMAIN_NAME_SIZE; string domain_name = new string(' ', domain_len + 1); NativeMethods.SID_NAME_USE deUse = NativeMethods.SID_NAME_USE.SidTypeUndefined; res = (NativeMethods.LookupAccountName(null, strUserName, pNewOwner, ref sid_len, domain_name, ref domain_len, ref deUse) != 0); if (res) { #if DEBUG domain_name = domain_name.Substring(0, domain_len); #endif int r = NativeMethods.SetNamedSecurityInfo(path, NativeMethods.SE_OBJECT_TYPE.SE_FILE_OBJECT, NativeMethods.OWNER_SECURITY_INFORMATION, pNewOwner, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); if (r != NativeMethods.ERROR_SUCCESS) { res = false; opError.Append(ERROR_MESSAGE_DELIMITER + "SetNamedSecurityInfo:" + GetLastErrorString(r)); } } else { opError.Append(ERROR_MESSAGE_DELIMITER + "LookupAccountName:" + GetLastErrorString()); } Marshal.FreeHGlobal(pNewOwner); } return(res); }
public string ConvertStringSidToAccountName(string sid, out string domain) { NativeMethods.SID_NAME_USE sIDNAMEUSE = 0; int lastWin32Error = 0; IntPtr intPtr = new IntPtr(0); byte[] numArray = null; try { if (NativeMethods.ConvertStringSidToSid(sid, out intPtr)) { int lengthSid = NativeMethods.GetLengthSid(intPtr); numArray = new byte[lengthSid]; Marshal.Copy(intPtr, numArray, 0, lengthSid); } else { lastWin32Error = Marshal.GetLastWin32Error(); object[] systemErrorMessage = new object[1]; systemErrorMessage[0] = ActiveDirectoryHelper.GetSystemErrorMessage(lastWin32Error); throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.SidToAccountConvertError, systemErrorMessage)); } } finally { NativeMethods.LocalFree(intPtr); } StringBuilder stringBuilder = new StringBuilder(); int capacity = stringBuilder.Capacity; StringBuilder stringBuilder1 = new StringBuilder(); int num = stringBuilder1.Capacity; if (!NativeMethods.LookupAccountSid(null, numArray, stringBuilder, ref capacity, stringBuilder1, ref num, out sIDNAMEUSE)) { lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error == 122) { stringBuilder.EnsureCapacity(capacity); stringBuilder1.EnsureCapacity(num); lastWin32Error = 0; if (!NativeMethods.LookupAccountSid(null, numArray, stringBuilder, ref capacity, stringBuilder1, ref num, out sIDNAMEUSE)) { lastWin32Error = Marshal.GetLastWin32Error(); } } } if (lastWin32Error != 0) { object[] objArray = new object[1]; objArray[0] = ActiveDirectoryHelper.GetSystemErrorMessage(lastWin32Error); throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.SidToAccountConvertError, objArray)); } else { if (!sid.StartsWith(BUILTIN_DOMAIN_SID_PREFIX, StringComparison.OrdinalIgnoreCase)) { domain = stringBuilder1.ToString(); } else { domain = Environment.MachineName; } return(stringBuilder.ToString()); } }
public static extern bool LookupAccountSid(string lpSystemName, byte[] Sid, StringBuilder lpName, ref int cchName, StringBuilder ReferencedDomainName, ref int cchReferencedDomainName, out NativeMethods.SID_NAME_USE peUse);