Mega Code Archive

 
Categories / Delphi / System
 

Sistem tarihini ve saatini değiştirme

1.} 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. } {************************************************************} {2.} 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; {************************************************************} {3.} function IsNT: Boolean; var OS: TOSVersionInfo; begin ZeroMemory(@OS, SizeOf(OS)); OS.dwOSVersionInfoSize := SizeOf(OS); GetVersionEx(OS); Result := OS.dwPlatformId = VER_PLATFORM_WIN32_NT; end; procedure SetDateTime(dDateTime: TDateTime); var dSysTime: TSystemTime; buffer: DWORD; tkp, tpko: TTokenPrivileges; hToken: THandle; begin if IsNT then begin if not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then Exit; LookupPrivilegeValue(nil, 'SE_SYSTEMTIME_NAME', tkp.Privileges[0].Luid); tkp.PrivilegeCount := 1; tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; if not AdjustTokenPrivileges(hToken, False, tkp, SizeOf(tkp), tpko, buffer) then Exit; end; DateTimeToSystemTime(dDateTime, dSysTime); Windows.SetLocalTime(dSysTime); end