求windows的关机程序,我的系统是win2k,我用了以下的程序来实现几种API函数的关机
procedure TForm1.Button1Click(Sender: TObject);
begin
if ExitWindowsEx(0,0) then
showmessage('true')
else
showmessage('false');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
ExitWindowsEx(EWX_FORCE,0);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
ExitWindowsEx(EWX_REBOOT,0);
end;
但是根本关不了呀!!
请问是怎么回事,要怎么样才能关机呀!!
procedure TForm1.Button1Click(Sender: TObject);
begin
if ExitWindowsEx(0,0) then
showmessage('true')
else
showmessage('false');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
ExitWindowsEx(EWX_FORCE,0);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
ExitWindowsEx(EWX_REBOOT,0);
end;
但是根本关不了呀!!
请问是怎么回事,要怎么样才能关机呀!!
//关闭系统
procedure Shutdown;
var
hToken:THANDLE;
tkp:TOKEN_PRIVILEGES;
ReturnLength,BufferLength: DWORD;
ver:TOSVersionInfo;
begin
ver.dwOSVersionInfoSize:=sizeof(TOSVersionInfo);
if not GetVersionEX(ver) then Exit; if ver.dwPlatformId<VER_PLATFORM_WIN32_NT then
begin
ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0);
exit;
end; // Get a token for this process.
if not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES OR TOKEN_QUERY, hToken) then
begin
ShowMessage('OpenProcessToken');
Exit;
end;
// Get the LUID for the shutdown privilege.
// SE_SHUTDOWN_NAME :="SeShutdownPrivilege";
// Dest:=@Buf[1]; LookupPrivilegeValue(nil,'SeShutdownPrivilege' , tkp.Privileges[0].Luid); tkp.PrivilegeCount := 1; // one privilege to set
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;// Get the shutdown privilege for this process.
BufferLength:=0;
ReturnLength:=0;
AdjustTokenPrivileges(hToken, LongBool(false), tkp, BufferLength,nil, ReturnLength);// Cannot test the return value of AdjustTokenPrivileges. if GetLastError() <> ERROR_SUCCESS then
begin
ShowMessage('AdjustTokenPrivileges');
Exit;
end;
// Shut down the system and force all applications to close. if not ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0) then
begin
ShowMessage('ExitWindowsEx');
Exit;
end;
end;
procedure ShutDown;
var
hdlProcessHandle: Cardinal;
hdlTokenHandle: Cardinal;
tmpLuid: 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); //取得关机特权的LUID
LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
//设置一个特权
tkp.PrivilegeCount := 1;
tkp.Privileges[0] := Privilege[0];
//在当前进程存取标识中加入关机特权
AdjustTokenPrivileges(hdlTokenHandle,
False,
tkp,
Sizeof(tkpNewButIgnored),
tkpNewButIgnored,
lBufferNeeded); if ForceShutdown then //强制关机
ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF or EWX_FORCE, $FFFF)
else
ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF, $FFFF);
end;
小弟 在线等 也可以加我QQ313739427 我在线等!!!!!!谢谢!!!
首先定义常数
const
EWX_FORCE=4; //关闭所有程序并以其他用户身份登录
EWX_LOGOFF=0; //重新启动计算机并切换到MS-DOS方式
EWX_REBOOT=2; //重新启动计算机
EWX_SHUTDOWN=1;//关闭计算机
运行时给How赋值,让他等于EWX_SHUTDOWN或其他,调用以下语句
ExitWindowsEx(How,0);///////////////////////////////////////////
Function ShutDownWindows(Flags: Byte) : Boolean;
begin
Result := ExitWindowsEx(Flage, 0)
// 如果成功返回 True.
end;Flags可用参数:EWX_LOGOFF:
(=0)
安全地关闭所有进程,并关闭用户登录。EWX_SHUTDOWN:
(=1)
关闭系统。所有缓冲区的内容都能被安全的存盘,所有进程都将被停止。
对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。
对于Windows 95: 不必有任何特权。EWX_REBOOT:
(=2)
关闭并重新启动系统。
对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。
对于Windows 95: 不必有任何特权。EWX_FORCE:
(=4)
强制切断连接,关闭所有应用程序。当使用这个参数时,Windows 将不向正在运行的应用程序发送 WM_QUERYENDSESSION 和 WM_ENDSESSION 消息,有可能造成数据丢失。所以推荐只在紧急时使用这个参数。EWX_POWEROFF:
(=8)
关闭系统并切断电源,需要ATX电源支持。
/////////////////////////////////////
unit Unit1;interfaceuses
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.