Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
GetTokenInformation (advapi32)
Retrieves a specified type of information about an access token

C# Signature:

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool GetTokenInformation(
    IntPtr TokenHandle,
    TOKEN_INFORMATION_CLASS TokenInformationClass,
    IntPtr TokenInformation,
    uint TokenInformationLength,
    out uint ReturnLength);

VB.NET Definition:

    Public Declare Function GetTokenInformation Lib "advapi32.dll" ( _
    ByVal TokenHandle As IntPtr, ByVal TokenInformationClass As TOKEN_INFORMATION_CLASS, _
    ByVal TokenInformation As IntPtr, ByVal TokenInformationLength As System.UInt32, _
    ByRef ReturnLength As System.UInt32) As Boolean

User-Defined Types:



Call once with zero for the third and fourth parameters to obtain the required size, then allocate the buffer and call again supplying these parameters.

Tips & Tricks:

Please add some!

Sample Code:

// Prints out sid of current user

using System;

using System.Runtime.InteropServices;

using System.Security.Principal;

using System.Text;

namespace test


    class clsLookupAccountName

            TokenUser = 1,

        public struct TOKEN_USER
            public SID_AND_ATTRIBUTES User ;

        public struct SID_AND_ATTRIBUTES

            public IntPtr Sid ;
            public int Attributes ;

        // Using IntPtr for pSID insted of Byte[]
        [DllImport("advapi32", CharSet=CharSet.Auto, SetLastError=true)]
        static extern bool ConvertSidToStringSid(
            IntPtr pSID,
            out IntPtr ptrSid);

        static extern IntPtr LocalFree(IntPtr hMem);

        [DllImport("advapi32.dll", SetLastError=true)]
        static extern bool GetTokenInformation(
            IntPtr TokenHandle,
            TOKEN_INFORMATION_CLASS TokenInformationClass,
            IntPtr TokenInformation,
            int TokenInformationLength,
            out int ReturnLength);

        static void Main(string[] args)
            int TokenInfLength = 0 ;
            bool Result ;

            // first call gets lenght of TokenInformation
            Result = GetTokenInformation( WindowsIdentity.GetCurrent().Token , TOKEN_INFORMATION_CLASS.TokenUser , IntPtr.Zero , TokenInfLength , out TokenInfLength );

            IntPtr TokenInformation = Marshal.AllocHGlobal( TokenInfLength ) ;

            Result = GetTokenInformation( WindowsIdentity.GetCurrent().Token  , TOKEN_INFORMATION_CLASS.TokenUser , TokenInformation , TokenInfLength , out TokenInfLength ) ;

            if( Result )
                TOKEN_USER TokenUser = ( TOKEN_USER )Marshal.PtrToStructure( TokenInformation , typeof( TOKEN_USER ) ) ;

                IntPtr pstr = IntPtr.Zero;
                Boolean ok = ConvertSidToStringSid( TokenUser.User.Sid  , out pstr );
                string sidstr = Marshal.PtrToStringAuto( pstr );
                Console.WriteLine(@"Found sid {0}",sidstr);

            Marshal.FreeHGlobal( TokenInformation );



There's a sample based on this at that gets the LogonSID instead.

Alternative Managed API:

Do you know one? Please contribute it!


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version