adjusttokenprivileges (advapi32)
Last changed: -91.119.3.204

.
Summary
TODO - a short description

C# Signature:

[DllImport("advapi32.dll")]
static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
   bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, UInt32 BufferLength,
   IntPtr PreviousState, IntPtr ReturnLength);

VB Signature:

Declare Function AdjustTokenPrivileges Lib "advapi32.dll" ByVal TokenHandle As IntPtr, _
    ByVal DisableAllPrivileges As Boolean, _
    ByRef NewState As TOKEN_PRIVILEGES, _
    ByVal BufferLength As Integer, _
    ByVal PreviousState As IntPtr, _
    ByVal ReturnLength As IntPtr) As Boolean

User-Defined Types:

TOKEN_PRIVILEGES

    <StructLayout(LayoutKind.Sequential, Pack:=4)> _
    Private Structure LUID_AND_ATTRIBUTES
    Dim Luid As Long
    Dim Attributes As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=4)> _
    Private Structure TOKEN_PRIVILEGES
    Dim PrivilegeCount As Integer
    Dim Privilege As LUID_AND_ATTRIBUTES
    End Structure

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    Dim hProc, hToken As IntPtr
    Dim luid_Restore, As Long
    Dim tp As New TOKEN_PRIVILEGES

    ' get the current process's token
    hProc = Process.GetCurrentProcess().Handle
    hToken = IntPtr.Zero
    If Not OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) Then
        Return False
    End If

    ' get the LUID for the Restore privilege (provided it already exist)
    luid_Restore = 0
    If Not LookupPrivilegeValue(Nothing, SE_RESTORE_NAME, luid_Restore) Then
        Return False
    End If

    tp.PrivilegeCount = 1
    tp.Privilege.Luid = luid_Restore
    tp.Privilege.Attributes = SE_PRIVILEGE_ENABLED

    ' enable the privileges
    If Not AdjustTokenPrivileges(hToken, False, tp, 0, IntPtr.Zero, IntPtr.Zero) Then
        Return False
    End If

    Return True

Alternative Managed API:

Do you know one? Please contribute it!

Documentation