procedure Tform1.windowsexitorreboot(flag: Integer); //关机、重起 var VerInfo:TOSVersionInfo; hToken:THANDLE; tkp:TOKEN_PRIVILEGES; Nothing:Cardinal; begin VerInfo.dwOSVersionInfoSize:=SizeOf(VerInfo); GetVersionEx(VerInfo); if VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT then Begin OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken); LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid); tkp.PrivilegeCount:= 1; tkp.Privileges[0].Attributes:= SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil, Nothing); End; case flag of 0 : begin ExitWindowsEx(EWX_POWEROFF,0);//关机 end ; 1 : begin ExitWindowsEx(EWX_REBOOT,0);//从启 end ; end ; end;
转载:NT中的关闭计算机,重新登陆的实现在NT中要实现以上的功能,首先必须获得SE_SHUTDOWN_NAME权限后,才能调用ExitWindowsEx;这是和Windows95的不同//iFlags:// one of the following must be specified// EWX_LOGOFF// EWX_REBOOT// EWX_SHUTDOWN// following attributes may be combined with above flags// EWX_POWEROFF// EWX_FORCE : terminate processesfunction WinExitInNT( iFlags : integer ) : boolean;beginResult := True;if( SetPrivilege( 'SeShutdownPrivilege'True ) )thenbeginif( not ExitWindowsEx( iFlags0 ) )thenbeginResult := False;end;SetPrivilege( 'SeShutdownPrivilege'False )endelse begin// handle errors...Result := False;end;end; //Enabled or DisEnabled the SE_SHUTDOWN_NAME privilege//sPrivilegeName:// 'SE_SHUTDOWN_NAME'//bEnabled :// Enabled or DisEnabled 'SE_SHUTDOWN_NAME' privilegefunction SetPrivilege(sPrivilegeName : string;bEnabled : boolean ): boolean;varTPPrev TP : TTokenPrivileges;Token : THandle;dwRetLen : DWord;beginResult := False; //opens the access token associated with a process.OpenProcessToken(GetCurrentProcess//handle to processTOKEN_ADJUST_PRIVILEGES //Required to change the privileges specified in an access token.or TOKEN_QUERY//Required to query the contents of an access token.Token); TP.PrivilegeCount := 1;//retrieves the locally unique identifier (LUID) used on a specified system to//locally represent the specified privilege name.if( LookupPrivilegeValue(Nil//attempts to find the privilege name on the local system.PChar( sPrivilegeName )// address of string specifying the privilegeTP.Privileges[ 0 ].LUID ) // address of locally unique identifier)thenbeginif( bEnabled )then //Give this privilegesbeginTP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;endelse begin //NOT Give this privilegesTP.Privileges[ 0 ].Attributes := 0;end; dwRetLen := 0;//enables or disables privileges in the specified access token.Result := AdjustTokenPrivileges(Token// handle to token that contains privilegesFalse//modifies privilegesTP// pointer to new privilege informationSizeOf( TPPrev )// sizein bytesof the TPPrev bufferTPPrev// receives original state of changed privilegesdwRetLen // receives required size of the TPPrev buffer);end; CloseHandle( Token );end;
var
VerInfo:TOSVersionInfo;
hToken:THANDLE;
tkp:TOKEN_PRIVILEGES;
Nothing:Cardinal;
begin
VerInfo.dwOSVersionInfoSize:=SizeOf(VerInfo);
GetVersionEx(VerInfo);
if VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT then
Begin
OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken);
LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
tkp.PrivilegeCount:= 1;
tkp.Privileges[0].Attributes:= SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil, Nothing);
End;
case flag of
0 :
begin
ExitWindowsEx(EWX_POWEROFF,0);//关机
end ;
1 :
begin
ExitWindowsEx(EWX_REBOOT,0);//从启
end ;
end ;
end;
//Enabled or DisEnabled the SE_SHUTDOWN_NAME privilege//sPrivilegeName:// 'SE_SHUTDOWN_NAME'//bEnabled :// Enabled or DisEnabled 'SE_SHUTDOWN_NAME' privilegefunction SetPrivilege(sPrivilegeName : string;bEnabled : boolean ): boolean;varTPPrev
TP : TTokenPrivileges;Token : THandle;dwRetLen : DWord;beginResult := False;
//opens the access token associated with a process.OpenProcessToken(GetCurrentProcess//handle to processTOKEN_ADJUST_PRIVILEGES //Required to change the privileges specified in an access token.or TOKEN_QUERY//Required to query the contents of an access token.Token);
TP.PrivilegeCount := 1;//retrieves the locally unique identifier (LUID) used on a specified system to//locally represent the specified privilege name.if( LookupPrivilegeValue(Nil//attempts to find the privilege name on the local system.PChar( sPrivilegeName )// address of string specifying the privilegeTP.Privileges[ 0 ].LUID ) // address of locally unique identifier)thenbeginif( bEnabled )then //Give this privilegesbeginTP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;endelse begin //NOT Give this privilegesTP.Privileges[ 0 ].Attributes := 0;end;
dwRetLen := 0;//enables or disables privileges in the specified access token.Result := AdjustTokenPrivileges(Token// handle to token that contains privilegesFalse//modifies privilegesTP// pointer to new privilege informationSizeOf( TPPrev )// sizein bytesof the TPPrev bufferTPPrev// receives original state of changed privilegesdwRetLen // receives required size of the TPPrev buffer);end;
CloseHandle( Token );end;