Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


adjustTokenPrivileges problems

Posted on 1999-07-19
Medium Priority
Last Modified: 2013-12-03
     Hi. I'm trying to kick-off a reboot under NT, and this code doesn't seem to work:

procedure tUpgrader._reboot;
  pri        : _token_privileges;
  b          : boolean;
  hToken     : cardinal;
  tiSize     : cardinal; //ti = token information
  hPrivilege : int64;
  r          : integer;
  procedure fatalErr (dc : string);
  var sE : string; iE : integer;
    iE := getLastError;
    if iE<>0 then sE:=sysErrorMessage (iE);
    raise eUpgrader.create (etVisible, 'tUpgrader._reboot', sE, iE, dc);
  if OSVersion.dwPlatformId=VER_PLATFORM_WIN32_NT then begin
  (* from the MSDN help for 'exitWindowsEx':
       Windows NT/2000: To shut down or restart the system, the calling process
       must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME
       privilege. For more information about security privileges, see Privileges.*)
  //GRRRRR!!! ;-)

  //get current privileges
    if not lookupPrivilegeValue (nil, 'SeShutdownPrivilege', hPrivilege)
      then fatalErr ('Can''t lookup privilege value for SE_SHUTDOWN_NAME');

    if not openProcessToken (getCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
      then fatalErr ('Can''t open process'' token.');

    pri.PrivilegeCount := 1;
    tiSize:=sizeOf (pri);

    adjustTokenPrivileges (hToken, false, pri, tiSize, oldPri, tiSize);
    if (r<>0) and (r<>ERROR_NOT_ALL_ASSIGNED) then fatalErr ('Couldn''t adjust token privileges.');

  b:=exitWindowsEx (EWX_REBOOT or EWX_FORCE,0);
  if not b then begin
  //hmm, the end-user's log-in-account doesn't have administrator rights to
  //actually force a reboot. Better pop up a window telling them that a manual
  //reboot might be necessary.
    b:=exitWindowsEx (EWX_REBOOT, 0);
  if b
    then ('forced reboot, NOW.')
    else begin ('Couldn''t reboot, getLastError returns : '+
                   sysErrorMessage (getLastError));
      messageDlg ('You will have to reboot manually.', mtWarning, [mbOK],0);

This code runs just fine, but adjustTokenPrivileges will return with 1300. GetLastError returns 'Not all privileges referenced are assigned to the caller'.

The help on adjustTokenPrivileges says

The AdjustTokenPrivileges function cannot add new privileges to the access token. It can only enable or disable the token's existing privileges. To determine the token's privileges, call the GetTokenInformation function.

But MSDN says on 'privileges' (

Before you can perform a privileged operation, you must first enable the necessary privileges in your access token. To do this, call the OpenThreadToken function to get a handle to your primary or impersonation access token, then call the AdjustTokenPrivileges function to enable the necessary privileges. After performing the privileged operation, call AdjustTokenPrivileges again to disable the privileges. For sample code that enables and disables a token's privileges, see Enabling and Disabling Privileges.

I don't get this paradox... BTW, I"m adjusting the *process*'s access token because that is what MSDN showed me at
Question by:lowlevel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 1403269
This means that you don't have that priviledge. Priviledge can be enabled only if you have it. You can check in User Manager Policies\User rights menu if you have a priviledge to shutdown the system. If you don't - you need to grant yourself this priviledge either in User Manager or by LSA functions, which are not fully documented. You can find some examples of their use in MSDN.

Accepted Solution

pavel030297 earned 200 total points
ID: 1403270
You have some error in your code.
You make the following assignment:
which is wrong.
You should get luid using LookupPriviledgeValue function:
(It's C code, I hope you can convert it)


Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question