定时关机问题,在线等.....急 找速达软件
2楼: http://pd.szhqzx.net/zlc/Article%5Cdelphiuseapiexp.htm 如速达软件
3楼: 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;
4楼: 以下的两个函数,执行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;
5楼: 帮顶!
╭=========================================╮
80G海量源代码,控件,书籍全免费狂下不停!
http://www.source520.com
╰=========================================╯
6楼: 多人接受答案了。