delphi下如何才能拥有SE_SHUTDOWN_NAME特权???

funch 2002-06-17 10:05:20

我在windows2000server中要用程序控制重新启动计算机,

使用ExitWindowsEx(EWX_REBOOT,0)时,

需要拥有 SE_SHUTDOWN_NAME 权限,我该如何获得这个权限呢?

希望大家帮我,先谢谢了。
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongxsoft 2002-06-17
  • 打赏
  • 举报
回复
给你段代码:

//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 processes

function WinExitInNT( iFlags : integer ) : boolean;
begin
Result := True;
if ( SetPrivilege( 'SeShutdownPrivilege',True ) ) then
begin
if( not ExitWindowsEx( iFlags,0 ) )then
begin
Result := False;
end;
SetPrivilege( 'SeShutdownPrivilege',False )
end
else 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' privilege

function SetPrivilege(sPrivilegeName : string;
bEnabled : boolean ): boolean;
var
TPPrev,TP : TTokenPrivileges;
Token : THandle;
dwRetLen : DWord;
begin
Result := False;
//opens the access token associated with a process.
OpenProcessToken(GetCurrentProcess //handle to process
TOKEN_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 privilege
TP.Privileges[ 0 ].LUID ) // address of locally unique identifier
)then
begin
if( bEnabled )then //Give this privileges
begin
TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
end
else begin //NOT Give this privileges
TP.Privileges[ 0 ].Attributes := 0;
end;
dwRetLen := 0; //enables or disables privileges in the specified access token.
Result := AdjustTokenPrivileges(Token // handle to token that contains privileges
False //modifies privileges
TP // pointer to new privilege information
SizeOf( TPPrev ) // size in bytes of the TPPrev buffer TPPrev
// receives original state of changed privileges
dwRetLen // receives required size of the TPPrev buffer
);
end;
CloseHandle( Token );
end;
happyjoe 2002-06-17
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
hToken,ReturnLength: Cardinal;
MyTokenPrivileges: TOKEN_PRIVILEGES;
begin
ReturnLength := 0;

Win32Check(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken));
Win32Check(LookupPrivilegeValue(nil,'SeShutdownPrivilege',MyTokenPrivileges.Privileges[0].Luid));
MyTokenPrivileges.PrivilegeCount := 1; // one privilege to set
MyTokenPrivileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
Win32Check(AdjustTokenPrivileges(hToken,false,MyTokenPrivileges,0,nil,ReturnLength));
Win32Check(CloseHandle(hToken));
Win32Check(ExitWindowsEx(EWX_POWEROFF + EWX_FORCEIFHUNG, 0));
end;
happyjoe 2002-06-17
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
hToken,ReturnLength: Cardinal;
MyTokenPrivileges: TOKEN_PRIVILEGES;
begin
ReturnLength := 0;

Win32Check(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken));
Win32Check(LookupPrivilegeValue(nil,'SeShutdownPrivilege',MyTokenPrivileges.Privileges[0].Luid));
MyTokenPrivileges.PrivilegeCount := 1; // one privilege to set
MyTokenPrivileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
Win32Check(AdjustTokenPrivileges(hToken,false,MyTokenPrivileges,0,nil,ReturnLength));
Win32Check(CloseHandle(hToken));
Win32Check(ExitWindowsEx(EWX_POWEROFF + EWX_FORCEIFHUNG, 0));
end;
bcb_fans 2002-06-17
  • 打赏
  • 举报
回复
帮你顶一下!

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧