退出window2000(100分)

  • 主题发起人 主题发起人 树熊133
  • 开始时间 开始时间

树熊133

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么在win2000下方法
exitwindowsex (ewx_reboot,0)
exitwindowsex (ewx_poweroff,0)
exitwindowsex (ewx_shutdown,0)
无法使用,如何使他生效,请各位大哥教教小弟。
 
我知道nt下的问题
NT中使用ExitWindowsEx时,
需设置SE_SHUTDOWN_NAME 权限
(用AdjustTokenPrivileges())。
否则只是重新登录罢了。

 
Win2000或NT中退出Windows时,需要获取退出权限。具体代码如下:

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

//iFlags:
//下面一种情况必须被指定
// EWX_LOGOFF
// EWX_REBOOT
// EWX_SHUTDOWN
// 以下标记可以组合使用
// 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;


procedure TfrmMain.btnExitClick(Sender: TObject);
begin
WinExitInNT(EWX_SHUTDOWN+EWX_POWEROFF+EWX_FORCE);
end;

在Win2000下测试通过,呵呵,给分吧。
 
多人接受答案了。
 
function SetPrivilege(aPrivilegeName : string

aEnabled : boolean ): boolean

var
TPPrev,
TP : TTokenPrivileges

Token : THandle

dwRetLen : DWord

begin
Result := False

OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES
or TOKEN_QUERY, @Token )


TP.PrivilegeCount := 1

if( LookupPrivilegeValue(nil, PChar( aPrivilegeName ),
TP.Privileges[ 0 ].LUID ) ) then
begin
if( aEnabled )then
TP.Privileges[0].Attributes:= SE_PRIVILEGE_ENABLED

else
TP.Privileges[0].Attributes:= 0


dwRetLen := 0

Result := AdjustTokenPrivileges(Token,False,TP,
SizeOf( TPPrev ),
TPPrev,dwRetLen )

end


CloseHandle( Token )

end



function WinExit( iFlags : integer ) : boolean

// possible Flags:
// EWX_LOGOFF
// EWX_REBOOT
// EWX_SHUTDOWN
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
Result := False

end

end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部