Funzione SetSystemTimeAdjustmentPrecise (sysinfoapi.h)
Abilita o disabilita le regolazioni periodiche dell'ora all'ora del giorno del sistema. Se abilitata, tali regolazioni temporali possono essere utilizzate per sincronizzare l'ora del giorno con altre informazioni sull'ora.
Sintassi
BOOL SetSystemTimeAdjustmentPrecise(
[in] DWORD64 dwTimeAdjustment,
[in] BOOL bTimeAdjustmentDisabled
);
Parametri
[in] dwTimeAdjustment
Fornisce la frequenza di aggiornamento del clock regolata.
[in] bTimeAdjustmentDisabled
Fornisce un flag che specifica la modalità di regolazione dell'ora che il sistema deve utilizzare.
Il valore TRUE indica che il sistema deve sincronizzare l'ora del giorno usando i propri meccanismi interni. In questo caso, il valore di dwTimeAdjustment viene ignorato.
Un valore FALSE indica che l'applicazione è nel controllo e che il valore specificato di dwTimeAdjustment deve essere aggiunto all'ora del giorno in corrispondenza di ogni interruzione dell'aggiornamento dell'orologio.
Valore restituito
Se la funzione ha esito positivo, il valore restituito è diverso da zero.
Se la funzione ha esito negativo, il valore restituito è zero. Per informazioni dettagliate sull'errore, chiamare GetLastError. Un modo in cui la funzione può avere esito negativo è se il chiamante non possiede il privilegio SE_SYSTEMTIME_NAME.
Commenti
Per usare questa funzione, il chiamante deve avere privilegi di tempo di sistema (SE_SYSTEMTIME_NAME). Questo privilegio è disabilitato per impostazione predefinita. Usare la funzione AdjustTokenPrivileges per abilitare il privilegio prima di chiamare questa funzione, quindi disabilitare il privilegio dopo la chiamata di funzione. Per altre informazioni, vedere l'esempio di codice seguente.
Esempio
In questo esempio viene illustrato come abilitare i privilegi di sistema, regolare l'orologio di sistema usando GetSystemTimeAdjustmentPrecise e SetSystemTimeAdjustmentPrecise e come stampare accuratamente le regolazioni correnti del tempo di sistema.
/******************************************************************
*
* ObtainRequiredPrivileges
*
* Enables system time adjustment privilege.
*
******************************************************************/
HRESULT
ObtainRequiredPrivileges()
{
HRESULT hr;
HANDLE hProcToken = NULL;
TOKEN_PRIVILEGES tp = {0};
LUID luid;
if (!LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &luid))
{
hr = HRESULT_FROM_WIN32(GetLastError());
printf("Failed to lookup privilege value. hr=0x%08x\n", hr);
return hr;
}
// get the token for our process
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hProcToken))
{
hr = HRESULT_FROM_WIN32(GetLastError());
printf("Failed to open process token. hr=0x%08x\n", hr);
return hr;
}
// Enable just the SYSTEMTIME privilege
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hProcToken, FALSE, &tp, 0, NULL, NULL))
{
hr = HRESULT_FROM_WIN32(GetLastError());
printf("Failed to adjust process token privileges. hr=0x%08x\n", hr);
}
else
{
hr = S_OK;
printf("Added SYSTEMTIME privilege to the process token\n");
}
if (NULL != hProcToken)
{
CloseHandle(hProcToken);
}
return hr;
}
/******************************************************************
*
* PrintCurrentClockAdjustments
*
* Prints current values of the system time adjustments.
*
******************************************************************/
void
PrintCurrentClockAdjustments()
{
// More granular clock adjustments
DWORD64 ullCurrentAdjustment = 0;
DWORD64 ullTimeIncrement = 0;
BOOL bEnabledPrecise = 0;
HRESULT hrPrecise = S_OK;
// Legacy clock adjustments
DWORD dwCurrentAdjustment = 0;
DWORD dwTimeIncrement = 0;
BOOL bEnabled = 0;
HRESULT hr = S_OK;
if (!GetSystemTimeAdjustmentPrecise(&ullCurrentAdjustment, &ullTimeIncrement, &bEnabledPrecise))
{
hrPrecise = HRESULT_FROM_WIN32(GetLastError());
}
if (!GetSystemTimeAdjustment(&dwCurrentAdjustment, &dwTimeIncrement, &bEnabled))
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
printf("Precise_ADJ:%I64u Precise_INCR:%I64u Precise_EN:%d Precise_hr:0x%08x ADJ:%u INCR:%u EN:%d hr:0x%08x\n",
ullCurrentAdjustment, ullTimeIncrement, bEnabledPrecise, hrPrecise,
dwCurrentAdjustment, dwTimeIncrement, bEnabled, hr);
}
/******************************************************************
*
* RunNewAdjustmentSequence
*
* Adjust the system time using high-resolution
* GetSystemTimeAdjustmentPrecise() and SetSystemTimeAdjustmentPrecise() API.
*
******************************************************************/
void
RunNewAdjustmentSequence(DWORD dwPPMAdjustment)
{
DWORD64 ullCurrentAdjustment = 0;
DWORD64 ullTimeIncrement = 0;
BOOL bEnabledPrecise = 0;
LARGE_INTEGER liPerfCounterFrequency = {0};
DWORD dwNewAdjustmentUnits;
const DWORD cMicroSecondsPerSecond = 1000000;
if (dwPPMAdjustment > 1000)
{
printf("Adjustment too large. Skipping new adjustment sequence.\n");
return;
}
printf("Starting adjustment sequence using new API...\n");
if (!GetSystemTimeAdjustmentPrecise(&ullCurrentAdjustment, &ullTimeIncrement, &bEnabledPrecise))
{
printf("Failed to read the system time adjustment. Adjustment sequence aborted. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
return;
}
(void)QueryPerformanceFrequency(&liPerfCounterFrequency);
printf("System Performance Counter Frequency: %I64u\n",
liPerfCounterFrequency.QuadPart);
dwNewAdjustmentUnits = (DWORD)(((float) dwPPMAdjustment * liPerfCounterFrequency.QuadPart/ cMicroSecondsPerSecond));
printf("Adjusting the system clock by +%d PPM (+%d new units)\n",
dwPPMAdjustment, dwNewAdjustmentUnits);
if (!SetSystemTimeAdjustmentPrecise(ullCurrentAdjustment + dwNewAdjustmentUnits, FALSE))
{
printf("Failed to set the system time adjustment. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
}
PrintCurrentClockAdjustments();
printf("Restoring system clock adjustment settings\n");
if (!SetSystemTimeAdjustmentPrecise(ullCurrentAdjustment, FALSE))
{
printf("Failed to set the system time adjustment. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
}
PrintCurrentClockAdjustments();
printf("Adjusting the system clock by -%d PPM (-%d new units)\n",
dwPPMAdjustment, dwNewAdjustmentUnits);
if (!SetSystemTimeAdjustmentPrecise(ullCurrentAdjustment - dwNewAdjustmentUnits, FALSE))
{
printf("Failed to set the system time adjustment. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
}
PrintCurrentClockAdjustments();
printf("Restoring system clock adjustment settings\n");
if (!SetSystemTimeAdjustmentPrecise(ullCurrentAdjustment, FALSE))
{
printf("Failed to set the system time adjustment. hr:0x%08x\n",
HRESULT_FROM_WIN32(GetLastError()));
}
PrintCurrentClockAdjustments();
printf("Adjustment sequence complete\n\n");
}
Requisiti
Client minimo supportato | Windows 10 [solo app desktop] |
Server minimo supportato | Windows Server 2016 [solo app desktop] |
Piattaforma di destinazione | Windows |
Intestazione | sysinfoapi.h |
Libreria | Mincore.lib |
DLL | Api-ms-win-core-version-l1-2-3.dll |