Global Const $tagLUID = 'dword LowPart;long HighPart'
Func _WinAPI_LookupPrivilegeValue($sPrivilege)
$tLUID = DllStructCreate($tagLUID)
Local $Ret = DllCall('advapi32.dll', 'int', 'LookupPrivilegeValueW', 'ptr', 0, 'wstr', $sPrivilege, 'ptr', DllStructGetPtr($tLUID))
If (@error) Or ($Ret[0] = 0) Then
Return SetError(1, 0, 0)
EndIf
Return $tLUID
EndFunc
Func _WinAPI_LookupPrivilegeName($tLUID)
$tData = DllStructCreate('wchar[128]')
Local $Ret = DllCall('advapi32.dll', 'int', 'LookupPrivilegeNameW', 'ptr', 0, 'ptr', DllStructGetPtr($tLUID), 'ptr', DllStructGetPtr($tData), 'dword*', 128)
If (@error) Or ($Ret[0] = 0) Then
Return SetError(1, 0, '')
EndIf
Return DllStructGetData($tData, 1)
EndFunc
Func _WinAPI_OpenProcessToken($iAccess, $hProcess = 0)
If Not $hProcess Then
$hProcess = _WinAPI_GetCurrentProcess()
EndIf
Local $Ret = DllCall('advapi32.dll', 'int', 'OpenProcessToken', 'ptr', $hProcess, 'dword', $iAccess, 'ptr*', 0)
If (@error) Or ($Ret[0] = 0) Then
Return SetError(1, 0, 0)
EndIf
Return $Ret[3]
EndFunc
Func _WinAPI_AdjustTokenPrivileges($hToken, $aPrivileges, $iState)
Switch $iState
Case 0, 1, 2
Case Else
Return SetError(1, 0, 0)
EndSwitch
Local $tLUID, $tPrivileges = 0, $tPrev = 0, $iPrivileges = $aPrivileges, $Global = 0, $Result = 1
Local $Struct = 'dword;dword;long;dword'
If $aPrivileges = -1 Then
$Global = 1
Else
If Not IsArray($aPrivileges) Then
Dim $aPrivileges[1][2] = [[$iPrivileges, $iState]]
$tPrev = DllStructCreate($Struct)
If @error Then
Return SetError(1, 0, 0)
EndIf
Else
If Not UBound($aPrivileges, 2) Then
Dim $aPrivileges[UBound($iPrivileges)][2]
For $i = 0 To UBound($iPrivileges) - 1
$aPrivileges[$i][0] = $iPrivileges[$i]
$aPrivileges[$i][1] = $iState
Next
EndIf
EndIf
For $i = 1 To UBound($aPrivileges) - 1
$Struct &= ';dword;long;dword'
Next
$tPrivileges = DllStructCreate($Struct)
If @error Then
Return SetError(1, 0, 0)
EndIf
DllStructSetData($tPrivileges, 1, UBound($aPrivileges))
For $i = 0 To UBound($aPrivileges) - 1
$tLUID = _WinAPI_LookupPrivilegeValue($aPrivileges[$i][0])
If @error Then
Return SetError(1, 0, 0)
EndIf
DllStructSetData($tPrivileges, 3 * $i + 2, DllStructGetData($tLUID, 1))
DllStructSetData($tPrivileges, 3 * $i + 3, DllStructGetData($tLUID, 2))
DllStructSetData($tPrivileges, 3 * $i + 4, $aPrivileges[$i][1])
Next
EndIf
Local $Ret = DllCall('advapi32.dll', 'int', 'AdjustTokenPrivileges', 'ptr', $hToken, 'int', $Global, 'ptr', DllStructGetPtr($tPrivileges), 'dword', DllStructGetSize($tPrev), 'ptr', DllStructGetPtr($tPrev), 'dword*', 0)
If (@error) Or ($Ret[0] = 0) Then
Return SetError(1, 0, 0)
EndIf
If IsDllStruct($tPrev) Then
$Result = DllStructGetData($tPrev, 4)
EndIf
Return SetError(0, _WinAPI_GetLastError(), $Result)
EndFunc