Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting size of TOKEN_PRIVILEGES.LUID_AND_ATTRIBUTES array returned by GetTokenInformation
    primarykey
    data
    text
    <p>I'm trying to retrieve the privileges and their current state associated with a token in C# but i can't figure out how to adjust the size of the <code>LUID_AND_ATTRIBUTES</code> array that is returned to fit the actual number of elements.</p> <p>From <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype%28v=VS.71%29.aspx" rel="nofollow">MSDN</a></p> <blockquote> <p>When MarshalAsAttribute.Value is set to ByValArray, the SizeConst must be set to indicate the number of elements in the array.</p> </blockquote> <p>I was able to watch the <code>TOKEN_PRIVILEGES.PrivilegeCount</code> property after the call to <code>GetTokenInformation</code> and see that the token I was working with had 24 of the 35 privileges listed on the <a href="http://msdn.microsoft.com/en-us/library/bb530716%28v=VS.85%29.aspx" rel="nofollow">Privilege Constants</a> reference page. Changing <code>SizeConst = 24</code> would then give me the ability to see all of them instead of just the first one (I had initially set <code>SizeConst = 1</code> following a usage example from <a href="http://www.pinvoke.net/default.aspx/Structures/TOKEN_PRIVILEGES.html" rel="nofollow">PInvoke</a>)</p> <p>Is there a way to specify the depth of the incoming array as it is being created or will I need to know how many privileges there are going to be before writing the code?</p> <h2>Code Snippet</h2> <pre><code>[DllImport("advapi32.dll", SetLastError = true)] protected static extern bool GetTokenInformation(IntPtr TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, IntPtr TokenInformation, int TokenInformationLength, ref int ReturnLength); [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)][return: MarshalAs(UnmanagedType.Bool)] protected static extern bool LookupPrivilegeName(string lpSystemName, IntPtr lpLuid,System.Text.StringBuilder lpName, ref int cchName); protected struct TOKEN_PRIVILEGES { public UInt32 PrivilegeCount; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public LUID_AND_ATTRIBUTES[] Privileges; }//TOKEN_PRIVILEGES [StructLayout(LayoutKind.Sequential)] protected struct LUID_AND_ATTRIBUTES { public LUID Luid; public UInt32 Attributes; }//LUID_AND_ATTRIBUTES [StructLayout(LayoutKind.Sequential)] protected struct LUID { public uint LowPart; public int HighPart; }//LUID int TokenInfLength = 0; IntPtr ThisHandle = WindowsIdentity.GetCurrent().Token; GetTokenInformation(ThisHandle, TOKEN_INFORMATION_CLASS.TokenPrivileges, IntPtr.Zero, TokenInfLength, ref TokenInfLength); //Get the TokenInformation length (returns false) IntPtr TokenInformation = Marshal.AllocHGlobal(TokenInfLength); if(GetTokenInformation(WindowsIdentity.GetCurrent().Token, TOKEN_INFORMATION_CLASS.TokenPrivileges, TokenInformation, TokenInfLength, ref TokenInfLength)){ TOKEN_PRIVILEGES ThisPrivilegeSet = (TOKEN_PRIVILEGES)Marshal.PtrToStructure(TokenInformation, typeof(TOKEN_PRIVILEGES)); //ThisPrivilegeSet now holds all of the LUID's i need to check out foreach(LUID_AND_ATTRIBUTES laa in ThisPrivilegeSet.Privileges){ //ThisPrivilegeSet.Privileges is only as deep as SizeConst will allow System.Text.StringBuilder StrBuilder = new System.Text.StringBuilder(); int LuidNameLen = 0; IntPtr LuidPointer = Marshal.AllocHGlobal(Marshal.SizeOf(laa.Luid)); Marshal.StructureToPtr(laa.Luid, LuidPointer, true); LookupPrivilegeName(null, LuidPointer, null, ref LuidNameLen); //Get the PrivilageName length (returns false) StrBuilder.EnsureCapacity(LuidNameLen + 1); if(LookupPrivilegeName(null, LuidPointer, StrBuilder, ref LuidNameLen)){ //StrBuilder gets the name this time Console.WriteLine("[{0}] : {1}", laa.Attributes.ToString(), StrBuilder.ToString()); }//end if Marshal.FreeHGlobal(LuidPointer); }//next }//end if </code></pre> <p>This is my first post, so sorry if I did it wrong and TIA for the help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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