procedure TzlUtils.ExitWindows32(AShutDownFlag: Word);
// ShutDownFlag in [EWX_REBOOT, EWX_SHUTDOWN, EWX_LOGOFF, EWX_POWEROFF] function ChangeNTSecurityForShutdown: Boolean;
var
hToken : THandle;
tkp, Newt : TTokenPrivileges;
retlength : DWORD;
begin
Result := False;
if OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES +
TOKEN_QUERY, hToken) <> False then
begin
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege',
tkp.Privileges[0].Luid) = True then
begin
tkp.PrivilegeCount := 1; // One to set
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
Result := AdjustTokenPrivileges(hToken, False, tkp,
SizeOf(TTokenPrivileges),
Newt, retlength);
end;
end;
end;begin
if Win32PlatForm = VER_PLATFORM_WIN32_NT then
begin
if ChangeNTSecurityForShutdown then
ExitWindowsEx(AShutDownFlag, 0);
end
else if Win32PlatForm = VER_PLATFORM_WIN32_WINDOWS then
ExitWindows(AShutDownFlag, 0);
end;我用上面的函数在win2000或winxp都可以成功的关机、重起、注销,但是在win98下所有的操作都变成了注销,不知道为什么,请问各位大侠该如何解决?
// ShutDownFlag in [EWX_REBOOT, EWX_SHUTDOWN, EWX_LOGOFF, EWX_POWEROFF] function ChangeNTSecurityForShutdown: Boolean;
var
hToken : THandle;
tkp, Newt : TTokenPrivileges;
retlength : DWORD;
begin
Result := False;
if OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES +
TOKEN_QUERY, hToken) <> False then
begin
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege',
tkp.Privileges[0].Luid) = True then
begin
tkp.PrivilegeCount := 1; // One to set
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
Result := AdjustTokenPrivileges(hToken, False, tkp,
SizeOf(TTokenPrivileges),
Newt, retlength);
end;
end;
end;begin
if Win32PlatForm = VER_PLATFORM_WIN32_NT then
begin
if ChangeNTSecurityForShutdown then
ExitWindowsEx(AShutDownFlag, 0);
end
else if Win32PlatForm = VER_PLATFORM_WIN32_WINDOWS then
ExitWindows(AShutDownFlag, 0);
end;我用上面的函数在win2000或winxp都可以成功的关机、重起、注销,但是在win98下所有的操作都变成了注销,不知道为什么,请问各位大侠该如何解决?
ExitWindows(AShutDownFlag, 0);请看这段,我不是用ExitWindows的吗?不行啊!
function GetWinVersion: String; var VersionInfo : TOSVersionInfo; OSName : String; begin // set the size of the record VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );
if Windows.GetVersionEx( VersionInfo ) then begin with VersionInfo do begin case dwPlatformId of VER_PLATFORM_WIN32s : OSName := 'Win32s'; VER_PLATFORM_WIN32_WINDOWS : OSName := 'Windows 95'; VER_PLATFORM_WIN32_NT : OSName := 'Windows NT'; end; // case dwPlatformId Result := OSName + ' Version ' + IntToStr( dwMajorVersion ) + '.' + IntToStr( dwMinorVersion ) + #13#10' (Build ' + IntToStr( dwBuildNumber ) + ': ' + szCSDVersion + ')'; end; // with VersionInfo end // if GetVersionEx else Result := ''; end;
procedure ShutDown; const SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; // Borland forgot this declaration var hToken : THandle; tkp : TTokenPrivileges; tkpo : TTokenPrivileges; zero : DWORD; begin if Pos( 'Windows NT', GetWinVersion) = 1 then // we've got to do a whole buch of things begin zero := 0; if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK ); Exit; end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK ); Exit; end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) // SE_SHUTDOWN_NAME if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then begin MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK ); Exit; end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid ) tkp.PrivilegeCount := 1; tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero ); if Boolean( GetLastError() ) then begin MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK ); Exit; end // if Boolean( GetLastError() ) else ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 ); end // if OSVersion = 'Windows NT' else begin // just shut the machine down ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 ); end; // else end;