exitwindowsex不起作用,看帮助说是要取得重起系统的权利,请问怎么才能获取权利呢?
解决方案 »
- 紧急求助-Windows 服务程序的制作
- Delphi中,怎么把Ascall码字符转换成数字?
- 有一个数组的问题请教?
- 我发布了一款软件,是直接将IDE里的文字处理成RTF或HTML格式,欢迎大家下载使用~~
- 几个简单问题,1、句柄2、APi函数的一些参数得怎么理解阿?比如-----
- 一个关于pos机系统开发的小问题!
- 求助,求助, 如何监控 IE中Silverlight程序,Silverlight完成一个查询,如何监控是否返回处理结果
- 请教打印高手,极臆怪的问题等着你解决,300分相送!!
- 请教delphi中的原始问题
- 那位高人能指点一下,如何制作像realplayer那样的状态栏,谢谢,必定给分
- 编程添加NT中具有Admin权限的用户?
- 我现在得到一个别的程序的实列句柄,现在我想根据该句柄找到该程序 的文件名和路径,(在线等待!)
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; // Borland forgot this declaration
var
hToken: THandle;
NewTP: TTokenPrivileges;
PreTP: TTokenPrivileges;
Zero: DWord;
begin
if Application.MessageBox('现在就重启Windows系统,还是待会再说?',
'Regfere提示您', MB_ICONWARNING + MB_OKCANCEL) = IDOK then
begin
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME, NewTP.Privileges[0].Luid);
NewTP.PrivilegeCount := 1;
NewTP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, False, NewTP, SizeOf(TTokenPrivileges), PreTP, Zero);
ExitWindowsEx(EWX_REBOOT, 0);
end
else
Exit;
end;
Windows, Messages, SysUtils, Variants, 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;
//******************
//在Windows2000下关闭计算机
function ShutDownSystem():BOOL;
var
hProcess,hAccessToken:THandle;
LUID_AND_ATTRIBUTES:TLUIDAndAttributes;
TOKEN_PRIVILEGES: TTokenPrivileges;
BufferIsNull:DWORD;
Const
SE_SHUTDOWN_NAME='SeShutdownPrivilege';
begin
hProcess:=GetCurrentProcess();OpenProcessToken(hprocess,TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,hAccessToken);
LookupPrivilegeValue(Nil,SE_SHUTDOWN_NAME,LUID_AND_ATTRIBUTES.Luid);
LUID_AND_ATTRIBUTES.Attributes:=SE_PRIVILEGE_ENABLED;
TOKEN_PRIVILEGES.PrivilegeCount:=1;
TOKEN_PRIVILEGES.Privileges[0]:=LUID_AND_ATTRIBUTES;
BufferIsNull:=0;AdjustTokenPrivileges(hAccessToken,False,TOKEN_PRIVILEGES,sizeof(TOKEN_PRIVILEGES),Nil,BufferIsNull);
ExitWindowsEx(EWX_shutdown, 0);
ShutDownSystem:=True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShutDownSystem
end;end.
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;
ExitWindowsEx(EWX_REBOOT, 0);
end;