设置系统时间
在NT/xp中,设置系统时间可能需要特权的,下面的代码可以调整特权来设置时间,和调整关机权限类似。
http://www.chami.com/tips/delphi/120496D.html
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 else
begin
TP.Privileges[ 0 ].Attributes :=
0;
end;
dwRetLen := 0;
Result := AdjustTokenPrivileges(
Token,
False,
TP,
SizeOf( TPPrev ),
TPPrev,
dwRetLen );
end;
CloseHandle( Token );
end;
procedure ChangeSystemTime;
var
st : TSystemTime;
begin
if( SetPrivilege(
'SeSystemtimePrivilege',
True ) )then
begin
GetLocalTime( st );
//
// change time using st structure
// for example, to 10:30pm
//
st.wHour := 22;
st.wMinute := 30;
SetLocalTime( st );
// or :
// SetSystemTime( st );
SetPrivilege(
'SeSystemtimePrivilege',
False );
end;
end;