// Use this signature if you want the previous state information returned
[DllImport("advapi32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
[MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState,
UInt32 BufferLengthInBytes,
ref TOKEN_PRIVILEGES PreviousState,
out UInt32 ReturnLengthInBytes);
// Use this signature if you do not want the previous state
[DllImport("advapi32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
[MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState,
UInt32 Zero,
IntPtr Null1,
IntPtr Null2);
' Use this signature if you want the previous state information returned
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Function AdjustTokenPrivileges( _
ByVal TokenHandle As IntPtr, _
ByVal DisableAllPrivileges As Boolean, _
ByRef NewState As TOKEN_PRIVILEGES, _
ByVal BufferLengthInBytes As Integer, _
ByRef PreviousState As TOKEN_PRIVILEGES, _
ByRef ReturnLengthInBytes As Integer _
) As Boolean
End Function
' Use this signature if you do not want the previous state
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Function AdjustTokenPrivileges( _
ByVal TokenHandle As IntPtr, _
ByVal DisableAllPrivileges As Boolean, _
ByRef NewState As TOKEN_PRIVILEGES, _
ByVal Zero As Integer, _
ByVal Null1 As IntPtr, _
ByVal Null2 As IntPtr _
) As Boolean
End Function
(alternate/older style)
Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (
ByVal TokenHandle As IntPtr, _
ByVal DisableAllPrivileges As Boolean, _
ByRef NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Integer, _
ByRef PreviousState As TOKEN_PRIVILEGES, _
ByRef ReturnLength As IntPtr _
) As Boolean
...and you may also need
None.
If SetLastError is set to true, get the error with this.
int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
Console.Error.WriteLine("NativeErr: " + lastError);
Then use Error lookup tool for troubleshooting it can be downloaded from MS, ''Err.exe'' I think...
//This snippet is tested on WinXP and Vista, only needed in Vista when using SetTimeZoneInformation
Public Class AdjPriv()
{
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr
phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name,
ref long pluid);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_TIME_ZONE_NAMETEXT = "SeTimeZonePrivilege"; //http://msdn.microsoft.com/en-us/library/bb530716(VS.85).aspx
private bool SetPriv()
{
try
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
retVal = LookupPrivilegeValue(null, SE_TIME_ZONE_NAMETEXT, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
return retVal;
}
catch (Exception ex)
{
throw;
return false;
}
}
}
'This routine enables the Shutdown privilege for the current process,
'which is necessary if you want to call ExitWindowsEx.
Const ANYSIZE_ARRAY As Integer = 1
Const TOKEN_QUERY As Integer = &H8
Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Const SE_PRIVILEGE_ENABLED As Integer = &H2
<StructLayout(LayoutKind.Sequential)> _
Private Structure LUID
Public LowPart As UInt32
Public HighPart As UInt32
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure LUID_AND_ATTRIBUTES
Public Luid As LUID
Public Attributes As UInt32
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure TOKEN_PRIVILEGES
Public PrivilegeCount As UInt32
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=ANYSIZE_ARRAY)> _
Public Privileges() As LUID_AND_ATTRIBUTES
End Structure
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Function LookupPrivilegeValue( _
ByVal lpSystemName As String, _
ByVal lpName As String, _
ByRef lpLuid As LUID _
) As Boolean
End Function
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Function OpenProcessToken( _
ByVal ProcessHandle As IntPtr, _
ByVal DesiredAccess As Integer, _
ByRef TokenHandle As IntPtr _
) As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Function CloseHandle(ByVal hHandle As IntPtr) As Boolean
End Function
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Function AdjustTokenPrivileges( _
ByVal TokenHandle As IntPtr, _
ByVal DisableAllPrivileges As Boolean, _
ByRef NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Integer, _
ByRef PreviousState As TOKEN_PRIVILEGES, _
ByRef ReturnLength As IntPtr _
) As Boolean
End Function
Public Sub AcquireShutdownPrivilege()
Dim lastWin32Error As Integer = 0
'Get the LUID that corresponds to the Shutdown privilege, if it exists.
Dim luid_Shutdown As LUID
If Not LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, luid_Shutdown) Then
lastWin32Error = Marshal.GetLastWin32Error()
Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
"LookupPrivilegeValue failed with error " & lastWin32Error.ToString & ".")
End If
'Get the current process's token.
Dim hProc As IntPtr = Process.GetCurrentProcess().Handle
Dim hToken As IntPtr
If Not OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) Then
lastWin32Error = Marshal.GetLastWin32Error()
Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
"OpenProcessToken failed with error " & lastWin32Error.ToString & ".")
End If
Try
'Set up a LUID_AND_ATTRIBUTES structure containing the Shutdown privilege, marked as enabled.
Dim luaAttr As New LUID_AND_ATTRIBUTES
luaAttr.Luid = luid_Shutdown
luaAttr.Attributes = SE_PRIVILEGE_ENABLED
'Set up a TOKEN_PRIVILEGES structure containing only the shutdown privilege.
Dim newState As New TOKEN_PRIVILEGES
newState.PrivilegeCount = 1
newState.Privileges = New LUID_AND_ATTRIBUTES() {luaAttr}
'Set up a TOKEN_PRIVILEGES structure for the returned (modified) privileges.
Dim prevState As TOKEN_PRIVILEGES = New TOKEN_PRIVILEGES
ReDim prevState.Privileges(CInt(newState.PrivilegeCount))
'Apply the TOKEN_PRIVILEGES structure to the current process's token.
Dim returnLength As IntPtr
If Not AdjustTokenPrivileges(hToken, False, newState, Marshal.SizeOf(prevState), prevState, returnLength) Then
lastWin32Error = Marshal.GetLastWin32Error()
Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
"AdjustTokenPrivileges failed with error " & lastWin32Error.ToString & ".")
End If
Finally
CloseHandle(hToken)
End Try
End Sub
Do you know one? Please contribute it!