如何在WINDOWS2000中实现关机(50分)

  • 主题发起人 主题发起人 zhufengWz
  • 开始时间 开始时间
Z

zhufengWz

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在WINDOWS2000中实现关机。
注:我找到InitiateSystemShutdown命令,但这个命令要使用的前提是
“To shut down the local computer, the calling process must have the SE_SHUTDOWN_NAME privilege. ”
请高手解答
 
当然你非得要有关机的权利
 
来自:温柔一刀 时间:2000-6-20 18:41:11 ID:268738
procedure ExitWindowsNT(uFlags : integer);
var
hToken : THANDLE;
tkp, tkDumb : TTokenPrivileges;
DumbInt : integer;
begin
FillChar(tkp, sizeof(tkp), 0);
// Get a token for this process
if not (OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES
or TOKEN_QUERY, hToken)) then
raise Exception.create('OpenProcessToken failed with code '
+ inttostr(GetLastError));

// Get the LUID for the Shutdown privilege
LookupPrivilegeValue(nil, pchar('SeShutdownPrivilege'),
tkp.Privileges[0].Luid);

tkp.PrivilegeCount := 1; // one privilege to set
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

// Get the shutdown provolege for this process
AdjustTokenPrivileges(hToken, false, tkp, sizeof(tkDumb), tkDumb, DumbInt);

// Cannot test the return value of AdjustTokenPrivileges
if GetLastError <> ERROR_SUCCESS then
Raise Exception.create('AdjustTokenPrivileges failed with code '
+ inttostr(GetLastError));

// shut down the system and for all applications to close
if not ExitWindowsEx(uFlags, 0) then
Raise Exception.create('ExitWindowsEx failed with code '
+ inttostr(GetLastError));
end;

调用方法:
ExitWindowsNT(EWX_SHUTDOWN or EWX_FORCE)
ExitWindowsNT(EWX_REBOOT)等等


 
还有权限问题如何解决?
 
用ExitWindowsEx.
 
请继续或结束
 
请继续或结束
 
WJIACHUN中已解决。为什么还不结束。
 
接受答案了.
 


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
procedure AdjustToken;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

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.Button1Click(Sender: TObject);
begin
AdjustToken;
ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF);
end;

end
 
后退
顶部