定时关机问题,在线等.....急(50分)

  • 主题发起人 主题发起人 wuyongzhen
  • 开始时间 开始时间
W

wuyongzhen

Unregistered / Unconfirmed
GUEST, unregistred user!
谁知道,怎么用DELPHI 实现定时关机啊windows.exitwindowscs(0,0)好象是重起,把参数该成什么就是关机了啊,我试了几个都是重起.
 
http://pd.szhqzx.net/zlc/Article%5Cdelphiuseapiexp.htm
 
procedure TCloseAllAppAndShutDown.run;
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);

if ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF) then
Raise Exception.create('ExitWindowsEx failed with code '
+ inttostr(GetLastError));;
end;
 
以下的两个函数,执行WinExitInNT(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF)

function SetPrivilege(sPrivilegeName: string;
bEnabled: Boolean): Boolean;
var
TPPrev,TP:TTokenPrivileges;
Token:THandle;
dwRetLen:DWord;
begin
Result:=False;
OpenProcessToken(
GetCurrentProcess,
TOKEN_ADJUST_PRIVILEGES
or TOKEN_QUERY,
Token);
TP.PrivilegeCount:=1;
if (LookupPrivilegeValue(
Nil,
Pchar(sPrivilegeName),
TP.Privileges[0].LUID)
) then
begin
if (bEnabled) then
begin
TP.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
end;
end else
begin
TP.Privileges[0].Attributes:=0;
end;
dwRetLen:=0;
Result:=AdjustTokenPrivileges(
Token,
False,
TP,
SizeOf(TPPrev),
TPPrev,
dwRetLen
);
CloseHandle(Token);
end;

function WinExitInNT(iFlags: integer): boolean;
begin
Result:=True;
//SE_SHUTDONW_NAME的权限值是SEShutdownPrivilege
if (SetPrivilege('SeShutdownPrivilege',True)) then
begin
if (not ExitWindowsEx(iFlags,0)) then
Result:=False;
SetPrivilege('SeShutdownPrivilege',False);
end
else begin
Result:=False;//句柄错误
end;
end;
 
帮顶!

╭=========================================╮

80G海量源代码,控件,书籍全免费狂下不停!

http://www.source520.com

╰=========================================╯
 
多人接受答案了。
 
后退
顶部