我想修改计算机的时间,例如我想把2002-7-1 16:46:26设定为当前的时间,高手怎么办啊?(35分)

  • 主题发起人 主题发起人 xiaoxiami1
  • 开始时间 开始时间
X

xiaoxiami1

Unregistered / Unconfirmed
GUEST, unregistred user!
我想修改计算机的时间,例如我想把2002-7-1 16:46:26设定为当前的时间,高手怎么办啊?
 
var MyTime:TSystemTime;
begin
MyTime.wYear:=2002;
MyTime.wMonth:=7;
MyTime.wDay:=1;
MyTime.wHour:=16;
MyTime.wMinute:=46;
MyTime.wSecond:=26;
SetSystemTime(MyTime);
用SetLocalTime也可以。
 
用SetLocalTime()函数。
 
晚了一步啊
 
1.用系统的修改时间就可以了
2.如要用程序完成,下面的方法不知行否?
procedure ChangeSystemDateTime;
var
SystemTime: TSystemTime;
yy,mon,dd,hh,min,ss,ms:word;
begin
decodedate(StrToDateTime('2002-7-1 16:46:26'),yy,mon,dd);
decodetime(StrToDateTime('2002-7-1 16:46:26'),hh,min,ss,ms);
With SystemTime Do Begin
wYear:=yy;
wMonth:=mon;
wDay:=dd;
wHour:=hh;
wMinute:=min;
wSecond:=ss;
wMilliseconds:=ms;
End;
SetLocalTime(SystemTime);
SendMessage(HWND_BROADCAST,WM_TIMECHANGE,0,0) ;
end;
 
1.}

{
For Windows 9X/ME/NT/2000/XP:

The SetLocalTime function fails if the calling process does not have
the SE_SYSTEMTIME_NAME privilege. This privilege is disabled by default.
Use the AdjustTokenPrivileges function to enable this privilege.
}

function SetPCSystemTime(dDateTime: TDateTime): Boolean;
const
SE_SYSTEMTIME_NAME = 'SeSystemtimePrivilege';
var
hToken: THandle;
ReturnLength: DWORD;
tkp, PrevTokenPriv: TTokenPrivileges;
luid: TLargeInteger;
dSysTime: TSystemTime;
begin
Result := False;
if (Win32Platform = VER_PLATFORM_WIN32_NT) then
begin
if OpenProcessToken(GetCurrentProcess,
TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
try
if not LookupPrivilegeValue(nil, SE_SYSTEMTIME_NAME, luid) then Exit;
tkp.PrivilegeCount := 1;
tkp.Privileges[0].luid := luid;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if not AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTOKENPRIVILEGES),
PrevTokenPriv, ReturnLength) then
Exit;
if (GetLastError <> ERROR_SUCCESS) then
begin
raise Exception.Create(SysErrorMessage(GetLastError));
Exit;
end;
finally
CloseHandle(hToken);
end;
end;
end;
DateTimeToSystemTime(dDateTime, dSysTime);
Result := Windows.SetLocalTime(dSysTime);
end;

{************************************************************}

{2.}

procedure TForm1.Button1Click(Sender: TObject);
var
SystemTime: TSystemTime;
NewTime, NewDate: string;
begin
NewTime := '13:58:00';
NewDate := '02.02.2001'; // or '02/02/01'
DateTimeToSystemTime(StrToDate(NewDate) + StrToTime(NewTime), SystemTime);
SetLocalTime(SystemTime);
// Tell windows, that the Time changed!
PostMessage(HWND_BROADCAST, WM_TIMECHANGE, 0, 0); // *
end;

{
Windows 2000 and later: An application should not broadcast
the WM_TIMECHANGE message because the system will broadcast
this message when the application changes the system time.
}

{************************************************************}

{3.}

function SetSystemTime(DateTime: TDateTime): Boolean;
{ (c) by UNDO }
var
tSetDati: TDateTime;
vDatiBias: Variant;
tTZI: TTimeZoneInformation;
tST: TSystemTime;
begin
GetTimeZoneInformation(tTZI);
vDatiBias := tTZI.Bias / 1440;
tSetDati := DateTime + vDatiBias;
with tST do
begin
wYear := StrToInt(FormatDateTime('yyyy', tSetDati));
wMonth := StrToInt(FormatDateTime('mm', tSetDati));
wDay := StrToInt(FormatDateTime('dd', tSetDati));
wHour := StrToInt(FormatDateTime('hh', tSetDati));
wMinute := StrToInt(FormatDateTime('nn', tSetDati));
wSecond := StrToInt(FormatDateTime('ss', tSetDati));
wMilliseconds := 0;
end;
Result := Windows.SetSystemTime(tST);
end;
 
可是时间总是快了8小时啊?
 
SetLocalTime
SetSystemTime
相差8小时,因为我们在东八区。[:D]
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
928
DelphiTeacher的专栏
D
D
回复
0
查看
883
DelphiTeacher的专栏
D
D
回复
0
查看
856
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部