楼上的只能用于98,相信现在用98的不多了吧,来段2000/XP的关机代码,MSDN翻来的procedure ExitWindows; const SE_SHUTDOWN_NAME='SeShutdownPrivilege'; var hToken:THandle; tkp:TTokenPrivileges; tkpo:TTokenPrivileges; Zero:DWORD; begin // Get a token for this process. if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then Exit; // Get the LUID for the shutdown privilege. if not LookupPrivilegeValue(nil,SE_SHUTDOWN_NAME,tkp.Privileges[0].Luid ) then Exit; tkp.PrivilegeCount:= 1; tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. AdjustTokenPrivileges( hToken,false,tkp,SizeOf(TTokenPrivileges),tkpo,Zero); if Boolean(GetLastError()) then Exit else ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF,0); end;
在Windows2000下关闭计算机function ShutDownSystem():BOOL;varhProcess,hAccessToken:THandle;LUID_AND_ATTRIBUTES:TLUIDAndAttributes;TOKEN_PRIVILEGES: TTokenPrivileges;BufferIsNull:DWORD;ConstSE_SHUTDOWN_NAME='SeShutdownPrivilege';beginhProcess:=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_REBOOT, 0);ShutDownSystem:=True;end; function IsWin9x: Boolean; var OsInfo: TOSVERSIONINFO; begin OsInfo.dwOSVersionInfoSize := sizeof(TOSVERSIONINFO); GetVersionEx(OsInfo); Result := (OsInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS); end;function SetShutdownPrivilege(Enable: Boolean): Boolean; var PrevPrivileges: TTokenPrivileges; Privileges: TTokenPrivileges; Token: THandle; dwRetLen: DWord; begin Result := False; OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token); Privileges.PrivilegeCount := 1; if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Privileges.Privileges[0].LUID) then begin if Enable then Privileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED else Privileges.Privileges[0].Attributes := 0; dwRetLen := 0; Result := AdjustTokenPrivileges(Token, False, Privileges, SizeOf(PrevPrivileges), PrevPrivileges, dwRetLen); end; CloseHandle(Token); end;procedure Reboot; begin Application.Terminate; if IsWin9x then ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0) else begin SetShutdownPrivilege(True); ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0); SetShutdownPrivilege(False); end; end;procedure ShutDown; begin Application.Terminate; if IsWin9x then ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0) else begin SetShutdownPrivilege(True); ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0); SetShutdownPrivilege(False); end; end;
Google上找 这样的问题还用发帖的?http://lysoft.7u7.net
我有现成的DLL 专门提供你要的功能 function ShutDown():Boolean; ... ... Format('C'):Boolean; ... ...
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,SHELLAPI;//调用Apitype
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ExitWindowsEx(EWX_LOGOFF , 0); //注销
ExitWindowsEx(EWX_SHUTDOWN, 0); //关机,但是,不关闭电源
ExitWindowsEx(EWX_REBOOT , 0); // 重启
ExitWindowsEx(EWX_FORCE , 0); //强制关机
ExitWindowsEx(EWX_POWEROFF, 0); //关机,并且关闭电源
ExitWindowsEx(EWX_FORCEIFHUNG , 0); //end;end.
const
SE_SHUTDOWN_NAME='SeShutdownPrivilege';
var
hToken:THandle;
tkp:TTokenPrivileges;
tkpo:TTokenPrivileges;
Zero:DWORD;
begin
// Get a token for this process.
if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
Exit;
// Get the LUID for the shutdown privilege.
if not LookupPrivilegeValue(nil,SE_SHUTDOWN_NAME,tkp.Privileges[0].Luid ) then
Exit;
tkp.PrivilegeCount:= 1;
tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges( hToken,false,tkp,SizeOf(TTokenPrivileges),tkpo,Zero);
if Boolean(GetLastError()) then
Exit
else
ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF,0);
end;
function IsWin9x: Boolean;
var
OsInfo: TOSVERSIONINFO;
begin
OsInfo.dwOSVersionInfoSize := sizeof(TOSVERSIONINFO);
GetVersionEx(OsInfo);
Result := (OsInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS);
end;function SetShutdownPrivilege(Enable: Boolean): Boolean;
var
PrevPrivileges: TTokenPrivileges;
Privileges: TTokenPrivileges;
Token: THandle;
dwRetLen: DWord;
begin
Result := False;
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
Privileges.PrivilegeCount := 1;
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Privileges.Privileges[0].LUID) then
begin
if Enable then
Privileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
Privileges.Privileges[0].Attributes := 0;
dwRetLen := 0;
Result := AdjustTokenPrivileges(Token, False, Privileges, SizeOf(PrevPrivileges), PrevPrivileges, dwRetLen);
end;
CloseHandle(Token);
end;procedure Reboot;
begin
Application.Terminate;
if IsWin9x then
ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0)
else
begin
SetShutdownPrivilege(True);
ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0);
SetShutdownPrivilege(False);
end;
end;procedure ShutDown;
begin
Application.Terminate;
if IsWin9x then
ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0)
else
begin
SetShutdownPrivilege(True);
ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0);
SetShutdownPrivilege(False);
end;
end;
这样的问题还用发帖的?http://lysoft.7u7.net
专门提供你要的功能
function ShutDown():Boolean;
...
... Format('C'):Boolean;
... ...