View Issue Details

IDProjectCategoryView StatusLast Update
0000169JEDI VCL00 JVCL Componentspublic2002-08-12 14:33
ReporteranonymousAssigned Touser72 
PrioritynormalSeverityfeatureReproducibilityalways
Status closedResolutionfixed 
Product Version 
Target VersionFixed in Version 
Summary0000169: TjvExitWindows : SE_SHUTDOWN_NAME privilege token is not automatically set.
DescriptionAs described in Win32 reference - "To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege". TJvExitWindows should enable this privilege for running process automatically.
Additional InformationNote : In order to use AdjustTokenPrivilege function, the process must open two process tokens --TOKEN_ADJUST_PRIVILEGES and TOKEN_QUERY.

OpenProcessToken(GetCurrentProcess(),
          TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken)
TagsNo tags attached.

Activities

user72

2002-07-15 11:46

  ~0000204

FWIW, this unit has been removed in 2.0

user72

2002-07-16 02:36

  ~0000210

I took a look at how this should be done and I think the following code is correct (at least for D6):

function JclExitWindows(ExitCode: Cardinal):boolean;
const
  SE_SHUTDOWN_NAME = 2;
var
  hToken:Cardinal;
  Privs:TTokenPrivileges;
  ReturnLength:Cardinal;
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    Result := OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY,hToken) and
      LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Privs.Privileges[0].luid);
    if Result then
    begin
      with Privs do
      begin
        PrivilegeCount := 1;
        Privileges[0].Attributes := SE_SHUTDOWN_NAME;
      end;
      Result := AdjustTokenPrivileges(hToken, false, Privs, sizeof(Privs), nil, ReturnLength);
    end;
    if not Result then Exit;
  end;
  Result := ExitWindowsEx(ExitCode,0);
end;

call something like this:

  if not JclExitWindows(EWX_LOGOFF) then
    RaiseLastOSError; // RaiseLastWin32Error for D5

BTW, since I didn't find anything similar in JCL I hereby donate the above code for inclusion in JCL (I think JCL is the better place to put it). I even named it according to the JCL rules <g>.

user72

2002-07-22 17:06

  ~0000281

Now in JclMiscel as ExitWindows (see 0000172)

Issue History

Date Modified Username Field Change