Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Below is the code sample I used. It works in my application and in my environment. Should work in others. Sorry this was not posted sooner, was new to StackOverFlow. I hope this posts properly.</p> <pre><code>public class GetUserInfo{ private const int NAME_SIZE = 0x40; // Methods [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] private static extern bool ConvertSidToStringSid(IntPtr Sid, ref IntPtr StringSid); public string ConvertSidToStringSidNT(IntPtr Sid) { string ans = string.Empty; if (IsValidSid(Sid)) { ans = "S-1-"; SID_IDENTIFIER_AUTHORITY psia = (SID_IDENTIFIER_AUTHORITY) Marshal.PtrToStructure(GetSidIdentifierAuthority(Sid), typeof(SID_IDENTIFIER_AUTHORITY)); int num = Marshal.ReadInt16(GetSidSubAuthorityCount(Sid)); if ((psia.Value[0] != 0) &amp; (psia.Value[1] != 0)) { ans = ((ans + Conversion.Hex(psia.Value[0]) + Conversion.Hex(psia.Value[1]).PadLeft(2, '0')) + Conversion.Hex(psia.Value[2]).PadLeft(2, '0') + Conversion.Hex(psia.Value[3]).PadLeft(2, '0')) + Conversion.Hex(psia.Value[4]).PadLeft(2, '0') + Conversion.Hex(psia.Value[5]).PadLeft(2, '0'); } else { long top = psia.Value[5]; top += psia.Value[4] * 0x100; top += (psia.Value[3] * 0x100) * 0x100; ans = ans + ((top + (((psia.Value[2] * 0x100) * 0x100) * 0x100))).ToString(); } int VB$t_i4$L0 = num - 1; for (int i = 0; i &lt;= VB$t_i4$L0; i++) { ans = ans + "-" + Marshal.ReadInt32(GetSidSubAuthority(Sid, i)).ToString(); } } return ans; } public string GetFileOwner(string Path) { string MachineName; IntPtr OwnerSid; int peUse; IntPtr SD; string UserName; IntPtr VB$t_struct$N0; SE_OBJECT_TYPE ObjectType = SE_OBJECT_TYPE.SE_FILE_OBJECT; if (GetNamedSecurityInfo(ref Path, ObjectType, SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION, ref OwnerSid, ref VB$t_struct$N0, ref VB$t_struct$N0, ref VB$t_struct$N0, ref SD) != 0) { return "Error"; } Marshal.FreeHGlobal(SD); if (Path.StartsWith(@"\\")) { MachineName = Path.Split(new char[] { '\\' })[2]; } else { MachineName = ""; } int name_len = 0x40; int domain_len = 0x40; string name = Strings.Space(name_len); string domain_name = Strings.Space(domain_len); if (!LookupAccountSid(ref MachineName, OwnerSid, ref name, ref name_len, ref domain_name, ref domain_len, ref peUse)) { string SidString; if (Marshal.GetLastWin32Error() != 0x534) { return "Error"; } if (Environment.Version.Major == 4) { SidString = this.ConvertSidToStringSidNT(OwnerSid); } else { IntPtr StringPtr; if (!ConvertSidToStringSid(OwnerSid, ref StringPtr)) { return "Error"; } SidString = Marshal.PtrToStringAuto(StringPtr); Marshal.FreeHGlobal(StringPtr); } domain_len = 0; name = SidString; name_len = Strings.Len(name); } if (domain_len &gt; 0) { UserName = Strings.Left(name, name_len); } else { UserName = Strings.Left(name, name_len); } return UserName; } [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] private static extern int GetNamedSecurityInfo([MarshalAs(UnmanagedType.VBByRefStr)] ref string pObjectName, SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo, ref IntPtr ppsidOwner, ref IntPtr ppsidGroup, ref IntPtr ppDacl, ref IntPtr ppSacl, ref IntPtr ppSecurityDescriptor); [DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] private static extern IntPtr GetSidIdentifierAuthority(IntPtr pSid); [DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] private static extern IntPtr GetSidSubAuthority(IntPtr pSid, int nSubAuthority); [DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] private static extern IntPtr GetSidSubAuthorityCount(IntPtr pSid); [DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] private static extern bool IsValidSid(IntPtr pSid); [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] private static extern bool LookupAccountSid([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpSystemName, IntPtr lpSid, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpName, ref int cchName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpReferenceDomainName, ref int cchReferencedDomainName, ref int peUse); // Nested Types private enum SE_OBJECT_TYPE { SE_UNKNOWN_OBJECT_TYPE, SE_FILE_OBJECT, SE_SERVICE, SE_PRINTER, SE_REGISTRY_KEY, SE_LMSHARE, SE_KERNEL_OBJECT, SE_WINDOW_OBJECT, SE_DS_OBJECT, SE_DS_OBJECT_ALL, SE_PROVIDER_DEFINED_OBJECT, SE_WMIGUID_OBJECT, SE_REGISTRY_WOW64_32 } private enum SECURITY_INFORMATION { DACL_SECURITY_INFORMATION = 4, GROUP_SECURITY_INFORMATION = 2, OWNER_SECURITY_INFORMATION = 1, PROTECTED_DACL_SECURITY_INFORMATION = 0x20, PROTECTED_SACL_SECURITY_INFORMATION = 0x10, SACL_SECURITY_INFORMATION = 8, UNPROTECTED_DACL_SECURITY_INFORMATION = 0x80, UNPROTECTED_SACL_SECURITY_INFORMATION = 0x40 } [StructLayout(LayoutKind.Sequential)] private struct SID_IDENTIFIER_AUTHORITY { [MarshalAs(UnmanagedType.ByValArray, SizeConst=6)] public byte[] Value; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload