ExitWindowsEx函数为什么用不了?(30分)

  • 主题发起人 主题发起人 风中的狼
  • 开始时间 开始时间

风中的狼

Unregistered / Unconfirmed
GUEST, unregistred user!
代码很简单,页面上只有一个按钮,在Button1.Onclick事件中写
ExitWindowsEx(ewx_poweroff,0);
但执行时什么反应都没有,这是为什么???
 
ID:1172897
 
ID:1172897
 
98下应该没问题.2000和NT下需要调整优先级别.给你点例子.
procedure TForm1.AdjustToken();
var
hdlProcessHandle : Cardinal;
hdlTokenHandle : Cardinal;
tmpLuid : Int64;
tkpPrivilegeCount : Int64;
tkp : TOKEN_PRIVILEGES;
tkpNewButIgnored : TOKEN_PRIVILEGES;
lBufferNeeded : Cardinal;
Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;
begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle,
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),
hdlTokenHandle);

// Get the LUID for shutdown privilege.
LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0] := Privilege[0];
// Enable the shutdown privilege in the access token of this
// process.
AdjustTokenPrivileges(hdlTokenHandle,
False,
tkp,
Sizeof(tkpNewButIgnored),
tkpNewButIgnored,
lBufferNeeded);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
AdjustToken;
ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF);
end;

function SetPrivilege(sPrivilegeName: string; bEnabled: boolean ): boolean;
var
TPPrev,TP : TTokenPrivileges;
Token : THandle;
dwRetLen : DWord;
begin
Result := False;

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;
 
To:peng_qs
在98下也是不能用啊,我奇怪的是为什么在98、NT、XP下ExitWindows函数可以正常使用,
(但那只是注销系统),运行ExitWindowsEx函数就没反应呢??
 
To:peng_qs
在98下也是不能用啊,我奇怪的是为什么在98、NT、XP下ExitWindows函数可以正常使用,
(但那只是注销系统),运行ExitWindowsEx函数就没反应呢??
 
试试
ExitWindowsEx(EWX_SHUTDOWN,0);
 
有此事??????、、
 
to PFans
所有的参数都已经用过了,都没反应~
to hamsoft
不可思议吧,但这是事实啊~~
 
后退
顶部